Skip to content

Commit a5611d3

Browse files
committed
feat: add auto-generated url names
1 parent 5870881 commit a5611d3

File tree

4 files changed

+23
-7
lines changed

4 files changed

+23
-7
lines changed

demo/demo/views/colors/add.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,5 @@ def view(request):
3535
form = ColorAddForm(request.POST or None)
3636
if request.method == "POST" and form.is_valid():
3737
color = form.save()
38-
return redirect(f"/colors/{color.slug}")
38+
return redirect("colors-slug", color.slug)
3939
return render_str(__doc__, request, {"form": form})

demo/demo/views/colors/index.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
44
<ul>
55
{% for color in colors %}
6-
<li><a href="/colors/{{ color.slug }}">{{ color.name }}</a></li>
6+
<li><a href="{% url 'colors-slug' color.slug %}">{{ color.name }}</a></li>
77
{% endfor %}
88
</ul>
99
10-
<p><a href="/colors/add">Add a new color</a></p>
10+
<p><a href="{% url 'colors-add' %}">Add a new color</a></p>
1111
"""
1212

1313
from demo.models import Color

demo/demo/views/index.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
44
<p>Here are some links:</p>
55
<ul>
6-
<li><a href="/colors">Colors</a></li>
7-
<li><a href="/current-time">Current Time</a></li>
6+
<li><a href="{% url 'colors' %}">Colors</a></li>
7+
<li><a href="{% url 'current-time' %}">Current Time</a></li>
88
</ul>
99
"""
1010

file_router/__init__.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@
99

1010
__version__ = "0.1.0"
1111

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+
1223

1324
def file_patterns(start_dir, append_slash=False):
1425
"""
@@ -17,6 +28,10 @@ def file_patterns(start_dir, append_slash=False):
1728
patterns = []
1829
start_dir_re = re.compile(f"^{start_dir}")
1930
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))
2035
for file in files:
2136
if not file.endswith(".py"):
2237
continue
@@ -33,13 +48,14 @@ def file_patterns(start_dir, append_slash=False):
3348
url = "" if file == "index.py" else file.replace(".py", "")
3449
url = start_dir_re.sub("", f"{root}/{url}").strip("/")
3550
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))
3753
return patterns
3854

3955

4056
def render_str(source, request, context=None):
4157
"""
42-
Take
58+
Take a string and respond with a fully rendered template
4359
"""
4460
rendered = Template(source).render(RequestContext(request, context))
4561
return HttpResponse(rendered)

0 commit comments

Comments
 (0)