Skip to content

Commit 9b9ff33

Browse files
authored
Merge pull request #242 from classy-python/elevate-models-out-of-template-context
Reduce dependency on models in templates
2 parents 31dd1c5 + f3f2594 commit 9b9ff33

10 files changed

Lines changed: 320 additions & 328 deletions

cbv/templates/cbv/_klass_list.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
<div class="span{{ column_width }}">
22
{% for obj in object_list %}
3-
{% ifchanged obj.module.name %}
3+
{% ifchanged obj.module_name %}
44
{% if not forloop.first %}</ul></div>{% endif %}
5-
{% if obj.module.short_name == 'detail'%}</div><div class="span{{ column_width }}">{% endif %}
5+
{% if obj.module_short_name == 'detail'%}</div><div class="span{{ column_width }}">{% endif %}
66
<div class="well skinny klass-list">
77
<ul class="nav nav-list">
8-
<li class="nav-header"><h3>{{ obj.module.long_name }}</h3></li>
8+
<li class="nav-header"><h3>{{ obj.module_long_name }}</h3></li>
99
{% endifchanged %}
1010
<li class="{% if obj.is_secondary %}secondary{% else %}primary{% endif %}">
11-
<a href="{{ obj.get_absolute_url }}" {% if obj.docstring %}class="klass-tooltip" data-original-title="{{ obj.docstring }}" data-placement="bottom"{% endif %}>
11+
<a href="{{ obj.url }}" {% if obj.docstring %}class="klass-tooltip" data-original-title="{{ obj.docstring }}" data-placement="bottom"{% endif %}>
1212
{{ obj.name }}
1313
</a>
1414
</li>

cbv/templates/cbv/klass_detail.html

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636

3737
{% block nav %}
38-
{% include "cbv/includes/nav.html" %}
38+
{% include "cbv/includes/nav.html" with nav=nav version_switcher=version_switcher only %}
3939
{% endblock nav %}
4040

4141

@@ -66,32 +66,32 @@ <h1><small>class</small>&nbsp;{{ klass.name }}</h1>
6666
{% block content %}
6767
<div class="span12">
6868
<div class="row">
69-
{% with klass.get_ancestors as direct_ancestors %}
70-
{% for ancestor in all_ancestors %}
71-
{% if forloop.first %}
72-
<div class="span4">
73-
<h2>Ancestors (<abbr title="Method Resolution Order">MRO</abbr>)</h2>
74-
<ol start='0' id="ancestors">
69+
{% if all_ancestors %}
70+
<div class="span4">
71+
<h2>Ancestors (<abbr title="Method Resolution Order">MRO</abbr>)</h2>
72+
<ol start='0' id="ancestors">
7573
<li><strong>{{ klass.name }}</strong></li>
76-
{% endif %}
77-
<li>
78-
<a href="{{ ancestor.get_absolute_url }}" class="{% if ancestor in direct_ancestors %}direct{% endif %}">
79-
{{ ancestor.name }}
80-
</a>
81-
</li>
82-
{% if forloop.last %}</ol></div>{% endif %}
83-
{% endfor %}
74+
{% for ancestor in all_ancestors %}
75+
<li>
76+
<a href="{{ ancestor.url }}" class="{% if ancestor.is_direct %}direct{% endif %}">
77+
{{ ancestor.name }}
78+
</a>
79+
</li>
80+
{% endfor %}
81+
</ol>
82+
</div>
83+
{% endif %}
8484

85-
{% for child in all_children %}
86-
{% if forloop.first %}
87-
<div id="descendants" class="span{% if direct_ancestors %}8{% else %}12{% endif %}">
85+
{% if all_children %}
86+
<div id="descendants" class="span{% if all_ancestors %}8{% else %}12{% endif %}">
8887
<h2>Descendants</h2>
8988
<ul class="unstyled">
90-
{% endif %}
91-
<li><a href="{{ child.get_absolute_url }}">{{ child.name }}</a></li>
92-
{% if forloop.last %}</ul></div>{% endif %}
93-
{% endfor %}
94-
{% endwith %}
89+
{% for child in all_children %}
90+
<li><a href="{{ child.url }}">{{ child.name }}</a></li>
91+
{% endfor %}
92+
</ul>
93+
</div>
94+
{% endif %}
9595
</div>
9696

9797
<div class="row">

cbv/templates/cbv/module_detail.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66

77
{% block nav %}
8-
{% include "cbv/includes/nav.html" %}
8+
{% include "cbv/includes/nav.html" with nav=nav version_switcher=version_switcher only %}
99
{% endblock nav %}
1010

1111
{% block page_header %}<h1>{{ module_name }}</h1>{% endblock %}

cbv/templates/cbv/version_detail.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212

