Skip to content

Commit a6b5ea9

Browse files
committed
Fix catalog navigation flicker by using url reverse instead of relative paths. Fixes #365
1 parent 65ee0af commit a6b5ea9

2 files changed

Lines changed: 35 additions & 26 deletions

File tree

catalog/templates/catalog/finder.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<nav aria-label="breadcrumb" class="d-none d-md-block">
99
<ol class="breadcrumb">
1010
{% for column in columns %}
11-
<li class="breadcrumb-item"><a href="{{ column.category_prefix }}/">{{ column.category.name }}</a></li>
11+
<li class="breadcrumb-item"><a href="{{ column.url }}">{{ column.category.name }}</a></li>
1212
{% endfor %}
1313
</ol>
1414
</nav>
@@ -55,7 +55,7 @@ <h5>
5555
{{ column.category.name }}
5656
</h5>
5757
<ul class="p-0" style="list-style: none;">
58-
{% regroup column.children by get_type_display as children_groups %}
58+
{% regroup column.children by category.get_type_display as children_groups %}
5959
{% for type in children_groups %}
6060
{% if type.grouper != "OTHE" %}
6161
<li>
@@ -71,16 +71,16 @@ <h5>
7171
</div>
7272
</li>
7373
{% endif %}
74-
{% for subcat in type.list %}
74+
{% for child in type.list %}
7575
<li>
76-
<a href="{{ column.category_prefix }}/{{ subcat.slug }}"
76+
<a href="{{ child.url }}"
7777
class="text-body text-decoration-none">
7878
<div class="d-flex align-items-center my-1 ">
7979
<div class="flex-shrink-0 px-2 py-1">
8080

8181
</div>
8282
<div class="flex-grow-1 hover-underline">
83-
{{ subcat.better_name|capfirst }}
83+
{{ child.category.better_name|capfirst }}
8484
</div>
8585
</div>
8686
</a>

catalog/views.py

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,18 @@ def unfollow_all_courses(request):
109109
return redirect("home")
110110

111111

112+
@dataclass
113+
class ChildCategory:
114+
category: Category
115+
url: str
116+
117+
112118
@dataclass
113119
class Column:
114120
category: Category
115-
children: list[Category]
121+
children: list[ChildCategory]
116122
depth: int
117-
category_prefix: str
123+
url: str
118124

119125

120126
def finder(request, slugs: str = ""):
@@ -128,29 +134,32 @@ def finder(request, slugs: str = ""):
128134
raise Http404(f"Invalid category path, {i}")
129135

130136
columns = []
131-
max_depth = len(categories)
132137
for i, category in enumerate(categories):
133-
depth = max_depth - i
134-
path_list = ["."] + [".."] * (depth - 1)
138+
slug_path = "/".join(slug_list[: i + 1])
139+
children = category.children.order_by(
140+
Case(
141+
When(type=Category.CategoryType.BACHELOR, then=Value(0)),
142+
When(type=Category.CategoryType.MASTER, then=Value(1)),
143+
When(
144+
type=Category.CategoryType.MASTER_SPECIALIZATION,
145+
then=Value(2),
146+
),
147+
default=Value(3),
148+
),
149+
"name",
150+
).all()
135151
columns.append(
136152
Column(
137153
category,
138-
list(
139-
category.children.order_by(
140-
Case(
141-
When(type=Category.CategoryType.BACHELOR, then=Value(0)),
142-
When(type=Category.CategoryType.MASTER, then=Value(1)),
143-
When(
144-
type=Category.CategoryType.MASTER_SPECIALIZATION,
145-
then=Value(2),
146-
),
147-
default=Value(3),
148-
),
149-
"name",
150-
).all()
151-
),
152-
depth,
153-
"/".join(path_list),
154+
[
155+
ChildCategory(
156+
child,
157+
reverse("catalog:finder", args=[f"{slug_path}/{child.slug}"]),
158+
)
159+
for child in children
160+
],
161+
len(categories) - i,
162+
reverse("catalog:finder", args=[slug_path]),
154163
)
155164
)
156165

0 commit comments

Comments
 (0)