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
203 lines (172 loc) · 6.17 KB
/
facade.py
File metadata and controls
203 lines (172 loc) · 6.17 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
from functools import partial as _partial
from django.conf import settings as _settings
from django.core.cache import cache as _cache
from django.db.models import Prefetch as _Prefetch
from django.urls import reverse
from pythonpro.modules.models import (
Chapter as _Chapter, Module as _Module, Section as _Section, Topic as _Topic,
)
__all__ = [
'get_topic_model',
'get_all_modules',
'get_module_with_contents',
'get_section_with_contents',
'get_chapter_with_contents',
'get_topic_with_contents',
'get_topic_with_contents_by_id',
'get_entire_content_forest',
'get_tree',
'topics_user_interacted_queryset',
'get_tree_by_module_slug',
'add_modules_purchase_link'
]
def get_topic_model():
return _Topic
def get_all_modules():
"""
Search all modules on database sorted by order
:return: tuple of Module
"""
lazy_all_modules = _partial(tuple, _Module.objects.order_by('order'))
return _cache.get_or_set('ALL_MODULES', lazy_all_modules, _settings.CACHE_TTL)
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').prefetch_related(
_Prefetch(
'topic_set',
queryset=_Topic.objects.order_by(
'order'),
to_attr='topics')
),
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').prefetch_related(
_Prefetch(
'topic_set',
queryset=_Topic.objects.order_by(
'order'),
to_attr='topics')
),
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'
)
def add_modules_purchase_link(modules):
"""
Add purchase link to modules
:param modules - a list of modules
:return modules - a list of modules with a purchase link
"""
purchase_links = {
'python-birds': reverse('core:lead_landing'),
'pytools': reverse('checkout:webdev_landing_page'),
'django': reverse('checkout:webdev_landing_page'),
'entrevistas-tecnicas': reverse('checkout:webdev_landing_page'),
'objetos-pythonicos': reverse('checkout:bootcamp_lp'),
'python-para-pythonistas': reverse('checkout:bootcamp_lp'),
'python-patterns': reverse('checkout:bootcamp_lp'),
}
for module in modules:
module.purchase_link = purchase_links[module.slug]
return modules
def get_tree_by_module_slug(module_slug: str):
module = _Module.objects.get(slug=module_slug)
module.sections = get_tree(module)
return module