Skip to content

Commit 7db1f3b

Browse files
committed
Nicer FBV support
1 parent eec92d1 commit 7db1f3b

File tree

4 files changed

+86
-10
lines changed

4 files changed

+86
-10
lines changed

docs/templates/view_artist4.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{% extends "iommi/base.html" %}
2+
3+
{% block content %}
4+
<h1>{{ artist }}</h1>
5+
6+
<ul>
7+
{% for album in artist.albums.all %}
8+
<li>
9+
{{ album }}
10+
</li>
11+
{% endfor %}
12+
</ul>
13+
{% endblock %}

docs/test_doc_legacy_fbv.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
Track,
1010
)
1111
from iommi import (
12+
middleware,
1213
Page,
1314
Table,
1415
)
1516
from iommi.docs import show_output
17+
from iommi.main_menu import main_menu_middleware
1618
from tests.helpers import (
1719
call_view_through_middleware,
1820
req,
@@ -198,3 +200,45 @@ class MyPage(Page):
198200
assert black_sabbath.name not in response.content.decode()
199201
assert album.name in response.content.decode()
200202
# @end
203+
204+
205+
def test_legacy_fbv_with_main_menu_and_basic_html(settings, black_sabbath, medium_discography):
206+
# language=rst
207+
"""
208+
Using iommi's base template with a FBV
209+
======================================
210+
211+
For existing FBVs, you can get the iommi `MainMenu` and the base style
212+
assets by simply extending `iommi/base.html`:
213+
214+
"""
215+
# @test
216+
settings.DEBUG = True
217+
settings.IOMMI_MAIN_MENU = 'iommi.main_menu__tests.menu_declaration'
218+
settings.ROOT_URLCONF = 'iommi.main_menu__tests'
219+
assert 'iommi.main_menu.main_menu_middleware' in settings.MIDDLEWARE
220+
# @end
221+
222+
def view_artist(request, artist_name):
223+
artist = get_object_or_404(Artist, name=artist_name)
224+
return render(
225+
request,
226+
'view_artist4.html',
227+
context={
228+
'artist': artist,
229+
}
230+
)
231+
232+
# language=rst
233+
"""
234+
.. literalinclude:: templates/view_artist4.html
235+
:language: jinja
236+
"""
237+
238+
# @test
239+
foo = main_menu_middleware(get_response=lambda request: view_artist(request, artist_name=black_sabbath.name))
240+
bar = middleware(get_response=foo)
241+
response = bar(req('get', url='/artists/'))
242+
show_output(response)
243+
assert black_sabbath.name in response.content.decode()
244+
# @end

iommi/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
register_cell_formatter,
5656
register_column_factory,
5757
)
58+
from iommi.thread_locals import get_current_request
5859

5960
setup_db_compat()
6061

@@ -97,6 +98,8 @@ def process_view(self, request, view_func, view_args, view_kwargs):
9798
request.iommi_not_atomic_for = getattr(view_func, '_non_atomic_requests', set())
9899

99100
def __call__(self, request):
101+
# For plain FBVs that want to extend iommi/base.html, we need to insert some assets
102+
request.iommi_fallback_assets = lambda: Page().bind(request=get_current_request()).iommi_collected_assets()
100103
response = self.get_response(request)
101104

102105
if isinstance(response, Part):

iommi/templates/iommi/base.html

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,19 @@
33
<head>
44
<title>{% block title %}{% if title %}{{ title }}{% endif %}{% endblock %}</title>
55
{% block iommi_head_contents %}{% endblock %}
6-
{% for asset in assets.values %}
7-
{% if not asset.in_body %}
8-
{{ asset }}
9-
{% endif %}
10-
{% endfor %}
6+
{% if assets %}
7+
{% for asset in assets.values %}
8+
{% if not asset.in_body %}
9+
{{ asset }}
10+
{% endif %}
11+
{% endfor %}
12+
{% elif request.iommi_fallback_assets %}
13+
{% for asset in request.iommi_fallback_assets.values %}
14+
{% if not asset.in_body %}
15+
{{ asset }}
16+
{% endif %}
17+
{% endfor %}
18+
{% endif %}
1119
{% block iommi_head_contents_last %}{% endblock %}
1220
</head>
1321
<body{% block iommi_body_tag_attrs %}{% if request.iommi_main_menu %} class="sidebar_menu_active"{% endif %}{% endblock %}>
@@ -25,10 +33,18 @@
2533

2634
{% block iommi_bottom %}{% endblock %}
2735

28-
{% for asset in assets.values %}
29-
{% if asset.in_body %}
30-
{{ asset }}
31-
{% endif %}
32-
{% endfor %}
36+
{% if assets %}
37+
{% for asset in assets.values %}
38+
{% if asset.in_body %}
39+
{{ asset }}
40+
{% endif %}
41+
{% endfor %}
42+
{% elif request.iommi_fallback_assets %}
43+
{% for asset in request.iommi_fallback_assets.values %}
44+
{% if asset.in_body %}
45+
{{ asset }}
46+
{% endif %}
47+
{% endfor %}
48+
{% endif %}
3349
</body>
3450
</html>

0 commit comments

Comments
 (0)