Files @ 6121ce79cc88
Branch filter:

Location: symposion_app/cms_pages/models.py

Nick Seidenman (N6)
Getting the splash homepage to work via CMS.

Nearly there. Much rubbish / cruft ripped out of site_base.html and content_page.html.
Had to modify cms_pages/models.py to accommodate a "simple" layout option in the homepage
editor.
Still some stuff to do. Styling not quite there yet, but want to checkpoint this.
  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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
from __future__ import unicode_literals

from django import forms
from django.core.exceptions import ValidationError
from django.db import models
from django.db.models.signals import pre_delete
from django.dispatch import receiver
from django.forms.utils import ErrorList
from django.http import Http404
from django.shortcuts import render
from django.utils.encoding import python_2_unicode_compatible

from modelcluster.fields import ParentalKey

from wagtail.wagtailadmin.edit_handlers import InlinePanel
from wagtail.wagtailadmin.edit_handlers import FieldPanel
from wagtail.wagtailadmin.edit_handlers import PageChooserPanel
from wagtail.wagtailadmin.edit_handlers import StreamFieldPanel

from wagtail.wagtailcore import blocks
from wagtail.wagtailcore.models import Page
from wagtail.wagtailcore.models import Orderable
from wagtail.wagtailcore.fields import RichTextField
from wagtail.wagtailcore.fields import StreamField
from wagtail.wagtailcore.url_routing import RouteResult

from wagtail.wagtailimages import blocks as imageblocks
from wagtail.wagtailimages.edit_handlers import ImageChooserPanel
from wagtail.wagtailimages.models import AbstractImage
from wagtail.wagtailimages.models import AbstractRendition
from wagtail.wagtailimages.models import Image

from wagtail.wagtailsearch import index
from wagtail.wagtailsnippets.models import register_snippet


from symposion import schedule

ILLUSTRATION_ANTARCTICA = "antarctica.svg"
ILLUSTRATION_BRIDGE = "bridge.svg"
ILLUSTRATION_CASINO = "casino.svg"
ILLUSTRATION_CRADLE = "cradle.svg"
ILLUSTRATION_DEVIL = "devil.svg"
ILLUSTRATION_FALLS = "falls.svg"
ILLUSTRATION_HOBART = "hobart.svg"
ILLUSTRATION_LAVENDER = "lavender.svg"
ILLUSTRATION_PYCONAU = "logo-mel.png"
ILLUSTRATION_PYTHON = "Python-logo-notext.svg"
ILLUSTRATION_TUZ = "tuz.svg"
ILLUSTRATION_WINEGLASS = "wineglass.svg"

ILLUSTRATION_TYPES = (
    (ILLUSTRATION_ANTARCTICA, "Antarctica"),
    (ILLUSTRATION_BRIDGE, "Bridge"),
    (ILLUSTRATION_CASINO, "Casino"),
    (ILLUSTRATION_CRADLE, "Cradle Mountain"),
    (ILLUSTRATION_DEVIL, "Tasmanian Devil"),
    (ILLUSTRATION_FALLS, "Waterfall"),
    (ILLUSTRATION_HOBART, "Hobart"),
    (ILLUSTRATION_LAVENDER, "Lavender"),
    (ILLUSTRATION_PYCONAU, "Pycon AU"),
    (ILLUSTRATION_PYTHON, "Python"),
    (ILLUSTRATION_TUZ, "Tuz"),
    (ILLUSTRATION_WINEGLASS, "Wineglass"),
)


class ExternalLinksBlock(blocks.StructBlock):

    class Meta:
        template = "cms_pages/home_page_blocks/external_link.html"

    EXTERNAL_LINK_TWITTER = "twitter"
    EXTERNAL_LINK_FACEBOOK = "facebook"
    EXTERNAL_LINK_GENERIC = "generic"

    EXTERNAL_LINK_TYPES = (
        (EXTERNAL_LINK_TWITTER, "Twitter"),
        (EXTERNAL_LINK_FACEBOOK, "Facebook"),
        (EXTERNAL_LINK_GENERIC, "Generic URL"),
    )

    alt = blocks.CharBlock(required=True)
    icon = blocks.ChoiceBlock(
        choices=EXTERNAL_LINK_TYPES,
        required=True,
    )
    url = blocks.URLBlock(required=True)


