From 00f6f44c619d9edef8e24c3073a2f1761d1fb176 Mon Sep 17 00:00:00 2001 From: Oleg Lavrovsky Date: Tue, 21 Feb 2017 18:00:16 +0100 Subject: [PATCH] Home page and article structure --- publichealth/home/models.py | 106 ++++++++++++++++-- .../home/templates/article/article_page.html | 28 +++++ publichealth/home/templates/banner.html | 7 +- .../home/templates/home/home_page.html | 52 ++------- 4 files changed, 138 insertions(+), 55 deletions(-) create mode 100644 publichealth/home/templates/article/article_page.html diff --git a/publichealth/home/models.py b/publichealth/home/models.py index 5530b51..ab52890 100644 --- a/publichealth/home/models.py +++ b/publichealth/home/models.py @@ -1,18 +1,41 @@ from __future__ import unicode_literals from django.db import models +from modelcluster.fields import ParentalKey -from wagtail.wagtailcore.models import Page -from wagtail.wagtailcore.fields import StreamField from wagtail.wagtailcore import blocks -from wagtail.wagtailadmin.edit_handlers import FieldPanel, StreamFieldPanel +from wagtail.wagtailcore.models import Page, Orderable +from wagtail.wagtailcore.fields import StreamField, RichTextField +from wagtail.wagtailadmin.edit_handlers import FieldPanel, StreamFieldPanel, InlinePanel, MultiFieldPanel from wagtail.wagtailimages.blocks import ImageChooserBlock +from wagtail.wagtailimages.edit_handlers import ImageChooserPanel from wagtail.wagtailsearch import index from .util import TranslatedField -class HomePage(Page): +class ArticleIndexPage(Page): title_fr = models.CharField(max_length=255, default="") + translated_title = TranslatedField( + 'title', + 'title_fr', + ) + content_panels = Page.content_panels + [ + FieldPanel('title_fr'), + ] + def get_context(self, request): + context = super(ArticleIndexPage, self).get_context(request) + # Add extra variables and return the updated context + context['article_entries'] = ArticlePage.objects.child_of(self).live() + return context + +class ArticlePage(Page): + title_fr = models.CharField(max_length=255, default="") + translated_title = TranslatedField( + 'title', + 'title_fr', + ) + + date = models.DateField("Date") body_de = StreamField([ ('heading', blocks.CharBlock(classname="full title")), @@ -24,22 +47,87 @@ class HomePage(Page): ('paragraph', blocks.RichTextBlock()), ('image', ImageChooserBlock()), ], null=True, blank=True) - - translated_title = TranslatedField( - 'title', - 'title_fr', - ) body = TranslatedField( 'body_de', 'body_fr', ) + feed_image = models.ForeignKey( + 'wagtailimages.Image', + null=True, + blank=True, + on_delete=models.SET_NULL, + related_name='+' + ) + search_fields = Page.search_fields + [ index.SearchField('body_de'), index.SearchField('body_fr'), + index.SearchField('title'), + index.SearchField('title_fr'), + index.FilterField('date'), ] content_panels = Page.content_panels + [ FieldPanel('title_fr'), + FieldPanel('date'), StreamFieldPanel('body_de'), StreamFieldPanel('body_fr'), + InlinePanel('related_links', label="Related links"), + ] + promote_panels = [ + MultiFieldPanel(Page.promote_panels, "Common page configuration"), + ImageChooserPanel('feed_image'), + ] + parent_page_types = ['home.ArticleIndexPage'] + subpage_types = [] + +class ArticleRelatedLink(Orderable): + page = ParentalKey(ArticlePage, related_name='related_links') + name = models.CharField(max_length=255) + url = models.URLField() + panels = [ + FieldPanel('name'), + FieldPanel('url'), + ] + +class InfoBlock(blocks.StructBlock): + title = blocks.CharBlock(required=True) + photo = ImageChooserBlock() + summary = blocks.RichTextBlock() + action = blocks.CharBlock(required=True) + url = models.URLField() + +class HomePage(Page): + intro_de = RichTextField(default='') + intro_fr = RichTextField(default='') + intro = TranslatedField( + 'intro_de', + 'intro_fr', + ) + + body_de = RichTextField(default='') + body_fr = RichTextField(default='') + body = TranslatedField( + 'body_de', + 'body_fr', + ) + + infos_de = StreamField([ + ('info', InfoBlock()) + ], null=True, blank=True) + infos_fr = StreamField([ + ('info', InfoBlock()) + ], null=True, blank=True) + infos = TranslatedField( + 'infos_de', + 'infos_fr', + ) + + content_panels = Page.content_panels + [ + FieldPanel('intro_de', classname="full"), + FieldPanel('intro_fr', classname="full"), + FieldPanel('body_de', classname="full"), + FieldPanel('body_fr', classname="full"), + StreamFieldPanel('infos_de'), + StreamFieldPanel('infos_fr'), ] diff --git a/publichealth/home/templates/article/article_page.html b/publichealth/home/templates/article/article_page.html new file mode 100644 index 0000000..3285133 --- /dev/null +++ b/publichealth/home/templates/article/article_page.html @@ -0,0 +1,28 @@ +{% extends "base.html" %} +{% load wagtailcore_tags %} + +{% block body_class %}template-{{ self.get_verbose_name|slugify }}{% endblock %} + +{% block content %} + +

{{ page.translated_title }}

+ {{ page.date }} + + {% include_block feed_image %} + + +
+ + {% for block in page.body %} + {% if block.block_type == 'heading' %} +

{{ block.value }}

+ {% else %} +
+ {% include_block block %} +
+ {% endif %} + {% endfor %} + +
+ +{% endblock %} diff --git a/publichealth/home/templates/banner.html b/publichealth/home/templates/banner.html index 1a19271..ed82111 100644 --- a/publichealth/home/templates/banner.html +++ b/publichealth/home/templates/banner.html @@ -1,3 +1,5 @@ +{% load wagtailcore_tags %} + - -
- - {% for block in page.body %} {% if block.block_type == 'heading' %}

{{ block.value }}

{% else %}
{% include_block block %}
- {% endif %} {% endfor %}