Skip to content

Commit

Permalink
Merge pull request #1 from vitaliypopel/workspace
Browse files Browse the repository at this point in the history
Fixed Router init method and changed FUNC_VIEW type location
  • Loading branch information
vitaliypopel authored Sep 19, 2024
2 parents 81c3162 + 2da8b58 commit a25a2a4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
11 changes: 9 additions & 2 deletions src/django_routify/_abstraction.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
from django.http import HttpRequest, HttpResponse
from django.urls import path

from abc import ABC, abstractmethod
from typing import Callable, Type

FUNC_VIEW: Type = Callable[[HttpRequest, ...], HttpResponse]


class RouterAbstraction(ABC):
Expand Down Expand Up @@ -31,7 +35,7 @@ class RouterAbstraction(ABC):
@abstractmethod
def __init__(
self,
prefix: str,
prefix: str = None,
app_name: str = None,
auto_naming: bool = True,
auto_trailing_slash: bool = False,
Expand All @@ -43,7 +47,10 @@ def __init__(
:param auto_naming: bool
:param auto_trailing_slash: bool
'''
...
raise NotImplementedError(
'Use Router from django_routify, '
'but not RouterAbstraction'
)

@property
@abstractmethod
Expand Down
16 changes: 8 additions & 8 deletions src/django_routify/router.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
from inspect import isclass
from typing import Callable, Type
import re

from django.http import HttpRequest, HttpResponse
from django.urls import path
from django.views import View

from ._abstraction import RouterAbstraction
from ._abstraction import RouterAbstraction, FUNC_VIEW


class Router(RouterAbstraction):
Expand All @@ -28,12 +26,16 @@ def __init__(
auto_naming: bool = True,
auto_trailing_slash: bool = False,
) -> None:
self.__prefix = prefix.rstrip('/').lstrip('/') or ''
if self.__prefix != '':
self.__prefix += '/'
prefix = prefix or ''

self.__prefix = prefix.rstrip('/').lstrip('/') + '/'
if self.__prefix == '/':
self.__prefix = ''

self.__app_name = app_name or ''
self.__auto_naming = auto_naming
self.__auto_trailing_slash = auto_trailing_slash

self.__urls = []

@property
Expand All @@ -57,8 +59,6 @@ def urls(self) -> list[path]:
return self.__urls

def route(self, url_path: str, name: str = None):
FUNC_VIEW: Type = Callable[[HttpRequest, ...], HttpResponse]

def register(view: FUNC_VIEW | View) -> FUNC_VIEW | View:
nonlocal url_path, name

Expand Down

0 comments on commit a25a2a4

Please sign in to comment.