class BasicContentLink(blocks.StructBlock):

    page = blocks.PageChooserBlock(
        required=False,
        help_text="You must specify either this, or the URL.",
    )
    url = blocks.CharBlock(
        required=False,
        help_text="You must specify either this, or the URL.",
    )
    title = blocks.CharBlock(required=True)


class BasicContentBlock(blocks.StructBlock):

    class Meta:
        template = "cms_pages/home_page_blocks/basic_content.html"

    PANEL_SIMPLE = "simple"
    PANEL_BLUE_LEFT = "blue_left"
    PANEL_WHITE_RIGHT = "white_right"
    PANEL_TYPES = (
        (PANEL_SIMPLE, "Simple panel, no image."),
        (PANEL_BLUE_LEFT, "Left-aligned image, blue-filtered image BG"),
        (PANEL_WHITE_RIGHT, "Right-aligned image, white background"),
    )

    panel_type = blocks.ChoiceBlock(
        choices=PANEL_TYPES,
        required=True,
    )
    heading = blocks.CharBlock(required=True)
    inset_illustration = blocks.ChoiceBlock(
        choices=ILLUSTRATION_TYPES,
        required=True,
    )
    background_image = imageblocks.ImageChooserBlock(
        required=False,
        help_text="This is used as the background image of a "
                  "blue-left block. It's not used for white-right."
    )
    #body = blocks.RichTextBlock(required=True)
    body = blocks.RawHTMLBlock(required=True)
    link = BasicContentLink()
    external_links = blocks.ListBlock(ExternalLinksBlock)
    compact = blocks.BooleanBlock(
        required=False,
        help_text="True if this block is to be displayed in 'compact' mode",
    )


class PresentationChooserBlock(blocks.ChooserBlock):
    target_model = schedule.models.Presentation
    widget = forms.Select

    # Return the key value for the select field
    def value_for_form(self, value):
        if isinstance(value, self.target_model):
            return value.pk
        else:
            return value


class KeynoteSpeakerBlock(blocks.StructBlock):

    class Meta:
        template = "cms_pages/home_page_blocks/keynote_speaker.html"

    name = blocks.CharBlock(required=True)
    body = blocks.RichTextBlock(required=True)
    links = blocks.ListBlock(ExternalLinksBlock)
    profile_image = imageblocks.ImageChooserBlock(
        required=False,
        help_text="Profile image for the speaker",
    )
    presentation = PresentationChooserBlock(
        help_text="This speaker's presentation",
    )


class KeynotesBlock(blocks.StructBlock):

    class Meta:
        template = "cms_pages/home_page_blocks/keynotes.html"

    heading = blocks.CharBlock(required=True)
    speakers = blocks.ListBlock(KeynoteSpeakerBlock)


class HomePage(Page):

    body = StreamField([
        ("basic_content", BasicContentBlock()),
        ("keynotes", KeynotesBlock()),
        # TODO: other bits
    ])

    content_panels = Page.content_panels + [
        StreamFieldPanel('body')
    ]


# Content pages

class FloatingImageBlock(imageblocks.ImageChooserBlock):

    class Meta:
        template = "cms_pages/content_page_blocks/floating_image.html"


class AnchorBlock(blocks.CharBlock):

    class Meta:
        template = "cms_pages/content_page_blocks/anchor.html"


class ColophonImageListBlock(blocks.StructBlock):

    class Meta:
        template = "cms_pages/content_page_blocks/colophon.html"

    do_nothing = blocks.BooleanBlock(required=False)


class AbstractContentPage(Page):

    class Meta:
        abstract = True

    intro = models.CharField(max_length=250)

    body = StreamField([
        ("rich_text", blocks.RichTextBlock(required=False)),
        ("raw_html", blocks.RawHTMLBlock(required=False)),
        ("floating_image", FloatingImageBlock()),
        ("anchor", AnchorBlock(
            help_text="Add a named anchor to this point in the page"
        )),
        ("colophon_image_list", ColophonImageListBlock()),
    ])

    background_image = models.ForeignKey(
        'CustomImage',
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name='+'
    )

    search_fields = Page.search_fields + [
        index.SearchField('intro'),
        index.SearchField('body'),
    ]

    content_panels = Page.content_panels + [
        ImageChooserPanel('background_image'),
        FieldPanel('intro'),
        StreamFieldPanel('body')
    ]


