From 0844d7ad7e899bce0ba6ee650f6ec0dd938de61f Mon Sep 17 00:00:00 2001 From: chapeau Date: Mon, 23 Nov 2020 19:58:55 +0100 Subject: [PATCH] Add functional views in api router --- api/routers.py | 18 ++++++++++++++++++ api/urls.py | 5 +++++ 2 files changed, 23 insertions(+) diff --git a/api/routers.py b/api/routers.py index d81ab402..9703d040 100644 --- a/api/routers.py +++ b/api/routers.py @@ -4,6 +4,7 @@ # quelques clics. # # Copyright © 2018 Mael Kervella +# Copyright © 2020 Corentin Canebier # # 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 @@ -41,6 +42,7 @@ class AllViewsRouter(DefaultRouter): def __init__(self, *args, **kwargs): self.view_registry = [] + self.functional_view_registry = [] super(AllViewsRouter, self).__init__(*args, **kwargs) def register_viewset(self, *args, **kwargs): @@ -64,6 +66,19 @@ class AllViewsRouter(DefaultRouter): name = self.get_default_name(pattern) 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 def get_default_name(pattern): """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: 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 diff --git a/api/urls.py b/api/urls.py index e64c1eec..5d06dff1 100644 --- a/api/urls.py +++ b/api/urls.py @@ -4,6 +4,7 @@ # quelques clics. # # Copyright © 2018 Maël Kervella +# Copyright © 2020 Corentin Canebier # # 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 @@ -38,12 +39,14 @@ router = AllViewsRouter() urls_viewset = [] urls_view = [] +urls_functional_view = [] for app in settings.INSTALLED_APPS: try: module = import_module(".api.urls", package=app) urls_viewset += getattr(module, "urls_viewset", []) urls_view += getattr(module, "urls_view", []) + urls_functional_view += getattr(module, "urls_functional_view", []) except ImportError: continue @@ -56,6 +59,8 @@ for _url, viewset, name in urls_viewset: for _url, view in urls_view: router.register_view(_url, view) +for _url, view, name in urls_functional_view: + router.register_functional_view(_url, view, name) # TOKEN AUTHENTICATION router.register_view(r"token-auth", views.ObtainExpiringAuthToken)