Skip to content

Commit 8cf458a

Browse files
feat: add module system (#5)
OMAP can now be started via a config system. More informations can be found in the README. Extend gitignore Renamed PortalModule to OmapModule
1 parent a16501e commit 8cf458a

File tree

9 files changed

+393
-9
lines changed

9 files changed

+393
-9
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,4 @@ dmypy.json
129129
.pyre/
130130

131131
!omap/theme/static/css/dist/
132+
/.idea/

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,23 @@ Speaking of roles, we usually distinguish between **Machine Manufacturers**, i.e
88
The *OMAP* project aims especially at pushing the cooperation of all involved parties over machine data to build ecosystems that lead to a gain for all involved parties.
99

1010
Documentation can be found on [readthedocs](https://omap.readthedocs.io/en/latest/).
11+
12+
## Start the OMAP
13+
14+
OMAP relies on a module system which extends Django Apps.
15+
Just start it with the omap.py script (which is pretty similar to manage.py).
16+
17+
```
18+
python omap.py runserver
19+
```
20+
or
21+
```
22+
./omap.py runserver
23+
```
24+
25+
After this start it behaves exactly like a regular django project.
26+
So migrations can e.g. be done via
27+
```
28+
./omap.py migrate
29+
```
30+
etc...

omap.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env python
2+
"""Django's command-line utility for administrative tasks."""
3+
import os
4+
import sys
5+
6+
from omap.modules import modules
7+
8+
9+
def main():
10+
"""Run administrative tasks."""
11+
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "omap.launcher.settings")
12+
# Here we set the OMAP_MODULES env varible, if not set
13+
os.environ.setdefault("OMAP_MODULES", "")
14+
15+
# Here we inject our custom settings
16+
modules.configure_modules()
17+
try:
18+
from django.core.management import execute_from_command_line
19+
except ImportError as exc:
20+
raise ImportError(
21+
"Couldn't import Django. Are you sure it's installed and "
22+
"available on your PYTHONPATH environment variable? Did you "
23+
"forget to activate a virtual environment?"
24+
) from exc
25+
execute_from_command_line(sys.argv)
26+
27+
28+
if __name__ == "__main__":
29+
main()

omap/core/apps.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@
44
class CoreConfig(AppConfig):
55
default_auto_field = "django.db.models.BigAutoField"
66
name = "omap.core"
7+
8+
url_prefix = "core"

omap/launcher/settings.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,6 @@
4141
"django.contrib.sessions",
4242
"django.contrib.messages",
4343
"django.contrib.staticfiles",
44-
"tailwind",
45-
"omap.core",
46-
"omap.assets",
4744
]
4845

4946
MIDDLEWARE = [
@@ -111,8 +108,6 @@
111108

112109
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
113110

114-
TAILWIND_APP_NAME = "omap/core"
115-
116111
INTERNAL_IPS = [
117112
"127.0.0.1",
118113
]

omap/launcher/urls.py

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,37 @@
1+
import importlib
2+
import logging
3+
4+
from django.apps import apps
5+
from django.conf import settings
16
from django.conf.urls import url, include
7+
from django.conf.urls.static import static
28
from django.contrib import admin
9+
from django.urls import path
10+
11+
urlpatterns = [url(r"admin/", admin.site.urls)] + (
12+
static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
13+
if settings.MEDIA_ROOT
14+
else []
15+
)
16+
17+
"""
18+
Here, apps are registered automatically with their urls if they have a property "url_prefix" set in their AppConfig
19+
AND if they have a valid urls.py file which sets a variable urlpatterns
20+
"""
21+
for config in apps.get_app_configs():
22+
if hasattr(config, "url_prefix"):
23+
logging.debug(f"Found url_prefix in {config.name}, checking for .urls module")
24+
urls_path = config.module.__name__ + ".urls"
25+
try:
26+
mod = importlib.import_module(urls_path)
27+
except ModuleNotFoundError:
28+
logging.debug(f"No url module found under {urls_path}", exc_info=True)
29+
continue
30+
31+
logging.debug(
32+
f"urls.py present under {urls_path}, setting for prefix {config.url_prefix}"
33+
)
34+
# Do the include
35+
urlpatterns.append(path(config.url_prefix + "/", include(urls_path)))
336

4-
urlpatterns = [
5-
url("", include("omap.core.urls")),
6-
url(r"admin/", admin.site.urls),
7-
]
37+
logging.debug(f"Patterns: {urlpatterns}")

omap/modules/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"""
2+
Contains all necessary code for the module system which allows for OMAPs module system to have pluggable apps.
3+
"""

0 commit comments

Comments
 (0)