Skip to content

Commit

Permalink
Merge pull request #242 from classy-python/elevate-models-out-of-temp…
Browse files Browse the repository at this point in the history
…late-context

Reduce dependency on models in templates
  • Loading branch information
meshy authored Jun 3, 2024
2 parents 31dd1c5 + f3f2594 commit 9b9ff33
Show file tree
Hide file tree
Showing 10 changed files with 320 additions and 328 deletions.
8 changes: 4 additions & 4 deletions cbv/templates/cbv/_klass_list.html
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<div class="span{{ column_width }}">
{% for obj in object_list %}
{% ifchanged obj.module.name %}
{% ifchanged obj.module_name %}
{% if not forloop.first %}</ul></div>{% endif %}
{% if obj.module.short_name == 'detail'%}</div><div class="span{{ column_width }}">{% endif %}
{% if obj.module_short_name == 'detail'%}</div><div class="span{{ column_width }}">{% endif %}
<div class="well skinny klass-list">
<ul class="nav nav-list">
<li class="nav-header"><h3>{{ obj.module.long_name }}</h3></li>
<li class="nav-header"><h3>{{ obj.module_long_name }}</h3></li>
{% endifchanged %}
<li class="{% if obj.is_secondary %}secondary{% else %}primary{% endif %}">
<a href="{{ obj.get_absolute_url }}" {% if obj.docstring %}class="klass-tooltip" data-original-title="{{ obj.docstring }}" data-placement="bottom"{% endif %}>
<a href="{{ obj.url }}" {% if obj.docstring %}class="klass-tooltip" data-original-title="{{ obj.docstring }}" data-placement="bottom"{% endif %}>
{{ obj.name }}
</a>
</li>
Expand Down
46 changes: 23 additions & 23 deletions cbv/templates/cbv/klass_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@


{% block nav %}
{% include "cbv/includes/nav.html" %}
{% include "cbv/includes/nav.html" with nav=nav version_switcher=version_switcher only %}
{% endblock nav %}


Expand Down Expand Up @@ -66,32 +66,32 @@ <h1><small>class</small>&nbsp;{{ klass.name }}</h1>
{% block content %}
<div class="span12">
<div class="row">
{% with klass.get_ancestors as direct_ancestors %}
{% for ancestor in all_ancestors %}
{% if forloop.first %}
<div class="span4">
<h2>Ancestors (<abbr title="Method Resolution Order">MRO</abbr>)</h2>
<ol start='0' id="ancestors">
{% if all_ancestors %}
<div class="span4">
<h2>Ancestors (<abbr title="Method Resolution Order">MRO</abbr>)</h2>
<ol start='0' id="ancestors">
<li><strong>{{ klass.name }}</strong></li>
{% endif %}
<li>
<a href="{{ ancestor.get_absolute_url }}" class="{% if ancestor in direct_ancestors %}direct{% endif %}">
{{ ancestor.name }}
</a>
</li>
{% if forloop.last %}</ol></div>{% endif %}
{% endfor %}
{% for ancestor in all_ancestors %}
<li>
<a href="{{ ancestor.url }}" class="{% if ancestor.is_direct %}direct{% endif %}">
{{ ancestor.name }}
</a>
</li>
{% endfor %}
</ol>
</div>
{% endif %}

{% for child in all_children %}
{% if forloop.first %}
<div id="descendants" class="span{% if direct_ancestors %}8{% else %}12{% endif %}">
{% if all_children %}
<div id="descendants" class="span{% if all_ancestors %}8{% else %}12{% endif %}">
<h2>Descendants</h2>
<ul class="unstyled">
{% endif %}
<li><a href="{{ child.get_absolute_url }}">{{ child.name }}</a></li>
{% if forloop.last %}</ul></div>{% endif %}
{% endfor %}
{% endwith %}
{% for child in all_children %}
<li><a href="{{ child.url }}">{{ child.name }}</a></li>
{% endfor %}
</ul>
</div>
{% endif %}
</div>

<div class="row">
Expand Down
2 changes: 1 addition & 1 deletion cbv/templates/cbv/module_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


