11from django .db .models import Prefetch
22
3- from pythonpro .modules .models import Section as _Section , Module as _Module , Chapter as _Chapter
3+ from pythonpro .modules .models import ( Section as _Section , Module as _Module , Chapter as _Chapter , Topic as _Topic )
44
55
66def get_all_modules ():
@@ -11,7 +11,7 @@ def get_all_modules():
1111 return tuple (_Module .objects .order_by ('order' ))
1212
1313
14- def get_module_with_sections_and_chapters (slug ):
14+ def get_module_with_contents (slug ):
1515 """
1616 Search for a module with respective sections and chapters
1717 :param slug: module's slug
@@ -32,7 +32,7 @@ def get_module_with_sections_and_chapters(slug):
3232 ).get ()
3333
3434
35- def get_section_with_module_and_chapters (slug ):
35+ def get_section_with_contents (slug ):
3636 """
3737 Search for a section with respective module and chapters
3838 :param slug: section's slug
@@ -49,8 +49,24 @@ def get_section_with_module_and_chapters(slug):
4949
5050def get_chapter_with_contents (slug ):
5151 """
52- Search for a chapter respective to slug with it's module and section
52+ Search for a chapter respective to slug with it's module, section and topics
5353 :param slug: chapter's slug
5454 :return: Chapter
5555 """
56- return _Chapter .objects .filter (slug = slug ).select_related ('section' ).select_related ('section__module' ).get ()
56+ return _Chapter .objects .filter (slug = slug ).select_related ('section' ).select_related (
57+ 'section__module' ).prefetch_related (
58+ Prefetch (
59+ 'topic_set' ,
60+ queryset = _Topic .objects .order_by ('order' ),
61+ to_attr = 'topics'
62+ )).get ()
63+
64+
65+ def get_topic_with_contents (slug ):
66+ """
67+ Search for a topic respective to slug with it's module, section anc chapter
68+ :param slug: topic's slug
69+ :return: Topic
70+ """
71+ return _Topic .objects .filter (slug = slug ).select_related ('chapter' ).select_related ('chapter__section' ).select_related (
72+ 'chapter__section__module' ).get ()
0 commit comments