Skip to content

Commit 187a8b6

Browse files
committed
Remove models from ancestors context
Before this, we generated the list of ancestors based on a list of model instances. We now create a simple python object, and pass that into the template context instead.
1 parent 7488406 commit 187a8b6

2 files changed

Lines changed: 16 additions & 2 deletions

File tree

cbv/templates/cbv/klass_detail.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ <h2>Ancestors (<abbr title="Method Resolution Order">MRO</abbr>)</h2>
7373
<li><strong>{{ klass.name }}</strong></li>
7474
{% for ancestor in all_ancestors %}
7575
<li>
76-
<a href="{{ ancestor.get_absolute_url }}" class="{% if ancestor in direct_ancestors %}direct{% endif %}">
76+
<a href="{{ ancestor.url }}" class="{% if ancestor.is_direct %}direct{% endif %}">
7777
{{ ancestor.name }}
7878
</a>
7979
</li>

cbv/views.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ def get_redirect_url(self, *, url_name: str, **kwargs):
2121
class KlassDetailView(TemplateView):
2222
template_name = "cbv/klass_detail.html"
2323

24+
@attrs.frozen
25+
class Ancestor:
26+
name: str
27+
url: str
28+
is_direct: bool
29+
2430
def get_context_data(self, **kwargs):
2531
qs = Klass.objects.filter(
2632
name__iexact=self.kwargs["klass"],
@@ -46,8 +52,16 @@ def get_context_data(self, **kwargs):
4652
klass.module.project_version, klass.module, klass
4753
)
4854
direct_ancestors = list(klass.get_ancestors())
55+
ancestors = [
56+
self.Ancestor(
57+
name=ancestor.name,
58+
url=ancestor.get_absolute_url(),
59+
is_direct=ancestor in direct_ancestors,
60+
)
61+
for ancestor in klass.get_all_ancestors()
62+
]
4963
return {
50-
"all_ancestors": list(klass.get_all_ancestors()),
64+
"all_ancestors": ancestors,
5165
"all_children": list(klass.get_all_children()),
5266
"attributes": klass.get_prepared_attributes(),
5367
"canonical_url": self.request.build_absolute_uri(canonical_url_path),

0 commit comments

Comments
 (0)