{% block nav %}
{% include "cbv/includes/nav.html" %}
{% include "cbv/includes/nav.html" with nav=nav version_switcher=version_switcher only %}
{% endblock nav %}

{% block page_header %}<h1>{{ module_name }}</h1>{% endblock %}
Expand Down
4 changes: 2 additions & 2 deletions cbv/templates/cbv/version_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@


{% block nav %}
{% include "cbv/includes/nav.html" %}
{% include "cbv/includes/nav.html" with nav=nav version_switcher=version_switcher only %}
{% endblock %}


Expand All @@ -22,7 +22,7 @@
</div>
<h1>{{ project }}</h1>
<div class="row">
{% include 'cbv/_klass_list.html' with column_width=6 %}
{% include 'cbv/_klass_list.html' with column_width=6 object_list=object_list only %}
</div>
</div>
{% endblock %}
Expand Down
4 changes: 2 additions & 2 deletions cbv/templates/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{% load static %}

{% block nav %}
{% include "cbv/includes/nav.html" %}
{% include "cbv/includes/nav.html" with nav=nav version_switcher=version_switcher only %}
{% endblock %}


Expand Down Expand Up @@ -31,7 +31,7 @@ <h2>Did you know?</h2>
</div>
<h2>Start Here for {{ project }}.</h2>
<div class="row">
{% include 'cbv/_klass_list.html' with column_width=3 %}
{% include 'cbv/_klass_list.html' with column_width=3 object_list=object_list only %}
</div>
</div>
</div>
Expand Down
72 changes: 64 additions & 8 deletions cbv/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ def get_redirect_url(self, *, url_name: str, **kwargs):
class KlassDetailView(TemplateView):
template_name = "cbv/klass_detail.html"

@attrs.frozen
class Ancestor:
name: str
url: str
is_direct: bool

@attrs.frozen
class Child:
name: str
url: str

