public-health-ch/publichealth/home/models.py

201 lines
5.9 KiB
Python
Raw Normal View History

2017-03-07 22:37:55 +00:00
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
2017-02-21 17:00:16 +00:00
from modelcluster.fields import ParentalKey
from wagtail.wagtailcore import blocks
2017-02-21 17:00:16 +00:00
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
2017-02-21 17:00:16 +00:00
from wagtail.wagtailimages.edit_handlers import ImageChooserPanel
from wagtail.wagtailsearch import index
2017-03-14 10:10:32 +00:00
from puput.models import EntryPage
from .util import TranslatedField
2017-02-21 17:00:16 +00:00
class ArticleIndexPage(Page):
title_fr = models.CharField(max_length=255, default="")
2017-02-23 09:13:44 +00:00
trans_title = TranslatedField(
2017-02-21 17:00:16 +00:00
'title',
'title_fr',
)
2017-03-08 21:02:47 +00:00
intro_de = RichTextField(default='', blank=True)
intro_fr = RichTextField(default='', blank=True)
trans_intro = TranslatedField(
'intro_de',
'intro_fr',
)
2017-02-21 17:00:16 +00:00
content_panels = Page.content_panels + [
2017-03-08 21:02:47 +00:00
FieldPanel('intro_de'),
2017-02-21 17:00:16 +00:00
FieldPanel('title_fr'),
2017-03-08 21:02:47 +00:00
FieldPanel('intro_fr'),
2017-02-21 17:00:16 +00:00
]
2017-03-08 22:04:08 +00:00
2017-02-21 17:00:16 +00:00
def get_context(self, request):
context = super(ArticleIndexPage, self).get_context(request)
2017-03-08 22:04:08 +00:00
articles = ArticlePage.objects.child_of(self).live()
context['articles'] = articles
2017-02-21 17:00:16 +00:00
return context
2017-03-08 22:04:08 +00:00
2017-04-06 12:52:13 +00:00
subpage_types = ['home.ArticlePage', 'home.ArticleIndexPage']
2017-02-23 09:13:44 +00:00
class Meta:
verbose_name = "Rubrik"
2017-02-21 17:00:16 +00:00
class ArticlePage(Page):
title_fr = models.CharField(max_length=255, default="")
2017-02-23 09:13:44 +00:00
trans_title = TranslatedField(
2017-02-21 17:00:16 +00:00
'title',
'title_fr',
)
2017-03-08 21:02:47 +00:00
intro_de = RichTextField(default='', blank=True)
intro_fr = RichTextField(default='', blank=True)
2017-02-23 09:13:44 +00:00
trans_intro = TranslatedField(
2017-02-23 08:23:18 +00:00
'intro_de',
'intro_fr',
)
body_de = StreamField([
('paragraph', blocks.RichTextBlock()),
('image', ImageChooserBlock()),
2017-02-23 09:13:44 +00:00
('section', blocks.CharBlock(classname="full title")),
], null=True, blank=True)
body_fr = StreamField([
('paragraph', blocks.RichTextBlock()),
('image', ImageChooserBlock()),
2017-02-23 09:13:44 +00:00
('section', blocks.CharBlock(classname="full title")),
], null=True, blank=True)
2017-02-23 09:13:44 +00:00
trans_body = TranslatedField(
'body_de',
'body_fr',
)
2017-03-08 21:54:15 +00:00
date = models.DateField("Date", null=True, blank=True)
2017-02-21 17:00:16 +00:00
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'),
2017-02-21 17:00:16 +00:00
index.SearchField('title'),
index.SearchField('title_fr'),
2017-02-23 09:13:44 +00:00
index.SearchField('intro_de'),
index.SearchField('intro_fr'),
]
2017-03-07 22:37:55 +00:00
content_panels = [
MultiFieldPanel([
FieldPanel('title'),
FieldPanel('intro_de'),
StreamFieldPanel('body_de'),
], heading="Deutsch"),
MultiFieldPanel([
FieldPanel('title_fr'),
FieldPanel('intro_fr'),
StreamFieldPanel('body_fr'),
], heading="Français"),
2017-03-13 22:13:45 +00:00
ImageChooserPanel('feed_image'),
2017-02-21 17:00:16 +00:00
]
promote_panels = [
2017-03-08 21:54:15 +00:00
FieldPanel('date'),
2017-03-07 22:37:55 +00:00
InlinePanel('related_links', label="Links"),
MultiFieldPanel(Page.promote_panels, "Common page configuration"),
2017-02-21 17:00:16 +00:00
]
parent_page_types = ['home.ArticleIndexPage']
subpage_types = []
2017-02-23 09:13:44 +00:00
class Meta:
verbose_name = "Artikel"
2017-02-21 17:00:16 +00:00
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()
2017-02-23 09:13:44 +00:00
summary = blocks.RichTextBlock(required=True)
action = blocks.CharBlock()
url = blocks.URLBlock()
2017-02-21 17:00:16 +00:00
class HomePage(Page):
intro_de = RichTextField(default='')
intro_fr = RichTextField(default='')
2017-02-23 09:13:44 +00:00
trans_intro = TranslatedField(
2017-02-21 17:00:16 +00:00
'intro_de',
'intro_fr',
)
2017-03-08 21:02:47 +00:00
body_de = RichTextField(default='', blank=True)
body_fr = RichTextField(default='', blank=True)
2017-02-23 09:13:44 +00:00
trans_body = TranslatedField(
2017-02-21 17:00:16 +00:00
'body_de',
'body_fr',
)
infos_de = StreamField([
('info', InfoBlock())
], null=True, blank=True)
infos_fr = StreamField([
('info', InfoBlock())
], null=True, blank=True)
2017-02-23 09:13:44 +00:00
trans_infos = TranslatedField(
2017-02-21 17:00:16 +00:00
'infos_de',
'infos_fr',
)
content_panels = Page.content_panels + [
2017-03-08 21:02:47 +00:00
MultiFieldPanel([
FieldPanel('intro_de', classname="full"),
FieldPanel('body_de', classname="full"),
StreamFieldPanel('infos_de'),
], heading="Deutsch"),
MultiFieldPanel([
FieldPanel('intro_fr', classname="full"),
FieldPanel('body_fr', classname="full"),
StreamFieldPanel('infos_fr'),
], heading="Français"),
]
2017-03-08 21:02:47 +00:00
@property
def featured(self):
# Get list of live pages that are descendants of this page
2017-03-14 10:10:32 +00:00
articles = ArticlePage.objects.live() #.descendant_of(self)
2017-03-08 21:02:47 +00:00
# Order by most recent date first
#articles = articles.order_by('-date')
2017-03-14 10:10:32 +00:00
return articles[:4]
@property
def newsfeed(self):
# Get list of latest news
# TODO: fetch children of 'News (DE)'
entries = EntryPage.objects.live().descendant_of(self)
# Order by most recent date first
entries = entries.order_by('-date')
return entries[:4]
2017-03-08 21:02:47 +00:00
def get_context(self, request):
# Update template context
context = super(HomePage, self).get_context(request)
2017-03-14 10:10:32 +00:00
context['featured'] = self.featured
context['newsfeed'] = self.newsfeed
2017-03-13 16:33:02 +00:00
return context
2017-03-13 16:56:33 +00:00
parent_page_types = []
2017-03-13 16:33:02 +00:00
class Meta:
2017-04-06 12:52:13 +00:00
verbose_name = "Frontpage"