urls.py
1 from django.urls import path 2 3 4 def protected_path(route, view, protect_fn=None, **kwargs): 5 """ 6 Wrap a URL path to require magic token authorization. 7 8 Args: 9 route: RoutePattern 10 view: view function or class 11 protect_fn: optional callable that is passed captured values of the 12 RequestPattern. Should return True if the path is protected 13 or False if not 14 Usage: 15 from django_magic_authorization.urls import protected_path 16 17 urlpatterns = [ 18 protected_path("private/", views.private_view), 19 protected_path( 20 "<str:visibility>/<int:pk>", 21 views.detail_view, 22 protect_fn=lambda kwargs: kwargs["visibility"] == "private" 23 ), 24 ] 25 """ 26 django_path = path(route, view, **kwargs) 27 setattr(django_path, "_django_magic_authorization", True) 28 setattr(django_path, "_django_magic_authorization_fn", protect_fn) 29 return django_path