improved plugin functionality

- added abiulity to extend default api router from plugion
- added dability to pass custom model definition to generic model/api functions
- added ability to pass custom API clients to generic API function
This commit is contained in:
vabene1111
2023-05-20 12:53:14 +02:00
parent ddd2f96b85
commit c777cfe5b9
5 changed files with 23 additions and 5 deletions

View File

@ -6,7 +6,7 @@ from rest_framework import permissions, routers
from rest_framework.schemas import get_schema_view
from cookbook.helper import dal
from recipes.settings import DEBUG
from recipes.settings import DEBUG, PLUGINS
from recipes.version import VERSION_NUMBER
from .models import (Automation, Comment, CustomFilter, Food, InviteLink, Keyword, MealPlan, Recipe,
@ -16,7 +16,13 @@ from .models import (Automation, Comment, CustomFilter, Food, InviteLink, Keywor
from .views import api, data, delete, edit, import_export, lists, new, telegram, views
from .views.api import CustomAuthToken
router = routers.DefaultRouter()
# extend DRF default router class to allow including additional routers
class DefaultRouter(routers.DefaultRouter):
def extend(self, r):
self.registry.extend(r.registry)
router = DefaultRouter()
router.register(r'automation', api.AutomationViewSet)
router.register(r'bookmarklet-import', api.BookmarkletImportViewSet)
router.register(r'cook-log', api.CookLogViewSet)
@ -53,6 +59,10 @@ router.register(r'user-space', api.UserSpaceViewSet)
router.register(r'view-log', api.ViewLogViewSet)
router.register(r'access-token', api.AccessTokenViewSet)
for p in PLUGINS:
if c := locate(f'{p["module"]}.urls.{p["api_router_name"]}'):
router.extend(c)
urlpatterns = [
path('', views.index, name='index'),
path('setup/', views.setup, name='view_setup'),
@ -119,7 +129,6 @@ urlpatterns = [
path('api/switch-active-space/<int:space_id>/', api.switch_active_space, name='api_switch_active_space'),
path('api/download-file/<int:file_id>/', api.download_file, name='api_download_file'),
path('dal/keyword/', dal.KeywordAutocomplete.as_view(), name='dal_keyword'),
# TODO is this deprecated? not yet, some old forms remain, could likely be changed to generic API endpoints
path('dal/food/', dal.IngredientsAutocomplete.as_view(), name='dal_food'), # TODO is this deprecated?