9
9
10
10
__version__ = "0.1.0"
11
11
12
+ DISALLOWED_CHARS = re .compile (
13
+ "|" .join (
14
+ [
15
+ r"^-" , # Dash at the start
16
+ r"[<>]" , # Angle brackets (url param wrapper)
17
+ r"\w+\:" , # Letters followed by colon (path converters)
18
+ ]
19
+ )
20
+ )
21
+ TO_DASHES = re .compile ("[/_]" ) # Match slash and underscore
22
+
12
23
13
24
def file_patterns (start_dir , append_slash = False ):
14
25
"""
@@ -17,6 +28,10 @@ def file_patterns(start_dir, append_slash=False):
17
28
patterns = []
18
29
start_dir_re = re .compile (f"^{ start_dir } " )
19
30
for root , dirs , files in os .walk (start_dir ):
31
+ # Reverse the list so files that start with "<" go to the bottom and
32
+ # regular files come to the top. This ensures hard-coded url params
33
+ # always match before variable ones
34
+ files = tuple (reversed (files ))
20
35
for file in files :
21
36
if not file .endswith (".py" ):
22
37
continue
@@ -33,13 +48,14 @@ def file_patterns(start_dir, append_slash=False):
33
48
url = "" if file == "index.py" else file .replace (".py" , "" )
34
49
url = start_dir_re .sub ("" , f"{ root } /{ url } " ).strip ("/" )
35
50
url = (url + "/" ) if append_slash else url
36
- patterns .append (path (url , view_fn ))
51
+ urlname = DISALLOWED_CHARS .sub ("" , TO_DASHES .sub ("-" , url ))
52
+ patterns .append (path (url , view_fn , name = urlname ))
37
53
return patterns
38
54
39
55
40
56
def render_str (source , request , context = None ):
41
57
"""
42
- Take
58
+ Take a string and respond with a fully rendered template
43
59
"""
44
60
rendered = Template (source ).render (RequestContext (request , context ))
45
61
return HttpResponse (rendered )
0 commit comments