Repository navigation
Expand file tree
/
Copy pathmodels.py
More file actions
71 lines (50 loc) · 1.87 KB
/
Copy pathmodels.py
File metadata and controls
71 lines (50 loc) · 1.87 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
from django.db import models
from django.urls import reverse
from ordered_model.models import OrderedModel
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)
class Meta:
abstract = True
ordering = ('order',)
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"""
return reverse('modules:detail', kwargs={'slug': self.slug})
def __str__(self):
return self.title
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
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