From 55d1a4e79fce91ab7cb3b138348f275655accab5 Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Wed, 3 Jun 2020 18:23:00 +0200 Subject: [PATCH] Preparation for APIfication --- core/models/__init__.py | 19 ++++++++++--------- core/models/api.py | 21 +++++++++++++++++++++ core/modules/urls.py | 11 ++++++++--- 3 files changed, 39 insertions(+), 12 deletions(-) create mode 100644 core/models/api.py diff --git a/core/models/__init__.py b/core/models/__init__.py index 49504a9..21d6287 100644 --- a/core/models/__init__.py +++ b/core/models/__init__.py @@ -1,9 +1,10 @@ -from core.models.files import * -from core.models.profiles import * -from core.models.auth import * -from core.models.local import * -from core.models.cron import * -from core.models.products import * -from core.models.billable import * -from core.models.services import * -from core.models.invoices import * \ No newline at end of file +from core.models.files import BaseFile, ImageField, File, FileAssociation +from core.models.profiles import ClientGroup, Profile, ClientProfile, AdminProfile +from core.models.auth import LoginSession, PWResetToken, LoginLog, IPLimit +from core.models.local import Currency, TaxPolicy, TaxRule +from core.models.cron import CronLog +from core.models.products import ProductGroup, Product, ProductPlan, ProductPlanItem +from core.models.billable import CycleChoices, Billable +from core.models.services import Service, ServicePlan, ServicePlanItem +from core.models.invoices import Invoice, InvoiceItem +from core.models.api import APIKey \ No newline at end of file diff --git a/core/models/api.py b/core/models/api.py new file mode 100644 index 0000000..0650c91 --- /dev/null +++ b/core/models/api.py @@ -0,0 +1,21 @@ +from django.db.models import Model, UUIDField, ForeignKey, CASCADE, TextField + +from core.models.profiles import AdminProfile + +import uuid + +class APIKey(Model): + key = UUIDField(default=uuid.uuid4) + admin = ForeignKey(AdminProfile, on_delete=CASCADE) + permission_string = TextField(blank=True, default=[]) + + @property + def permissions(self): + return self.permission_string.split(",") + + @permissions.setter + def permissions(self, permissions): + if isinstance(permissions, str): + self.permission_string = permissions + else: + self.permission_string = ",".join(permissions) \ No newline at end of file diff --git a/core/modules/urls.py b/core/modules/urls.py index 36abe17..c2cc9cb 100644 --- a/core/modules/urls.py +++ b/core/modules/urls.py @@ -4,11 +4,16 @@ from django.conf import settings from django.urls import path URLPATTERNS = [] +APIPATTERNS = [] for module in settings.EXPEPHALON_MODULES: try: mou = importlib.import_module(f"{module}.urls") - for url, action, name in mou.ADMIN_URLS: - URLPATTERNS.append(path(f'admin/modules/{module}/{url}', action, name=f"{module}_{name}")) - except (AttributeError, ModuleNotFoundError): + if "ADMIN_URLS" in mou.__dict__.keys(): + for url, action, name in mou.ADMIN_URLS: + URLPATTERNS.append(path(f'admin/modules/{module}/{url}', action, name=f"{module}_{name}")) + if "API_URLS" in mou.__dict__.keys(): + for url, action, name in mou.API_URLS: + URLPATTERNS.append(path(f"api/modules/{module}/{url}", action, name=f"api_{module}_{name}")) + except ModuleNotFoundError: pass