dynamic load of apps/urls/databases

This commit is contained in:
Jonathan Baecker 2020-05-04 17:29:39 +02:00
parent 314e7c8d60
commit b22a8ec607
13 changed files with 52 additions and 14 deletions

View File

@ -1,4 +1,4 @@
from api_player.models import GuiSettings, MessengePresets from apps.api_player.models import GuiSettings, MessengePresets
from django.contrib import admin from django.contrib import admin

View File

@ -0,0 +1,11 @@
import os
BASE_DIR = os.path.dirname(os.path.abspath(os.path.join(__file__)))
class Connector:
config = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}

View File

@ -1,4 +1,4 @@
from api_player.models import GuiSettings, MessengePresets from apps.api_player.models import GuiSettings, MessengePresets
from django.contrib.auth.models import User from django.contrib.auth.models import User
from rest_framework import serializers from rest_framework import serializers

View File

@ -8,7 +8,7 @@ import psutil
import yaml import yaml
import zmq import zmq
from api_player.models import GuiSettings from apps.api_player.models import GuiSettings
from django.conf import settings from django.conf import settings
from natsort import natsorted from natsort import natsorted
from pymediainfo import MediaInfo from pymediainfo import MediaInfo

View File

@ -2,8 +2,8 @@ import os
import shutil import shutil
from urllib.parse import unquote from urllib.parse import unquote
from api_player.models import GuiSettings, MessengePresets from apps.api_player.models import GuiSettings, MessengePresets
from api_player.serializers import (GuiSettingsSerializer, MessengerSerializer, from apps.api_player.serializers import (GuiSettingsSerializer, MessengerSerializer,
UserSerializer) UserSerializer)
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django_filters import rest_framework as filters from django_filters import rest_framework as filters

View File

@ -12,6 +12,7 @@ https://docs.djangoproject.com/en/3.0/ref/settings/
import os import os
from datetime import timedelta from datetime import timedelta
from pydoc import locate
# Build paths inside the project like this: os.path.join(BASE_DIR, ...) # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname( BASE_DIR = os.path.dirname(
@ -28,8 +29,7 @@ INSTALLED_APPS = [
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'django_filters', 'django_filters',
'rest_framework', 'rest_framework',
'corsheaders', 'corsheaders'
'api_player'
] ]
MIDDLEWARE = [ MIDDLEWARE = [
@ -67,12 +67,7 @@ WSGI_APPLICATION = 'ffplayout.wsgi.application'
# Database # Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases # https://docs.djangoproject.com/en/3.0/ref/settings/#databases
DATABASES = { DATABASES = {}
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
# Password validation # Password validation
@ -114,6 +109,24 @@ USE_L10N = True
USE_TZ = True USE_TZ = True
# dynamic app loader
APPS_DIR = os.path.join(BASE_DIR, 'apps/')
for dir in os.listdir(APPS_DIR):
if os.path.isdir(os.path.join(APPS_DIR, dir)):
app_name = 'apps.{}'.format(dir)
if app_name not in INSTALLED_APPS:
# add app to installed apps
INSTALLED_APPS += (app_name, )
if os.path.isfile(os.path.join(APPS_DIR, dir, 'db.py')):
db_conn = locate('{}.db.Connector.config'.format(app_name))
if list(db_conn.keys())[0] not in DATABASES:
# add app db to DATABASES
DATABASES.update(db_conn)
# Static files (CSS, JavaScript, Images) # Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/ # https://docs.djangoproject.com/en/3.0/howto/static-files/

View File

@ -13,14 +13,15 @@ Including another URLconf
1. Import the include() function: from django.urls import include, path 1. Import the include() function: from django.urls import include, path
2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
""" """
import os
from django.contrib import admin from django.contrib import admin
from django.conf import settings
from django.urls import include, path from django.urls import include, path
from rest_framework_simplejwt.views import (TokenObtainPairView, from rest_framework_simplejwt.views import (TokenObtainPairView,
TokenRefreshView) TokenRefreshView)
urlpatterns = [ urlpatterns = [
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
path('api/', include('api_player.urls', namespace='api_player')),
path('api-auth/', include( path('api-auth/', include(
'rest_framework.urls', namespace='rest_framework')), 'rest_framework.urls', namespace='rest_framework')),
path('auth/token/', TokenObtainPairView.as_view(), path('auth/token/', TokenObtainPairView.as_view(),
@ -28,3 +29,16 @@ urlpatterns = [
path('auth/token/refresh/', TokenRefreshView.as_view(), path('auth/token/refresh/', TokenRefreshView.as_view(),
name='token_refresh') name='token_refresh')
] ]
# dynamic url loader
for dir in os.listdir(settings.APPS_DIR):
if os.path.isdir(os.path.join(settings.APPS_DIR, dir)):
app_name = 'apps.{}'.format(dir)
_path = path('api/', include(
'{}.urls'.format(app_name),
namespace='{}'.format(app_name.split('.')[1])))
if _path not in urlpatterns:
urlpatterns += (_path, )