Skip to content

Commit

Permalink
Implemented support for setting methods for views
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaliypopel committed Oct 6, 2024
1 parent 072ca49 commit a126db4
Showing 1 changed file with 32 additions and 2 deletions.
34 changes: 32 additions & 2 deletions src/django_routify/router.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
from django.urls import URLPattern, path
from django.utils.decorators import method_decorator
from django.views.decorators.http import require_http_methods

from django.urls import URLPattern, path
from django.views import View

import re

from ._abstraction import RouterAbstraction
from ._utils import FUNC_VIEW, validate_type
from ._utils import (
ALLOWED_METHODS,
FUNC_VIEW,
validate_type,
)


class Router(RouterAbstraction):
Expand Down Expand Up @@ -74,9 +80,11 @@ def register(view: FUNC_VIEW | View) -> FUNC_VIEW | View:
nonlocal url_path, kwargs

name: str | None = kwargs.get('name', None)
methods: list[str] | None = kwargs.get('methods', None)

validate_type('url_path', url_path, str)
validate_type('name', name, (str, type(None)))
validate_type('methods', methods, (list, type(None)))

if self.__auto_trailing_slash:
url_path = url_path.lstrip('/').rstrip('/')
Expand All @@ -102,6 +110,28 @@ def register(view: FUNC_VIEW | View) -> FUNC_VIEW | View:

name = name.lower()

if methods:
for i in range(len(methods)):
methods[i] = methods[i].upper()
if methods[i] not in ALLOWED_METHODS:
raise ValueError(
f'Method "{methods[i]}" is not in '
f'allowed methods {ALLOWED_METHODS}'
)

require_http_methods_decorator = require_http_methods(methods)

if hasattr(view, 'as_view'):
# wrap Class-Based-View into decorator with require methods
# using method_decorator
view = method_decorator(
decorator=require_http_methods_decorator,
name='dispatch',
)(view)
else:
# wrap Function-Based-View into decorator with require methods
view = require_http_methods_decorator(view)

as_view = view
if hasattr(view, 'as_view'):
view: View
Expand Down

0 comments on commit a126db4

Please sign in to comment.