Skip to content

Commit 56316a9

Browse files
renzonrenzon
authored andcommitted
Implemented complete content tree on module detail page
close #2270
1 parent 9108ff9 commit 56316a9

File tree

3 files changed

+51
-12
lines changed

3 files changed

+51
-12
lines changed

pythonpro/modules/facade.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from django.db.models import Prefetch as _Prefetch
66
from django.urls import reverse
77

8-
98
from pythonpro.modules.models import (
109
Chapter as _Chapter, Module as _Module, Section as _Section, Topic as _Topic,
1110
)
@@ -50,7 +49,13 @@ def get_module_with_contents(slug):
5049
queryset=_Section.objects.order_by('order').prefetch_related(
5150
_Prefetch(
5251
'chapter_set',
53-
queryset=_Chapter.objects.order_by('order'),
52+
queryset=_Chapter.objects.order_by('order').prefetch_related(
53+
_Prefetch(
54+
'topic_set',
55+
queryset=_Topic.objects.order_by(
56+
'order'),
57+
to_attr='topics')
58+
),
5459
to_attr='chapters'
5560
)
5661
),
@@ -141,13 +146,13 @@ def get_tree(module):
141146
sections = list(_Section.objects.filter(module=module).order_by('order').prefetch_related(
142147
_Prefetch(
143148
'chapter_set',
144-
queryset=_Chapter.objects.order_by(
145-
'order').prefetch_related(
149+
queryset=_Chapter.objects.order_by('order').prefetch_related(
146150
_Prefetch(
147151
'topic_set',
148152
queryset=_Topic.objects.order_by(
149153
'order'),
150-
to_attr='topics')),
154+
to_attr='topics')
155+
),
151156
to_attr='chapters')))
152157
module.sections = sections
153158
return sections

pythonpro/modules/templates/modules/module_detail.html

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ <h1 class="mt-4 mb-3">{{ module.title }}</h1>
1919
<dt>Duração: 4 semanas</dt>
2020
<dd>
2121
<ul>
22-
<li><a class="btn-success btn" href="{% url 'modules:enrol' slug=module.slug %}">Quero receber minhas metas por email &raquo;</a></li>
22+
<li><a class="btn-success btn" href="{% url 'modules:enrol' slug=module.slug %}">Quero
23+
receber minhas metas por email &raquo;</a></li>
2324
</ul>
2425
</dd>
2526
{% endif %}
@@ -44,15 +45,25 @@ <h1 class="mt-4 mb-3">{{ module.title }}</h1>
4445
<ol>
4546
{% for chapter in section.chapters %}
4647
<li><a href="{{ chapter.get_absolute_url }}">{{ chapter.title }}</a></li>
48+
<dd>
49+
<ol>
50+
{% for topic in chapter.topics %}
51+
<li><a href="{{ topic.get_absolute_url }}">{{ topic.title }}</a>
52+
</li>
53+
{% empty %}
54+
<li>Nenhum Capítulo definido ainda</li>
55+
{% endfor %}
56+
</ol>
57+
</dd>
4758
{% empty %}
4859
<li>Nenhum Capítulo definido ainda</li>
4960
{% endfor %}
50-
</ol>
61+
</ol>
5162
</dd>
5263
{% empty %}
5364
<li>Nenhuma seção definida ainda</li>
5465
{% endfor %}
55-
</ol>
66+
</ol>
5667
</dd>
5768
</div>
5869
</div>

pythonpro/modules/tests/test_module_detail_view.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from pythonpro.django_assertions import dj_assert_contains, dj_assert_not_contains, dj_assert_template_used
77
from pythonpro.modules import facade
8-
from pythonpro.modules.models import Chapter, Module, Section
8+
from pythonpro.modules.models import Chapter, Module, Section, Topic
99

1010

1111
def generate_resp(slug, client):
@@ -97,10 +97,10 @@ def python_birds(modules):
9797

9898
@pytest.fixture
9999
def resp_with_sections(client_with_lead, sections, python_birds):
100-
return _resp_with_sections(client_with_lead, sections, python_birds)
100+
return _resp_module_detail(client_with_lead, python_birds)
101101

102102

103-
def _resp_with_sections(client_with_lead, sections, python_birds):
103+
def _resp_module_detail(client_with_lead, python_birds):
104104
"""Plain function to avoid _pytest.warning_types.RemovedInPytest4Warning: Fixture "resp" called directly."""
105105
return client_with_lead.get(reverse('modules:detail', kwargs={'slug': python_birds.slug}))
106106

@@ -125,7 +125,7 @@ def chapters(sections):
125125

126126
@pytest.fixture
127127
def resp_with_chapters(client_with_lead, python_birds, sections, chapters):
128-
return _resp_with_sections(client_with_lead, sections, python_birds)
128+
return _resp_module_detail(client_with_lead, python_birds)
129129

130130

131131
def test_chapter_titles(resp_with_chapters, chapters):
@@ -143,3 +143,26 @@ def test_enrol_user_tags(python_birds, client_with_lead, mocker, logged_user):
143143
resp = client_with_lead.get(reverse('modules:enrol', kwargs={'slug': python_birds.slug}))
144144
tag_as.assert_called_once_with(logged_user.email, logged_user.id, python_birds.slug)
145145
assert resp.status_code == 200
146+
147+
148+
@pytest.fixture
149+
def topics(chapters):
150+
result = []
151+
for chapter in chapters:
152+
result.extend(baker.make(Topic, 2, chapter=chapter))
153+
return result
154+
155+
156+
@pytest.fixture
157+
def resp_with_topics(client_with_lead, python_birds, topics):
158+
return _resp_module_detail(client_with_lead, python_birds)
159+
160+
161+
def test_topic_titles(resp_with_topics, topics):
162+
for topic in topics:
163+
dj_assert_contains(resp_with_topics, topic.title)
164+
165+
166+
def test_topic_urls(resp_with_topics, topics):
167+
for topic in topics:
168+
dj_assert_contains(resp_with_topics, topic.get_absolute_url())

0 commit comments

Comments
 (0)