-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmodels.py
More file actions
130 lines (110 loc) · 4.12 KB
/
models.py
File metadata and controls
130 lines (110 loc) · 4.12 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
import datetime
from django.core.validators import MaxValueValidator, MinValueValidator
from django.db import models
from modelcluster.fields import ParentalKey
from wagtail.admin.panels import FieldPanel, InlinePanel, ObjectList, TabbedInterface
from wagtail.api import APIField
from wagtail.fields import RichTextField
from wagtail.search import index
from cms.composite.managers import CompositePageManager
from cms.dashboard.enums import (
DEFAULT_RELATED_LINKS_LAYOUT_FIELD_LENGTH,
RelatedLinksLayoutEnum,
)
from cms.dashboard.models import UKHSAPage, UKHSAPageRelatedLink
from cms.dynamic_content import help_texts
from cms.dynamic_content.access import ALLOWABLE_BODY_CONTENT_COMPOSITE
from cms.dynamic_content.announcements import Announcement
class CompositePage(UKHSAPage):
body = ALLOWABLE_BODY_CONTENT_COMPOSITE
page_description = RichTextField(
features=[],
blank=True,
null=True,
)
related_links_layout = models.CharField(
verbose_name="Layout",
help_text=help_texts.RELATED_LINKS_LAYOUT_FIELD,
default=RelatedLinksLayoutEnum.Sidebar.value,
max_length=DEFAULT_RELATED_LINKS_LAYOUT_FIELD_LENGTH,
choices=RelatedLinksLayoutEnum.choices(),
)
show_pagination = models.BooleanField(
default=True,
help_text=help_texts.SHOW_PAGINATION_FIELD,
)
pagination_size = models.IntegerField(
default=10,
help_text=help_texts.PAGINATION_SIZE_FIELD,
validators=[
MaxValueValidator(50),
MinValueValidator(5),
],
)
search_fields = UKHSAPage.search_fields + [
index.SearchField("page_description"),
]
content_panels = UKHSAPage.content_panels + [
FieldPanel("body"),
FieldPanel("page_description"),
FieldPanel("show_pagination"),
FieldPanel("pagination_size"),
]
sidebar_content_panels = [
FieldPanel("related_links_layout"),
InlinePanel("related_links", heading="Related links", label="Related link"),
]
# Sets which fields to expose on the API
api_fields = UKHSAPage.api_fields + [
APIField("body"),
APIField("related_links"),
APIField("search_description"),
APIField("related_links_layout"),
APIField("related_links"),
APIField("page_description"),
APIField("show_pagination"),
APIField("pagination_size"),
]
# Tabs to position at the top of the view
edit_handler = TabbedInterface(
[
ObjectList(content_panels, heading="Content"),
ObjectList(sidebar_content_panels, heading="Related Links"),
ObjectList(UKHSAPage.announcement_content_panels, heading="Announcements"),
ObjectList(UKHSAPage.promote_panels, heading="Promote"),
]
)
objects = CompositePageManager()
@property
def last_updated_at(self) -> datetime.datetime:
"""Takes the most recent update of this page and any of its children
Notes:
When calculating this timestamp,
this property considers the following:
- The latest content change of this page
- The latest released embargo across all children
- The latest content change across all children
Returns:
datetime object representing the last updated on the page
across all children as well.
"""
timestamps = [self.last_published_at]
child_pages = [page.specific for page in self.get_children()]
timestamps += [child_page.last_updated_at for child_page in child_pages]
timestamps = [timestamp for timestamp in timestamps if timestamp]
return max(timestamps)
class CompositeRelatedLink(UKHSAPageRelatedLink):
page = ParentalKey(
CompositePage,
on_delete=models.SET_NULL,
null=True,
related_name="related_links",
)
body = RichTextField(features=[], blank=True)
class CompositePageAnnouncement(Announcement):
page = ParentalKey(
CompositePage,
on_delete=models.SET_NULL,
null=True,
related_name="announcements",
)