-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy pathfacade.py
More file actions
88 lines (67 loc) · 2.24 KB
/
facade.py
File metadata and controls
88 lines (67 loc) · 2.24 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
from functools import partial
from django.conf import settings
from django.core.cache import cache
from django.db.models import Prefetch as _Prefetch
from django.urls import reverse
from pythonpro.cohorts.models import Cohort as _Cohort, CohortStudent, LiveClass as _LiveClass, Webinar as _Webinar
__all__ = [
'get_all_cohorts_desc',
'find_cohort',
'find_most_recent_cohort',
'calculate_most_recent_cohort_path',
'find_webinars',
'find_webinar',
'find_live_class',
]
def get_all_cohorts_desc():
lazy_all_cohorts = partial(tuple, _Cohort.objects.order_by('-start'))
return cache.get_or_set('ALL_COHORTS', lazy_all_cohorts, settings.CACHE_TTL)
def find_cohort(slug):
return _Cohort.objects.filter(slug=slug).prefetch_related(
_Prefetch(
'liveclass_set',
queryset=_LiveClass.objects.order_by('start'),
to_attr='classes'
)
).prefetch_related(
_Prefetch(
'webinar_set',
queryset=_Webinar.objects.order_by('start'),
to_attr='webinars'
)
).get()
def find_most_recent_cohort():
return _Cohort.objects.order_by('-start').first()
def calculate_most_recent_cohort_path() -> str:
slug_dct = _Cohort.objects.order_by('-start').values('slug').first()
return reverse('modules:detail', kwargs=slug_dct)
def find_webinars():
"""
Retrieve Webinars from database ordered by date desc
:return: Tuple of webinars
"""
return tuple(_Webinar.objects.order_by('-start'))
def find_recorded_webinars():
"""
Retrieve recorded Webinars from database ordered by date desc.
A recorded Webinar has vimeo_id not empty
:return: Tuple of webinars
"""
return tuple(_Webinar.objects.order_by('-start').exclude(vimeo_id__exact=''))
def find_webinar(slug):
"""
Retrieve Webinar by its slug
:return: Webinar
"""
return _Webinar.objects.filter(slug=slug).get()
def find_live_class(pk):
"""
Find Live Class by its PK, selecting related cohort
:param pk:
:return:
"""
return _LiveClass.objects.select_related('cohort').get(pk=pk)
def subscribe_to_last_cohort(user):
ch = CohortStudent(user=user, cohort=find_most_recent_cohort())
ch.save()
return ch