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

332 lines
11 KiB
Python
Raw Normal View History

2017-04-08 12:57:00 +00:00
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
2017-05-30 13:24:00 +00:00
from django.utils import translation
2017-09-14 11:44:58 +00:00
from django.conf import settings
2017-05-30 13:24:00 +00:00
2017-04-08 12:57:00 +00:00
from modelcluster.fields import ParentalKey
2019-05-13 15:11:16 +00:00
from wagtail.core.blocks import StructBlock, CharBlock, URLBlock, RichTextBlock, ListBlock, TextBlock, ChoiceBlock
from wagtail.core.models import Page, Orderable
from wagtail.core.fields import StreamField, RichTextField
from wagtail.admin.edit_handlers import FieldPanel, StreamFieldPanel, InlinePanel, MultiFieldPanel
from wagtail.images.blocks import ImageChooserBlock
from wagtail.images.edit_handlers import ImageChooserPanel
from wagtail.search import index
2017-04-08 12:57:00 +00:00
2017-07-03 15:38:13 +00:00
from puput.models import EntryPage, BlogPage
2017-09-14 11:39:10 +00:00
from feedler.models import Entry, Stream
2017-07-26 10:42:06 +00:00
from itertools import chain
2017-04-08 12:57:00 +00:00
from ..util import TranslatedField
class InfoBlock(StructBlock):
title = CharBlock(required=True)
2017-04-19 15:57:29 +00:00
photo = ImageChooserBlock(required=True)
summary = RichTextBlock(required=True)
action = CharBlock(required=False)
url = URLBlock(required=False)
2017-04-19 15:57:29 +00:00
2017-04-08 12:57:00 +00:00
class ArticleIndexPage(Page):
title_fr = models.CharField(max_length=255, default="")
2018-05-25 13:19:46 +00:00
title_en = models.CharField(max_length=255, default="", blank=True)
2017-04-08 12:57:00 +00:00
trans_title = TranslatedField(
'title',
'title_fr',
2018-05-16 13:16:20 +00:00
'title_en',
2017-04-08 12:57:00 +00:00
)
intro_de = RichTextField(default='', blank=True)
intro_fr = RichTextField(default='', blank=True)
2018-05-16 13:08:59 +00:00
intro_en = RichTextField(default='', blank=True)
2017-04-08 12:57:00 +00:00
trans_intro = TranslatedField(
'intro_de',
'intro_fr',
2018-05-16 13:08:59 +00:00
'intro_en',
2017-04-08 12:57:00 +00:00
)
2017-04-19 16:34:55 +00:00
feed_image = models.ForeignKey(
'wagtailimages.Image',
null=True, blank=True,
on_delete=models.SET_NULL,
related_name='+'
)
2017-04-08 12:57:00 +00:00
content_panels = Page.content_panels + [
FieldPanel('intro_de'),
FieldPanel('title_fr'),
FieldPanel('intro_fr'),
2018-05-16 13:08:59 +00:00
FieldPanel('title_en'),
FieldPanel('intro_en'),
2017-04-19 16:34:55 +00:00
ImageChooserPanel('feed_image'),
2017-04-08 12:57:00 +00:00
]
def get_context(self, request):
context = super(ArticleIndexPage, self).get_context(request)
articles = ArticlePage.objects.child_of(self).live()
context['articles'] = articles
2017-04-19 15:47:06 +00:00
subcategories = ArticleIndexPage.objects.child_of(self).live()
context['subcategories'] = subcategories
2017-04-08 12:57:00 +00:00
return context
2017-07-03 15:04:09 +00:00
parent_page_types = [
'home.ArticleIndexPage',
'home.HomePage'
]
2017-04-19 15:47:06 +00:00
subpage_types = [
'home.ArticlePage',
'home.ArticleIndexPage',
2017-07-04 08:28:38 +00:00
'home.ContactForm',
'wagtailcore.Page'
2017-04-19 15:47:06 +00:00
]
2017-04-08 12:57:00 +00:00
class Meta:
verbose_name = "Rubrik"
2017-09-05 14:24:48 +00:00
class ImageCarouselBlock(StructBlock):
image = ImageChooserBlock()
caption = TextBlock(required=False)
class Meta:
icon = 'image'
2017-04-08 12:57:00 +00:00
class ArticlePage(Page):
title_fr = models.CharField(max_length=255, default="")
2018-05-25 13:19:46 +00:00
title_en = models.CharField(max_length=255, default="", blank=True)
2017-04-08 12:57:00 +00:00
trans_title = TranslatedField(
'title',
'title_fr',
2018-05-16 13:08:59 +00:00
'title_en',
2017-04-08 12:57:00 +00:00
)
intro_de = RichTextField(default='', blank=True)
intro_fr = RichTextField(default='', blank=True)
2018-05-16 13:08:59 +00:00
intro_en = RichTextField(default='', blank=True)
2017-04-08 12:57:00 +00:00
trans_intro = TranslatedField(
'intro_de',
'intro_fr',
2018-05-16 13:08:59 +00:00
'intro_en',
2017-04-08 12:57:00 +00:00
)
2017-09-06 12:57:19 +00:00
gallery = StreamField([
('image', ListBlock(ImageCarouselBlock(), icon="image")),
], blank=True)
# documents = StreamField([
# ('documents', ListBlock(DocumentChooserBlock(), icon="document")),
# ])
2017-04-08 12:57:00 +00:00
body_de = StreamField([
('paragraph', RichTextBlock()),
('section', CharBlock(classname="full title")),
('info', InfoBlock(icon='help')),
('media', ChoiceBlock(choices=[
2017-09-06 12:57:19 +00:00
('gallery', 'Image gallery'),
], icon='media'))
2017-04-08 12:57:00 +00:00
], null=True, blank=True)
body_fr = StreamField([
('paragraph', RichTextBlock()),
('section', CharBlock(classname="full title")),
('info', InfoBlock(icon='help')),
('media', ChoiceBlock(choices=[
2017-09-06 12:57:19 +00:00
('gallery', 'Image gallery'),
], icon='media'))
2017-04-08 12:57:00 +00:00
], null=True, blank=True)
2018-05-16 13:08:59 +00:00
body_en = StreamField([
('paragraph', RichTextBlock()),
('section', CharBlock(classname="full title")),
('info', InfoBlock(icon='help')),
('media', ChoiceBlock(choices=[
('gallery', 'Image gallery'),
], icon='media'))
], null=True, blank=True)
2017-04-08 12:57:00 +00:00
trans_body = TranslatedField(
'body_de',
'body_fr',
2018-05-16 13:08:59 +00:00
'body_en',
2017-04-08 12:57:00 +00:00
)
date = models.DateField("Date", null=True, blank=True)
2017-04-10 23:01:41 +00:00
2017-04-11 21:57:09 +00:00
on_homepage = models.BooleanField(default=False, verbose_name="Featured",
help_text="Auf der Frontpage anzeigen")
2017-04-10 23:01:41 +00:00
2017-04-08 12:57:00 +00:00
feed_image = models.ForeignKey(
'wagtailimages.Image',
2017-04-19 16:34:55 +00:00
null=True, blank=True,
2017-04-08 12:57:00 +00:00
on_delete=models.SET_NULL,
related_name='+'
)
2017-05-03 22:07:07 +00:00
search_fields = Page.search_fields + [
index.SearchField('title', partial_match=True, boost=10),
index.SearchField('title_fr', partial_match=True, boost=10),
2018-05-16 13:08:59 +00:00
index.SearchField('title_en', partial_match=True, boost=10),
2017-05-03 22:07:07 +00:00
index.SearchField('body_de', partial_match=True),
index.SearchField('body_fr', partial_match=True),
2018-05-16 13:08:59 +00:00
index.SearchField('body_en', partial_match=True),
2017-05-03 22:07:07 +00:00
index.SearchField('intro_de', partial_match=True),
index.SearchField('intro_fr', partial_match=True),
2018-05-16 13:08:59 +00:00
index.SearchField('intro_en', partial_match=True),
2017-05-03 22:07:07 +00:00
]
2017-04-08 12:57:00 +00:00
content_panels = [
MultiFieldPanel([
FieldPanel('title'),
FieldPanel('intro_de'),
2018-06-18 13:29:33 +00:00
], heading="Deutsch",
classname="collapsible collapsed"),
StreamFieldPanel('body_de'),
2017-04-08 12:57:00 +00:00
MultiFieldPanel([
FieldPanel('title_fr'),
FieldPanel('intro_fr'),
2018-06-18 13:29:33 +00:00
], heading="Français",
classname="collapsible collapsed"),
StreamFieldPanel('body_fr'),
2018-05-16 13:08:59 +00:00
MultiFieldPanel([
FieldPanel('title_en'),
FieldPanel('intro_en'),
2018-06-18 13:29:33 +00:00
], heading="English",
classname="collapsible collapsed"),
2018-05-16 13:08:59 +00:00
StreamFieldPanel('body_en'),
MultiFieldPanel([
ImageChooserPanel('feed_image'),
], heading="Images"),
2017-09-06 12:57:19 +00:00
StreamFieldPanel('gallery'),
2017-04-08 12:57:00 +00:00
]
promote_panels = [
InlinePanel('related_links', label="Links"),
2017-04-11 21:57:09 +00:00
MultiFieldPanel([
FieldPanel('date'),
FieldPanel('on_homepage'),
], heading="Veröffentlichung"),
MultiFieldPanel(Page.promote_panels, "Einstellungen"),
2017-04-08 12:57:00 +00:00
]
2017-04-11 22:10:00 +00:00
2017-07-03 15:04:09 +00:00
parent_page_types = [
'home.ArticleIndexPage',
'home.HomePage'
]
2017-04-08 12:57:00 +00:00
subpage_types = []
class Meta:
verbose_name = "Artikel"
class ArticleRelatedLink(Orderable):
page = ParentalKey(ArticlePage, related_name='related_links')
name = models.CharField(max_length=255)
url = models.URLField()
panels = [
FieldPanel('name'),
FieldPanel('url'),
]
2017-04-19 16:34:55 +00:00
2017-04-08 12:57:00 +00:00
class HomePage(Page):
intro_de = RichTextField(default='')
intro_fr = RichTextField(default='')
2018-05-25 13:19:46 +00:00
intro_en = RichTextField(default='', blank=True)
2017-04-08 12:57:00 +00:00
trans_intro = TranslatedField(
'intro_de',
'intro_fr',
2018-05-25 13:17:53 +00:00
'intro_en',
2017-04-08 12:57:00 +00:00
)
body_de = RichTextField(default='', blank=True)
body_fr = RichTextField(default='', blank=True)
2018-05-25 13:17:53 +00:00
body_en = RichTextField(default='', blank=True)
2017-04-08 12:57:00 +00:00
trans_body = TranslatedField(
'body_de',
'body_fr',
2018-05-25 13:17:53 +00:00
'body_en',
2017-04-08 12:57:00 +00:00
)
infos_de = StreamField([
('info', InfoBlock())
], null=True, blank=True)
infos_fr = StreamField([
('info', InfoBlock())
], null=True, blank=True)
2018-05-25 13:17:53 +00:00
infos_en = StreamField([
('info', InfoBlock())
], null=True, blank=True)
2017-04-08 12:57:00 +00:00
trans_infos = TranslatedField(
'infos_de',
'infos_fr',
2018-05-25 13:17:53 +00:00
'infos_en',
2017-04-08 12:57:00 +00:00
)
content_panels = Page.content_panels + [
MultiFieldPanel([
FieldPanel('intro_de', classname="full"),
FieldPanel('body_de', classname="full"),
StreamFieldPanel('infos_de'),
2018-06-18 13:29:33 +00:00
], heading="Deutsch",
classname="collapsible collapsed"),
MultiFieldPanel([
2017-04-08 12:57:00 +00:00
FieldPanel('intro_fr', classname="full"),
FieldPanel('body_fr', classname="full"),
StreamFieldPanel('infos_fr'),
2018-06-18 13:29:33 +00:00
], heading="Français",
classname="collapsible collapsed"),
2018-05-25 13:17:53 +00:00
MultiFieldPanel([
FieldPanel('intro_en', classname="full"),
FieldPanel('body_en', classname="full"),
StreamFieldPanel('infos_en'),
2018-06-18 13:29:33 +00:00
], heading="English",
classname="collapsible collapsed"),
2017-04-08 12:57:00 +00:00
]
@property
def featured(self):
# Get list of live pages that are descendants of this page
2018-05-25 14:38:06 +00:00
articles = ArticlePage.objects.live().descendant_of(self)
2017-04-10 23:01:41 +00:00
articles = articles.filter(on_homepage=True)
2018-05-25 14:38:06 +00:00
articles = articles.filter(feed_image__isnull=False)
2017-04-08 12:57:00 +00:00
# Order by most recent date first
#articles = articles.order_by('-date')
return articles[:4]
@property
def blogentries(self):
2017-04-08 12:57:00 +00:00
# Get list of latest news
2017-05-30 13:24:00 +00:00
curlang = translation.get_language()
2018-05-16 13:08:59 +00:00
if not curlang in ['de', 'fr', 'en', 'it']: curlang = 'de' # Default language
2017-05-30 13:24:00 +00:00
parent = BlogPage.objects.filter(slug='news-%s' % curlang)
if not parent: return []
2017-07-26 10:42:06 +00:00
posts = EntryPage.objects.live().descendant_of(parent[0])
2017-04-08 12:57:00 +00:00
# Order by most recent date first
2017-07-26 10:42:06 +00:00
posts = posts.order_by('-date')
2017-09-14 11:44:58 +00:00
return posts[:settings.BLOG_ENTRIES_HOME_PAGE]
2017-09-04 13:30:33 +00:00
@property
def newsentries(self):
2017-09-14 11:39:10 +00:00
# Get the last few news entries for the home page
2017-07-26 10:42:06 +00:00
entries = Entry.objects.all().order_by('-published')
2017-09-04 21:03:01 +00:00
# Filter out by current language
curlang = translation.get_language()
if curlang in ['de']:
entries = entries.exclude(lang='fr')
2018-05-16 13:08:59 +00:00
else:
2017-09-04 21:03:01 +00:00
entries = entries.exclude(lang='de')
2018-05-16 13:08:59 +00:00
# TODO: English news?
2017-09-14 11:39:10 +00:00
news = events = jobs = []
Stream1 = Stream.objects.filter(title='News')
2019-06-14 20:02:15 +00:00
if Stream1: news = entries.filter(stream=Stream1.first())
2017-09-14 11:39:10 +00:00
Stream2 = Stream.objects.filter(title='Events')
2019-06-14 20:02:15 +00:00
if Stream2: events = entries.filter(stream=Stream2.first())
2017-09-14 11:39:10 +00:00
Stream3 = Stream.objects.filter(title='Jobs')
2019-06-14 20:02:15 +00:00
if Stream3: jobs = entries.filter(stream=Stream3.first())
2017-09-14 11:44:58 +00:00
i = settings.NEWS_ENTRIES_HOME_PAGE
return list(chain(news[:i], events[:i], jobs[:i]))
2017-04-08 12:57:00 +00:00
def get_context(self, request):
# Update template context
context = super(HomePage, self).get_context(request)
context['featured'] = self.featured
context['blogentries'] = self.blogentries
2017-09-04 13:30:33 +00:00
context['newsentries'] = self.newsentries
2017-09-14 11:44:58 +00:00
context['entryfeeds'] = settings.STREAMS_ON_HOME_PAGE
2017-04-08 12:57:00 +00:00
return context
2017-07-03 11:35:36 +00:00
parent_page_types = ['wagtailcore.Page']
2017-04-08 12:57:00 +00:00
class Meta:
verbose_name = "Frontpage"