forked from ietf-tools/datatracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouters.py
More file actions
31 lines (23 loc) · 1.4 KB
/
routers.py
File metadata and controls
31 lines (23 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# Copyright The IETF Trust 2024, All Rights Reserved
"""Custom django-rest-framework routers"""
from django.core.exceptions import ImproperlyConfigured
from rest_framework import routers
class PrefixedBasenameMixin:
"""Mixin to add a prefix to the basename of a rest_framework BaseRouter"""
def __init__(self, name_prefix="", *args, **kwargs):
self.name_prefix = name_prefix
if len(self.name_prefix) == 0 or self.name_prefix[-1] == ".":
raise ImproperlyConfigured("Cannot use a name_prefix that is empty or ends with '.'")
super().__init__(*args, **kwargs)
def register(self, prefix, viewset, basename=None):
# Get the superclass "register" method from the class this is mixed-in with.
# This avoids typing issues with calling super().register() directly in a
# mixin class.
super_register = getattr(super(), "register")
if not super_register or not callable(super_register):
raise TypeError("Must mixin with superclass that has register() method")
super_register(prefix, viewset, basename=f"{self.name_prefix}.{basename}")
class PrefixedSimpleRouter(PrefixedBasenameMixin, routers.SimpleRouter):
"""SimpleRouter that adds a dot-separated prefix to its basename"""
class PrefixedDefaultRouter(PrefixedBasenameMixin, routers.DefaultRouter):
"""DefaultRouter that adds a dot-separated prefix to its basename"""