1313
{% block nav %}
14-
{% include "cbv/includes/nav.html" %}
14+
{% include "cbv/includes/nav.html" with nav=nav version_switcher=version_switcher only %}
1515
{% endblock %}
1616

1717

@@ -22,7 +22,7 @@
2222
</div>
2323
<h1>{{ project }}</h1>
2424
<div class="row">
25-
{% include 'cbv/_klass_list.html' with column_width=6 %}
25+
{% include 'cbv/_klass_list.html' with column_width=6 object_list=object_list only %}
2626
</div>
2727
</div>
2828
{% endblock %}

cbv/templates/home.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
{% load static %}
44

55
{% block nav %}
6-
{% include "cbv/includes/nav.html" %}
6+
{% include "cbv/includes/nav.html" with nav=nav version_switcher=version_switcher only %}
77
{% endblock %}
88

99

@@ -31,7 +31,7 @@ <h2>Did you know?</h2>
3131
</div>
3232
<h2>Start Here for {{ project }}.</h2>
3333
<div class="row">
34-
{% include 'cbv/_klass_list.html' with column_width=3 %}
34+
{% include 'cbv/_klass_list.html' with column_width=3 object_list=object_list only %}
3535
</div>
3636
</div>
3737
</div>

cbv/views.py

Lines changed: 64 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ 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+
30+
@attrs.frozen
31+
class Child:
32+
name: str
33+
url: str
34+
2435
def get_context_data(self, **kwargs):
2536
qs = Klass.objects.filter(
2637
name__iexact=self.kwargs["klass"],
@@ -45,9 +56,25 @@ def get_context_data(self, **kwargs):
4556
nav = nav_builder.get_nav_data(
4657
klass.module.project_version, klass.module, klass
4758
)
59+
direct_ancestors = list(klass.get_ancestors())
60+
ancestors = [
61+
self.Ancestor(
62+
name=ancestor.name,
63+
url=ancestor.get_absolute_url(),
64+
is_direct=ancestor in direct_ancestors,
65+
)
66+
for ancestor in klass.get_all_ancestors()
67+
]
68+
children = [
69+
self.Child(
70+
name=child.name,
71+
url=child.get_absolute_url(),
72+
)
73+
for child in klass.get_all_children()
74+
]
4875
return {
49-
"all_ancestors": list(klass.get_all_ancestors()),
50-
"all_children": list(klass.get_all_children()),
76+
"all_ancestors": ancestors,
77+
"all_children": children,
5178
"attributes": klass.get_prepared_attributes(),
5279
"canonical_url": self.request.build_absolute_uri(canonical_url_path),
5380
"klass": klass,
@@ -142,6 +169,17 @@ def get_context_data(self, **kwargs):
142169
}
143170

144171

172+
@attrs.frozen
173+
class DjangoClassListItem:
174+
docstring: str
175+
is_secondary: bool
176+
name: str
177+
module_long_name: str
178+
module_name: str
179+
module_short_name: str
180+
url: str
181+
182+
145183
class VersionDetailView(TemplateView):
146184
template_name = "cbv/version_detail.html"
147185

@@ -157,11 +195,20 @@ def get_context_data(self, **kwargs):
157195
nav = nav_builder.get_nav_data(project_version)
158196
return {
159197
"nav": nav,
160-
"object_list": list(
161-
Klass.objects.filter(
198+
"object_list": [
199+
DjangoClassListItem(
200+
docstring=class_.docstring,
201+
is_secondary=class_.is_secondary(),
202+
name=class_.name,
203+
module_long_name=class_.module.long_name,
204+
module_name=class_.module.name,
205+
module_short_name=class_.module.short_name,
206+
url=class_.get_absolute_url(),
207+
)
208+
for class_ in Klass.objects.filter(
162209
module__project_version=project_version
163210
).select_related("module__project_version")
164-
),
211+
],
165212
"project": f"Django {project_version.version_number}",
166213
"version_switcher": version_switcher,
167214
}
@@ -177,11 +224,20 @@ def get_context_data(self, **kwargs):
177224
nav = nav_builder.get_nav_data(project_version)
178225
return {
179226
"nav": nav,
180-
"object_list": list(
181-
Klass.objects.filter(
227+
"object_list": [
228+
DjangoClassListItem(
229+
docstring=class_.docstring,
230+
is_secondary=class_.is_secondary(),
231+
name=class_.name,
232+
module_long_name=class_.module.long_name,
233+
module_name=class_.module.name,
234+
module_short_name=class_.module.short_name,
235+
url=class_.get_absolute_url(),
236+
)
237+
for class_ in Klass.objects.filter(
182238
module__project_version=project_version
183239
).select_related("module__project_version")
184-
),
240+
],
185241
"project": f"Django {project_version.version_number}",
186242
"version_switcher": version_switcher,
187243
}

tests/_page_snapshots/fuzzy-klass-detail-old.html

Lines changed: 56 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -408,82 +408,66 @@ <h1><small>class</small>&nbsp;FormView</h1>
408408
<div class="span12">
409409
<div class="row">
410410

411-
412-
413-
<div class="span4">
414-
<h2>Ancestors (<abbr title="Method Resolution Order">MRO</abbr>)</h2>
415-
<ol start='0' id="ancestors">
411+
<div class="span4">
412+
<h2>Ancestors (<abbr title="Method Resolution Order">MRO</abbr>)</h2>
413+
<ol start='0' id="ancestors">
416414
<li><strong>FormView</strong></li>
417-
418-
<li>
419-
<a href="/projects/Django/3.2/django.views.generic.base/TemplateResponseMixin/" class="direct">
420-
TemplateResponseMixin
421-
</a>
422-
</li>
423-
424-
425-
426-
<li>
427-
<a href="/projects/Django/3.2/django.views.generic.edit/BaseFormView/" class="direct">
428-
BaseFormView
429-
</a>
430-
</li>
431-
432-
433-
434-
<li>
435-
<a href="/projects/Django/3.2/django.views.generic.edit/FormMixin/" class="">
436-
FormMixin
437-
</a>
438-
</li>
439-
440-
441-
442-
<li>
443-
<a href="/projects/Django/3.2/django.views.generic.base/ContextMixin/" class="">
444-
ContextMixin
445-
</a>
446-
</li>
447-
448-
449-
450-
<li>
451-
<a href="/projects/Django/3.2/django.views.generic.edit/ProcessFormView/" class="">
452-
ProcessFormView
453-
</a>
454-
</li>
455-
456-
457-
458-
<li>
459-
<a href="/projects/Django/3.2/django.views.generic.base/View/" class="">
460-
View
461-
</a>
462-
</li>
463-
</ol></div>
464-
415+
416+
<li>
417+
<a href="/projects/Django/3.2/django.views.generic.base/TemplateResponseMixin/" class="direct">
418+
TemplateResponseMixin
419+
</a>
420+
</li>
421+
422+
<li>
423+
<a href="/projects/Django/3.2/django.views.generic.edit/BaseFormView/" class="direct">
424+
BaseFormView
425+
</a>
426+
</li>
427+
428+
<li>
429+
<a href="/projects/Django/3.2/django.views.generic.edit/FormMixin/" class="">
430+
FormMixin
431+
</a>
432+
</li>
433+
434+
<li>
435+
<a href="/projects/Django/3.2/django.views.generic.base/ContextMixin/" class="">
436+
ContextMixin
437+
</a>
438+
</li>
439+
440+
<li>
441+
<a href="/projects/Django/3.2/django.views.generic.edit/ProcessFormView/" class="">
442+
ProcessFormView
443+
</a>
444+
</li>
445+
446+
<li>
447+
<a href="/projects/Django/3.2/django.views.generic.base/View/" class="">
448+
View
449+
</a>
450+
</li>
451+
452+
</ol>
453+
</div>
454+
465455

466-
467-
468-
<div id="descendants" class="span8">
456+
457+
<div id="descendants" class="span8">
469458
<h2>Descendants</h2>
470459
<ul class="unstyled">
471-
472-
<li><a href="/projects/Django/3.2/django.contrib.auth.views/LoginView/">LoginView</a></li>
473-
474-
475-
476-
<li><a href="/projects/Django/3.2/django.contrib.auth.views/PasswordChangeView/">PasswordChangeView</a></li>
477-
478-
479-
480-
<li><a href="/projects/Django/3.2/django.contrib.auth.views/PasswordResetConfirmView/">PasswordResetConfirmView</a></li>
481-
482-
483-
484-
<li><a href="/projects/Django/3.2/django.contrib.auth.views/PasswordResetView/">PasswordResetView</a></li>
485-
</ul></div>
486-
460+
461+
<li><a href="/projects/Django/3.2/django.contrib.auth.views/LoginView/">LoginView</a></li>
462+
463+
<li><a href="/projects/Django/3.2/django.contrib.auth.views/PasswordChangeView/">PasswordChangeView</a></li>
464+
465+
<li><a href="/projects/Django/3.2/django.contrib.auth.views/PasswordResetConfirmView/">PasswordResetConfirmView</a></li>
466+
467+
<li><a href="/projects/Django/3.2/django.contrib.auth.views/PasswordResetView/">PasswordResetView</a></li>
468+
469+
</ul>
470+
</div>
487471

488472
</div>
489473

0 commit comments

Comments
 (0)