-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmodels.py
326 lines (276 loc) · 8.96 KB
/
models.py
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
from django.db import models
from django.forms import CheckboxSelectMultiple
from django.utils import timezone
from wagtail.core.fields import StreamField, RichTextField
from wagtail.core.models import Page, Orderable
from wagtail.admin.edit_handlers import (
FieldPanel,
StreamFieldPanel,
PageChooserPanel,
InlinePanel,
MultiFieldPanel,
)
from wagtail.images.edit_handlers import ImageChooserPanel
from wagtail.search import index
from modelcluster.fields import ParentalKey, ParentalManyToManyField
from modelcluster.tags import ClusterTaggableManager
from taggit.models import TaggedItemBase
from .blocks import ButtonBlock
from .settings import get_grid_item_parent_page_types, get_grid_index_page_subpage_types
class GridItemTag(TaggedItemBase):
"""
Object to hold existing tags for Grid Items.
"""
content_object = ParentalKey(
"GridItem",
related_name="tagged_items",
on_delete=models.CASCADE,
)
class GridCategory(models.Model):
"""
Categories which a grid item can belong to. A grid item can belong to many
categories. Categories can be selected from the top of the grid index page.
"""
name = models.CharField(max_length=255)
def __str__(self):
return self.name
class Meta:
verbose_name_plural = "grid categories"
class GridItem(Page):
"""
The fields needed to properly display a grid item.
The template will omit any fields not included automagically.
"""
parent_page_types = get_grid_item_parent_page_types()
class Meta:
verbose_name = "Grid Item"
summary_image = models.ForeignKey(
"wagtailimages.Image",
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name="+",
)
summary_text = RichTextField(
"Summary",
default="",
help_text='The summary will appear in the item "card" view.',
)
description_image = models.ForeignKey(
"wagtailimages.Image",
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name="+",
help_text="This image will appear in the expanded area when populated.",
)
description_text = RichTextField(
"Full Description",
null=True,
blank=True,
help_text="This description will appear in the expanded area when populated.",
)
description_video = models.URLField(
null=True,
blank=True,
help_text="This video will be embedded in the expanded area when populated.",
)
landing_page_text = RichTextField(
"Landing Page Text",
null=True,
blank=True,
help_text="This is the text which will appear on the grid item's landing page.",
)
buttons = StreamField(ButtonBlock(), null=True)
tags = ClusterTaggableManager(through=GridItemTag, blank=True)
categories = ParentalManyToManyField("GridCategory", blank=True)
modified = models.DateTimeField("Page Modified", null=True)
search_fields = Page.search_fields + [
index.SearchField("summary_text"),
index.SearchField("description_text"),
index.SearchField("landing_page_text"),
]
CARD_PANELS = [
ImageChooserPanel("summary_image"),
FieldPanel("summary_text"),
]
DETAIL_PANELS = [
ImageChooserPanel("description_image"),
FieldPanel("description_text"),
FieldPanel("description_video"),
FieldPanel("landing_page_text"),
]
META_PANELS = [
FieldPanel("tags"),
FieldPanel("categories", widget=CheckboxSelectMultiple),
]
content_panels = Page.content_panels + [
MultiFieldPanel(
CARD_PANELS,
heading="Card Information",
classname="collapsible",
),
MultiFieldPanel(
DETAIL_PANELS,
heading="Expanded Description & Page Information",
classname="collapsible",
),
StreamFieldPanel(
"buttons",
),
MultiFieldPanel(
META_PANELS,
heading="Metadata",
classname="collapsible",
),
]
def save(self, *args, **kwargs):
self.modified = timezone.now()
super(GridItem, self).save(*args, **kwargs)
class GridIndexGridItemRelationship(Orderable, models.Model):
"""
Allows the content creator to associate Grid Items on a
Grid Index Page.
"""
grid_relationship = ParentalKey(
"GridIndexPage",
related_name="grid_index_grid_item_relationship",
on_delete=models.CASCADE,
)
grid_item = models.ForeignKey(
"GridItem",
related_name="+",
help_text="Add a grid item to the page",
verbose_name="Grid Items",
on_delete=models.CASCADE,
)
panels = [PageChooserPanel("grid_item")]
class GridIndexPageAbstract(models.Model):
"""
Index page for Grid Items.
This links the grid items to the categories and provides a page to display them on.
This abstract class exists to allow compositing the index page with other page
functionality by future users. GridIndexPage below makes this concrete.
"""
subpage_types = get_grid_index_page_subpage_types()
hero_background_image = models.ForeignKey(
"wagtailimages.Image",
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name="+",
help_text="The background image for the hero section. This triggers the "
"section to be displayed if an image is selected.",
)
hero_logo_image = models.ForeignKey(
"wagtailimages.Image",
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name="+",
help_text="The logo image to be displayed over the background image.",
)
hero_description = RichTextField(
null=True,
blank=True,
help_text="Text to be displayed beneath the logo over the background image.",
)
hero_button_text = models.CharField(
null=True,
blank=True,
max_length=255,
help_text="Text for the call-to-action button beneath the text and logo over "
"the background image.",
)
hero_button_url = models.CharField(
null=True,
blank=True,
max_length=255,
help_text="URL for the call-to-action button beneath the text and logo over "
"the background image.",
)
featured_description = RichTextField(
null=True,
blank=True,
help_text="Text to be displayed below the hero image next to the featured "
"items.",
)
featured_grid_item_1 = models.ForeignKey(
GridItem,
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name="+",
help_text="First featured grid item underneath the hero image.",
verbose_name="Featured Item One",
)
featured_grid_item_2 = models.ForeignKey(
GridItem,
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name="+",
help_text="Second featured grid item underneath the hero image.",
verbose_name="Featured Item Two,",
)
@property
def grid_items(self):
grid_items = [n.grid_item for n in self.grid_index_grid_item_relationship.all()]
return grid_items
@property
def categories(self):
grid_item_categories = (
GridIndexGridItemRelationship.objects.values_list(
"grid_item__categories__name"
)
.filter(
grid_relationship__id=self.id,
)
.order_by(
"grid_item__categories__name",
)
.distinct()
)
categories = []
for gic in grid_item_categories:
if gic[0] is not None:
categories.append(gic[0])
return categories
HERO_PANELS = [
ImageChooserPanel("hero_background_image"),
ImageChooserPanel("hero_logo_image"),
FieldPanel("hero_description"),
FieldPanel("hero_button_text"),
FieldPanel("hero_button_url"),
FieldPanel("featured_description"),
PageChooserPanel("featured_grid_item_1", GridItem),
PageChooserPanel("featured_grid_item_2", GridItem),
]
content_panels = Page.content_panels + [
MultiFieldPanel(
HERO_PANELS,
heading="Hero Section (Optional)",
classname="collapsible collapsed",
),
InlinePanel(
"grid_index_grid_item_relationship",
label="grid_items",
panels=None,
min_num=1,
),
]
search_fields = Page.search_fields + [
index.SearchField("hero_description"),
]
class Meta:
abstract = True
def __str__(self):
return "{0}".format(
self.grid_items,
)
class GridIndexPage(Page, GridIndexPageAbstract):
"""
Concrete implementation of GridIndexPageAbstract.
"""
class Meta:
verbose_name = "Grid Index Page"