forked from pythonprobr/pythonpro-website
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfacade.py
More file actions
72 lines (60 loc) · 2.14 KB
/
facade.py
File metadata and controls
72 lines (60 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
from django.db.models import Prefetch
from pythonpro.modules.models import (Section as _Section, Module as _Module, Chapter as _Chapter, Topic as _Topic)
def get_all_modules():
"""
Search all modules on database sorted by order
:return: tuple of Module
"""
return tuple(_Module.objects.order_by('order'))
def get_module_with_contents(slug):
"""
Search for a module with respective sections and chapters
:param slug: module's slug
:return: Module with respective section on attribute sections
"""
return _Module.objects.filter(slug=slug).prefetch_related(
Prefetch(
'section_set',
queryset=_Section.objects.order_by('order').prefetch_related(
Prefetch(
'chapter_set',
queryset=_Chapter.objects.order_by('order'),
to_attr='chapters'
)
),
to_attr='sections')
).get()
def get_section_with_contents(slug):
"""
Search for a section with respective module and chapters
:param slug: section's slug
:return: Section
"""
return _Section.objects.filter(slug=slug).select_related('module').prefetch_related(
Prefetch(
'chapter_set',
queryset=_Chapter.objects.order_by('order'),
to_attr='chapters'
)
).get()
def get_chapter_with_contents(slug):
"""
Search for a chapter respective to slug with it's module, section and topics
:param slug: chapter's slug
:return: Chapter
"""
return _Chapter.objects.filter(slug=slug).select_related('section').select_related(
'section__module').prefetch_related(
Prefetch(
'topic_set',
queryset=_Topic.objects.order_by('order'),
to_attr='topics'
)).get()
def get_topic_with_contents(slug):
"""
Search for a topic respective to slug with it's module, section anc chapter
:param slug: topic's slug
:return: Topic
"""
return _Topic.objects.filter(slug=slug).select_related('chapter').select_related('chapter__section').select_related(
'chapter__section__module').get()