Skip to content

Commit 50b96b8

Browse files
committed
Render class list from POPOs, not Models
Before this change, when rendering the `cbv/_klass_list.html` template, we passed in models and rendered the data from them. To put a layer between the models and the display logic, this first puts the required data onto a Plain Old Python Object, and then passes that into the templates.
1 parent f0bc8dd commit 50b96b8

2 files changed

Lines changed: 39 additions & 10 deletions

File tree

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/views.py

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,17 @@ def get_context_data(self, **kwargs):
142142
}
143143

144144

145+
@attrs.frozen
146+
class DjangoClassListItem:
147+
docstring: str
148+
is_secondary: bool
149+
name: str
150+
module_long_name: str
151+
module_name: str
152+
module_short_name: str
153+
url: str
154+
155+
145156
class VersionDetailView(TemplateView):
146157
template_name = "cbv/version_detail.html"
147158

@@ -157,11 +168,20 @@ def get_context_data(self, **kwargs):
157168
nav = nav_builder.get_nav_data(project_version)
158169
return {
159170
"nav": nav,
160-
"object_list": list(
161-
Klass.objects.filter(
171+
"object_list": [
172+
DjangoClassListItem(
173+
docstring=class_.docstring,
174+
is_secondary=class_.is_secondary(),
175+
name=class_.name,
176+
module_long_name=class_.module.long_name,
177+
module_name=class_.module.name,
178+
module_short_name=class_.module.short_name,
179+
url=class_.get_absolute_url(),
180+
)
181+
for class_ in Klass.objects.filter(
162182
module__project_version=project_version
163183
).select_related("module__project_version")
164-
),
184+
],
165185
"project": f"Django {project_version.version_number}",
166186
"version_switcher": version_switcher,
167187
}
@@ -177,11 +197,20 @@ def get_context_data(self, **kwargs):
177197
nav = nav_builder.get_nav_data(project_version)
178198
return {
179199
"nav": nav,
180-
"object_list": list(
181-
Klass.objects.filter(
200+
"object_list": [
201+
DjangoClassListItem(
202+
docstring=class_.docstring,
203+
is_secondary=class_.is_secondary(),
204+
name=class_.name,
205+
module_long_name=class_.module.long_name,
206+
module_name=class_.module.name,
207+
module_short_name=class_.module.short_name,
208+
url=class_.get_absolute_url(),
209+
)
210+
for class_ in Klass.objects.filter(
182211
module__project_version=project_version
183212
).select_related("module__project_version")
184-
),
213+
],
185214
"project": f"Django {project_version.version_number}",
186215
"version_switcher": version_switcher,
187216
}

0 commit comments

Comments
 (0)