class ContentPage(AbstractContentPage):

    inset_illustration = models.CharField(
        choices=ILLUSTRATION_TYPES,
        max_length=256,
    )

    content_panels = AbstractContentPage.content_panels + [
        FieldPanel('inset_illustration')
    ]


# News pages

class NewsIndexPage(AbstractContentPage):

    def route(self, request, path_components):

        # Try the default to allow children to resolve
        try:
            return super(NewsIndexPage, self).route(request, path_components)
        except Http404:
            pass

        if path_components:
            # tell Wagtail to call self.serve() with an additional 'path_components' kwarg
            return RouteResult(self, kwargs={'path_components': path_components})
        else:
            raise Http404

    def serve(self, request, path_components=[]):
        ''' Optionally return the RSS version of the page '''

        template = self.template

        if path_components and path_components[0] == "rss":
            template = template.replace(".html", ".rss")

        r = super(NewsIndexPage, self).serve(request)
        r.template_name = template
        return r

    def child_pages(self):
        return NewsPage.objects.live().child_of(self).specific().order_by("-date")

    subpage_types = [
        "NewsPage",
    ]

    content_panels = AbstractContentPage.content_panels


class NewsPage(AbstractContentPage):

    date = models.DateField("Post date")

    portrait_image = models.ForeignKey(
        'CustomImage',
        null=True,
        blank=True,
        on_delete=models.SET_NULL,
        related_name='+'
    )

    parent_page_types = [
        NewsIndexPage,
    ]

    content_panels = AbstractContentPage.content_panels + [
        FieldPanel('date'),
        ImageChooserPanel('portrait_image'),
    ]


@register_snippet
@python_2_unicode_compatible
class ScheduleHeaderParagraph(models.Model):
    ''' Used to show the paragraph in the header for a schedule page. '''
    schedule = models.OneToOneField(
        schedule.models.Schedule,
        related_name="header_paragraph",
    )
    text = models.TextField()

    panels = [
        FieldPanel('schedule'),
        FieldPanel('text'),
    ]

    def __str__(self):
        return str(self.schedule)


@register_snippet
@python_2_unicode_compatible
class NamedHeaderParagraph(models.Model):
    ''' Used to show the paragraph in the header for a schedule page. '''
    name = models.CharField(
        max_length=64,
        help_text="Pass this name to header_paragraph tag.",
    )
    text = models.TextField()

    panels = [
        FieldPanel('name'),
        FieldPanel('text'),
    ]

    def __str__(self):
        return str(self.name)


# Image models -- copied from wagtail docs


class CustomImage(AbstractImage):
    # Add any extra fields to image here

    # eg. To add a caption field:
    copyright_year = models.CharField(
        max_length=64,
        help_text="The year the image was taken",
    )
    licence = models.CharField(
        max_length=64,
        help_text="The short-form code for the licence (e.g. CC-BY)",
    )
    author = models.CharField(
        max_length=255,
        help_text="The name of the author of the work",
    )
    source_url = models.URLField(
        help_text="The URL where you can find the original of this image",
    )

    admin_form_fields = Image.admin_form_fields + (
        "copyright_year",
        "licence",
        "author",
        "source_url",
    )


class CustomRendition(AbstractRendition):
    image = models.ForeignKey(CustomImage, related_name='renditions')

    class Meta:
        unique_together = (
            ('image', 'filter', 'focal_point_key'),
        )


# Delete the source image file when an image is deleted
@receiver(pre_delete, sender=CustomImage)
def image_delete(sender, instance, **kwargs):
    instance.file.delete(False)


# Delete the rendition image file when a rendition is deleted
@receiver(pre_delete, sender=CustomRendition)
def rendition_delete(sender, instance, **kwargs):
    instance.file.delete(False)