forked from pythonprobr/pythonpro-website
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfacade.py
More file actions
141 lines (119 loc) · 4.14 KB
/
facade.py
File metadata and controls
141 lines (119 loc) · 4.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
from django.db.models import Prefetch
from pythonpro.modules.models import (
Chapter as _Chapter, Module as _Module, Section as _Section, Topic as _Topic,
)
def get_topic_model():
return _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 and 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()
def get_topic_with_contents_by_id(id: int) -> _Topic:
"""
Search for a topic respective to slug with it's module, section and chapter
:param id: topic's id
:return: Topic
"""
return _Topic.objects.filter(id=id).select_related('chapter').select_related('chapter__section').select_related(
'chapter__section__module').get()
def get_entire_content_forest():
"""
Return a list of modules with the entire content on it
:return:
"""
return list(_Module.objects.all().prefetch_related(
Prefetch(
'section_set',
queryset=_Section.objects.order_by('order').prefetch_related(
Prefetch(
'chapter_set',
queryset=_Chapter.objects.order_by('order').prefetch_related(
Prefetch(
'topic_set',
queryset=_Topic.objects.order_by('order'),
to_attr='topics'
)
),
to_attr='chapters'
)
),
to_attr='sections')
))
def get_tree(module):
"""
Return a list of modules with the entire content on it
:return:
"""
sections = list(_Section.objects.filter(module=module).order_by('order').prefetch_related(
Prefetch(
'chapter_set',
queryset=_Chapter.objects.order_by(
'order').prefetch_related(
Prefetch(
'topic_set',
queryset=_Topic.objects.order_by(
'order'),
to_attr='topics')),
to_attr='chapters')))
module.sections = sections
return sections
def topics_user_interacted_queryset(user):
return _Topic.objects.filter(
topicinteraction__user=user
).select_related('chapter').select_related('chapter__section').select_related(
'chapter__section__module'
)