2
2
import re
3
3
from importlib import import_module
4
4
5
- from django .core .exceptions import ImproperlyConfigured
6
5
from django .http import HttpResponse
7
6
from django .template import RequestContext , Template
8
7
from django .urls import path
12
11
DISALLOWED_CHARS = re .compile (
13
12
"|" .join (
14
13
[
15
- r"^-" , # Dash at the start
14
+ r"^-+ " , # Leading dashes
16
15
r"[<>]" , # Angle brackets (url param wrapper)
17
16
r"\w+\:" , # Letters followed by colon (path converters)
17
+ r"-+$" , # Trailing dashes
18
18
]
19
19
)
20
20
)
@@ -35,20 +35,25 @@ def file_patterns(start_dir, append_slash=False):
35
35
for file in files :
36
36
if not file .endswith (".py" ):
37
37
continue
38
+
38
39
module_path = f"{ root } /{ file } " .replace (".py" , "" ).replace ("/" , "." )
39
- try :
40
- module = import_module (module_path )
41
- except ImportError as exc :
42
- raise ImproperlyConfigured (
43
- f"Failed to import { module_path } . Ensure it's a valid Python file."
44
- ) from exc
40
+ module = import_module (module_path )
45
41
view_fn = getattr (module , "view" , None )
46
42
if not callable (view_fn ):
47
43
continue
48
- url = "" if file == "index.py" else file .replace (".py" , "" )
49
- url = start_dir_re .sub ("" , f"{ root } /{ url } " ).strip ("/" )
50
- url = (url + "/" ) if append_slash else url
51
- urlname = DISALLOWED_CHARS .sub ("" , TO_DASHES .sub ("-" , url ))
44
+
45
+ try :
46
+ url = view_fn .url
47
+ except AttributeError :
48
+ url = "" if file == "index.py" else file .replace (".py" , "" )
49
+ url = start_dir_re .sub ("" , f"{ root } /{ url } " ).strip ("/" )
50
+ url = (url + "/" ) if append_slash else url
51
+
52
+ try :
53
+ urlname = view_fn .urlname
54
+ except AttributeError :
55
+ urlname = DISALLOWED_CHARS .sub ("" , TO_DASHES .sub ("-" , url ))
56
+
52
57
patterns .append (path (url , view_fn , name = urlname ))
53
58
return patterns
54
59
0 commit comments