8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-23 03:43:12 +00:00

Add functional views in api router

This commit is contained in:
chapeau 2020-11-23 19:58:55 +01:00 committed by Gabriel Detraz
parent 69f78d8c60
commit 0844d7ad7e
2 changed files with 23 additions and 0 deletions

View file

@ -4,6 +4,7 @@
# quelques clics. # quelques clics.
# #
# Copyright © 2018 Mael Kervella # Copyright © 2018 Mael Kervella
# Copyright © 2020 Corentin Canebier
# #
# This program is free software; you can redistribute it and/or modify # This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by # it under the terms of the GNU General Public License as published by
@ -41,6 +42,7 @@ class AllViewsRouter(DefaultRouter):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self.view_registry = [] self.view_registry = []
self.functional_view_registry = []
super(AllViewsRouter, self).__init__(*args, **kwargs) super(AllViewsRouter, self).__init__(*args, **kwargs)
def register_viewset(self, *args, **kwargs): def register_viewset(self, *args, **kwargs):
@ -64,6 +66,19 @@ class AllViewsRouter(DefaultRouter):
name = self.get_default_name(pattern) name = self.get_default_name(pattern)
self.view_registry.append((pattern, view, name)) self.view_registry.append((pattern, view, name))
def register_functional_view(self, pattern, view, name=None):
"""Register a functional view in the router.
Args:
pattern: The URL pattern to use for this view.
view: The functional view to register.
name: An optional name for the route generated. Defaults is
based on the pattern last section (delimited by '/').
"""
if name is None:
name = self.get_default_name(pattern)
self.functional_view_registry.append((pattern, view, name))
@staticmethod @staticmethod
def get_default_name(pattern): def get_default_name(pattern):
"""Returns the name to use for the route if none was specified. """Returns the name to use for the route if none was specified.
@ -155,4 +170,7 @@ class AllViewsRouter(DefaultRouter):
for pattern, view, name in self.view_registry: for pattern, view, name in self.view_registry:
urls.append(url(pattern, view.as_view(), name=name)) urls.append(url(pattern, view.as_view(), name=name))
for pattern, view, name in self.functional_view_registry:
urls.append(url(pattern, view, name=name))
return urls return urls

View file

@ -4,6 +4,7 @@
# quelques clics. # quelques clics.
# #
# Copyright © 2018 Maël Kervella # Copyright © 2018 Maël Kervella
# Copyright © 2020 Corentin Canebier
# #
# This program is free software; you can redistribute it and/or modify # This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by # it under the terms of the GNU General Public License as published by
@ -38,12 +39,14 @@ router = AllViewsRouter()
urls_viewset = [] urls_viewset = []
urls_view = [] urls_view = []
urls_functional_view = []
for app in settings.INSTALLED_APPS: for app in settings.INSTALLED_APPS:
try: try:
module = import_module(".api.urls", package=app) module = import_module(".api.urls", package=app)
urls_viewset += getattr(module, "urls_viewset", []) urls_viewset += getattr(module, "urls_viewset", [])
urls_view += getattr(module, "urls_view", []) urls_view += getattr(module, "urls_view", [])
urls_functional_view += getattr(module, "urls_functional_view", [])
except ImportError: except ImportError:
continue continue
@ -56,6 +59,8 @@ for _url, viewset, name in urls_viewset:
for _url, view in urls_view: for _url, view in urls_view:
router.register_view(_url, view) router.register_view(_url, view)
for _url, view, name in urls_functional_view:
router.register_functional_view(_url, view, name)
# TOKEN AUTHENTICATION # TOKEN AUTHENTICATION
router.register_view(r"token-auth", views.ObtainExpiringAuthToken) router.register_view(r"token-auth", views.ObtainExpiringAuthToken)