Add public routes feature

This commit is contained in:
Erik Kalkoken
2023-08-01 10:20:13 +00:00
committed by Ariel Rin
parent 08fd86db8f
commit 7cb7e2c77b
8 changed files with 269 additions and 55 deletions

View File

@@ -1,15 +1,18 @@
from string import Formatter
from typing import Iterable, Optional
from django.conf import settings
from django.conf.urls import include
from django.urls import re_path
from django.core.exceptions import ObjectDoesNotExist
from django.template.loader import render_to_string
from django.urls import re_path
from django.utils.functional import cached_property
from django.conf import settings
from string import Formatter
from allianceauth.hooks import get_hooks
from .models import NameFormatConfig
def get_extension_logger(name):
"""
Takes the name of a plugin/extension and generates a child logger of the extensions logger
@@ -156,8 +159,32 @@ class MenuItemHook:
class UrlHook:
def __init__(self, urls, namespace, base_url):
"""A hook for registering the URLs of a Django app.
Args:
- urls: The urls module to include
- namespace: The URL namespace to apply. This is usually just the app name.
- base_url: The URL prefix to match against in regex form.
Example ``r'^app_name/'``.
This prefix will be applied in front of all URL patterns included.
It is possible to use the same prefix as existing apps (or no prefix at all),
but standard URL resolution ordering applies
(hook URLs are the last ones registered).
- excluded_views: Optional list of views to be excluded
from auto-decorating them with the
default ``main_character_required`` decorator, e.g. to make them public.
Views must be specified by their qualified name,
e.g. ``["example.views.my_public_view"]``
"""
def __init__(
self,
urls,
namespace: str,
base_url: str,
excluded_views : Optional[Iterable[str]] = None
):
self.include_pattern = re_path(base_url, include(urls, namespace=namespace))
self.excluded_views = set(excluded_views or [])
class NameFormatter:

View File

@@ -0,0 +1,30 @@
from unittest import TestCase
from allianceauth.services.hooks import UrlHook
from allianceauth.groupmanagement import urls
class TestUrlHook(TestCase):
def test_can_create_simple_hook(self):
# when
obj = UrlHook(urls, "groupmanagement", r"^groupmanagement/")
# then
self.assertEqual(obj.include_pattern.app_name, "groupmanagement")
self.assertFalse(obj.excluded_views)
def test_can_create_hook_with_excluded_views(self):
# when
obj = UrlHook(
urls,
"groupmanagement",
r"^groupmanagement/",
["groupmanagement.views.group_management"],
)
# then
self.assertEqual(obj.include_pattern.app_name, "groupmanagement")
self.assertIn("groupmanagement.views.group_management", obj.excluded_views)
def test_should_raise_error_when_called_with_invalid_excluded_views(self):
# when/then
with self.assertRaises(TypeError):
UrlHook(urls, "groupmanagement", r"^groupmanagement/", 99)