def get_context_data(self, **kwargs):
qs = Klass.objects.filter(
name__iexact=self.kwargs["klass"],
Expand All @@ -45,9 +56,25 @@ def get_context_data(self, **kwargs):
nav = nav_builder.get_nav_data(
klass.module.project_version, klass.module, klass
)
direct_ancestors = list(klass.get_ancestors())
ancestors = [
self.Ancestor(
name=ancestor.name,
url=ancestor.get_absolute_url(),
is_direct=ancestor in direct_ancestors,
)
for ancestor in klass.get_all_ancestors()
]
children = [
self.Child(
name=child.name,
url=child.get_absolute_url(),
)
for child in klass.get_all_children()
]
return {
"all_ancestors": list(klass.get_all_ancestors()),
"all_children": list(klass.get_all_children()),
"all_ancestors": ancestors,
"all_children": children,
"attributes": klass.get_prepared_attributes(),
"canonical_url": self.request.build_absolute_uri(canonical_url_path),
"klass": klass,
Expand Down Expand Up @@ -142,6 +169,17 @@ def get_context_data(self, **kwargs):
}


@attrs.frozen
class DjangoClassListItem:
docstring: str
is_secondary: bool
name: str
module_long_name: str
module_name: str
module_short_name: str
url: str


class VersionDetailView(TemplateView):
template_name = "cbv/version_detail.html"

Expand All @@ -157,11 +195,20 @@ def get_context_data(self, **kwargs):
nav = nav_builder.get_nav_data(project_version)
return {
"nav": nav,
"object_list": list(
Klass.objects.filter(
"object_list": [
DjangoClassListItem(
docstring=class_.docstring,
is_secondary=class_.is_secondary(),
name=class_.name,
module_long_name=class_.module.long_name,
module_name=class_.module.name,
module_short_name=class_.module.short_name,
url=class_.get_absolute_url(),
)
for class_ in Klass.objects.filter(
module__project_version=project_version
).select_related("module__project_version")
),
],
"project": f"Django {project_version.version_number}",
"version_switcher": version_switcher,
}
Expand All @@ -177,11 +224,20 @@ def get_context_data(self, **kwargs):
nav = nav_builder.get_nav_data(project_version)
return {
"nav": nav,
"object_list": list(
Klass.objects.filter(
"object_list": [
DjangoClassListItem(
docstring=class_.docstring,
is_secondary=class_.is_secondary(),
name=class_.name,
module_long_name=class_.module.long_name,
module_name=class_.module.name,
module_short_name=class_.module.short_name,
url=class_.get_absolute_url(),
)
for class_ in Klass.objects.filter(
module__project_version=project_version
).select_related("module__project_version")
),
],
"project": f"Django {project_version.version_number}",
"version_switcher": version_switcher,
}
Expand Down
128 changes: 56 additions & 72 deletions tests/_page_snapshots/fuzzy-klass-detail-old.html
Original file line number Diff line number Diff line change
Expand Up @@ -408,82 +408,66 @@ <h1><small>class</small>&nbsp;FormView</h1>
<div class="span12">
<div class="row">



<div class="span4">
<h2>Ancestors (<abbr title="Method Resolution Order">MRO</abbr>)</h2>
<ol start='0' id="ancestors">
<div class="span4">
<h2>Ancestors (<abbr title="Method Resolution Order">MRO</abbr>)</h2>
<ol start='0' id="ancestors">
<li><strong>FormView</strong></li>

<li>
<a href="/projects/Django/3.2/django.views.generic.base/TemplateResponseMixin/" class="direct">
TemplateResponseMixin
</a>
</li>



<li>
<a href="/projects/Django/3.2/django.views.generic.edit/BaseFormView/" class="direct">
BaseFormView
</a>
</li>



<li>
<a href="/projects/Django/3.2/django.views.generic.edit/FormMixin/" class="">
FormMixin
</a>
</li>



<li>
<a href="/projects/Django/3.2/django.views.generic.base/ContextMixin/" class="">
ContextMixin
</a>
</li>



<li>
<a href="/projects/Django/3.2/django.views.generic.edit/ProcessFormView/" class="">
ProcessFormView
</a>
</li>



<li>
<a href="/projects/Django/3.2/django.views.generic.base/View/" class="">
View
</a>
</li>
</ol></div>


<li>
<a href="/projects/Django/3.2/django.views.generic.base/TemplateResponseMixin/" class="direct">
TemplateResponseMixin
</a>
</li>

<li>
<a href="/projects/Django/3.2/django.views.generic.edit/BaseFormView/" class="direct">
BaseFormView
</a>
</li>

<li>
<a href="/projects/Django/3.2/django.views.generic.edit/FormMixin/" class="">
FormMixin
</a>
</li>

<li>
<a href="/projects/Django/3.2/django.views.generic.base/ContextMixin/" class="">
ContextMixin
</a>
</li>

<li>
<a href="/projects/Django/3.2/django.views.generic.edit/ProcessFormView/" class="">
ProcessFormView
</a>
</li>

<li>
<a href="/projects/Django/3.2/django.views.generic.base/View/" class="">
View
</a>
</li>

</ol>
</div>




<div id="descendants" class="span8">

<div id="descendants" class="span8">
<h2>Descendants</h2>
<ul class="unstyled">

<li><a href="/projects/Django/3.2/django.contrib.auth.views/LoginView/">LoginView</a></li>



<li><a href="/projects/Django/3.2/django.contrib.auth.views/PasswordChangeView/">PasswordChangeView</a></li>



<li><a href="/projects/Django/3.2/django.contrib.auth.views/PasswordResetConfirmView/">PasswordResetConfirmView</a></li>



<li><a href="/projects/Django/3.2/django.contrib.auth.views/PasswordResetView/">PasswordResetView</a></li>
</ul></div>


<li><a href="/projects/Django/3.2/django.contrib.auth.views/LoginView/">LoginView</a></li>

<li><a href="/projects/Django/3.2/django.contrib.auth.views/PasswordChangeView/">PasswordChangeView</a></li>

<li><a href="/projects/Django/3.2/django.contrib.auth.views/PasswordResetConfirmView/">PasswordResetConfirmView</a></li>

<li><a href="/projects/Django/3.2/django.contrib.auth.views/PasswordResetView/">PasswordResetView</a></li>

</ul>
</div>

</div>

Expand Down
Loading

0 comments on commit 9b9ff33

Please sign in to comment.