forked from pythonprobr/pythonpro-website
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodels.py
More file actions
144 lines (103 loc) · 4.05 KB
/
models.py
File metadata and controls
144 lines (103 loc) · 4.05 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
from django.core.exceptions import ObjectDoesNotExist
from django.db import models
from django.urls import reverse
from ordered_model.models import OrderedModel
class _NoneCache:
pass
class Content(OrderedModel):
"""Mixing for defining content that can produce a breacrumb interface"""
title = models.CharField(max_length=50)
description = models.TextField()
slug = models.SlugField(unique=True)
_next_content_cache = _NoneCache
class Meta:
abstract = True
ordering = ('order',)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def parent(self):
"""Must return the parent of current content, which must be also implement Content or None"""
raise NotImplementedError()
def breadcrumb(self):
"""Must return an iterable with where which item is a tuple of (title, url) to content """
return gen_breadcrum(self)
def get_absolute_url(self):
"""Must return the absolute url for this content"""
raise NotImplementedError()
def __str__(self):
return self.title
def next_content(self):
if self._next_content_cache is not _NoneCache:
return self._next_content_cache
current = self
while current is not None:
try:
self._next_content_cache = current._next_content_query_set().get()
break
except ObjectDoesNotExist:
current = current.parent()
else:
self._next_content_cache = None
return self._next_content_cache
def _next_content_query_set(self):
"""Must provide a query set for next content"""
raise NotImplementedError()
class ContentWithTitleMixin(Content):
"""
Mising implementing breadcrumb method for models which has a title
"""
class Meta:
abstract = True
def breadcrumb(self):
return gen_breadcrum(self)
def gen_breadcrum(content):
"""Function that generates breadcrumb for contents that respect Content protocol"""
parent = content.parent()
if parent is not None:
yield from parent.breadcrumb()
yield content.title, content.get_absolute_url()
class Module(Content):
objective = models.TextField()
target = models.TextField()
def parent(self):
return None
def get_absolute_url(self):
"""Must return the absolute url for this content"""
return reverse('modules:detail', kwargs={'slug': self.slug})
def _next_content_query_set(self):
return Module.objects.filter(order=self.order + 1)
class Section(Content):
module = models.ForeignKey('Module', on_delete=models.CASCADE)
order_with_respect_to = 'module'
class Meta:
ordering = ['module', 'order']
def get_absolute_url(self):
return reverse('sections:detail', kwargs={'slug': self.slug})
def parent(self):
return self.module
def _next_content_query_set(self):
return Section.objects.filter(module=self.module, order=self.order + 1)
class Chapter(Content):
section = models.ForeignKey('Section', on_delete=models.CASCADE)
order_with_respect_to = 'section'
class Meta:
ordering = ['section', 'order']
def get_absolute_url(self):
return reverse('chapters:detail', kwargs={'slug': self.slug})
def parent(self):
return self.section
def _next_content_query_set(self):
return Chapter.objects.filter(section=self.section, order=self.order + 1)
class Topic(Content):
chapter = models.ForeignKey('Chapter', on_delete=models.CASCADE)
vimeo_id = models.CharField(max_length=11, db_index=False)
discourse_topic_id = models.CharField(max_length=11, db_index=False)
order_with_respect_to = 'chapter'
class Meta:
ordering = ['chapter', 'order']
def get_absolute_url(self):
return reverse('topics:detail', kwargs={'slug': self.slug})
def parent(self):
return self.chapter
def _next_content_query_set(self):
return Topic.objects.filter(chapter=self.chapter, order=self.order + 1)