init
This commit is contained in:
0
app/api/__init__.py
Normal file
0
app/api/__init__.py
Normal file
6
app/api/apps.py
Normal file
6
app/api/apps.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class ApiConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'api'
|
||||
19
app/api/autoload.py
Normal file
19
app/api/autoload.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from django.conf import settings
|
||||
|
||||
import importlib
|
||||
|
||||
|
||||
def load_viewset_registries(router):
|
||||
"""
|
||||
Parcourt toutes les apps installées et appelle leur register_viewsets(router) si présent.
|
||||
"""
|
||||
for app in settings.INSTALLED_APPS:
|
||||
module_path = f"{app}.api.registry"
|
||||
try:
|
||||
module = importlib.import_module(module_path)
|
||||
if hasattr(module, "register_viewsets"):
|
||||
module.register_viewsets(router)
|
||||
except ModuleNotFoundError:
|
||||
continue
|
||||
except AttributeError as e:
|
||||
print(f"[ERROR] Could not import {module_path}: {e}")
|
||||
4
app/api/routers.py
Normal file
4
app/api/routers.py
Normal file
@@ -0,0 +1,4 @@
|
||||
from rest_framework import routers
|
||||
|
||||
|
||||
router = routers.DefaultRouter()
|
||||
3
app/api/tests.py
Normal file
3
app/api/tests.py
Normal file
@@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
20
app/api/urls.py
Normal file
20
app/api/urls.py
Normal file
@@ -0,0 +1,20 @@
|
||||
from django.urls import path, include
|
||||
|
||||
from rest_framework.authtoken import views
|
||||
|
||||
from .routers import router
|
||||
from .autoload import load_viewset_registries
|
||||
|
||||
load_viewset_registries(router)
|
||||
|
||||
app_name = 'api'
|
||||
urlpatterns = [
|
||||
# Endpoints d'API REST
|
||||
path('', include(router.urls)),
|
||||
|
||||
# Endpoint d'authentification par token
|
||||
path('auth-token/', views.obtain_auth_token, name='api-token-auth'),
|
||||
|
||||
# Endpoints d'authentification de l'API browsable
|
||||
path('auth/', include('rest_framework.urls', namespace='rest_framework')),
|
||||
]
|
||||
14
app/api/utils.py
Normal file
14
app/api/utils.py
Normal file
@@ -0,0 +1,14 @@
|
||||
import inspect
|
||||
|
||||
|
||||
def register_in_app(router, prefix, viewset, basename=None):
|
||||
# Trouve le module appelant pour déduire le nom de l'app
|
||||
caller = inspect.stack()[1]
|
||||
module = inspect.getmodule(caller.frame)
|
||||
module_parts = module.__name__.split('.')
|
||||
app_label = module_parts[0] # ex: "my_app"
|
||||
|
||||
full_prefix = f"{app_label}/{prefix.strip('/')}"
|
||||
if basename is None:
|
||||
basename = f"{app_label}-{prefix}"
|
||||
router.register(full_prefix, viewset, basename=basename)
|
||||
Reference in New Issue
Block a user