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

244 lines
7.4 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-04-08 12:57:00 +00:00
from modelcluster.fields import ParentalKey
from wagtail.wagtailcore.blocks import StructBlock, CharBlock, URLBlock, RichTextBlock
2017-04-08 12:57:00 +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
from wagtail.wagtailimages.edit_handlers import ImageChooserPanel
2017-05-03 22:07:07 +00:00
from wagtail.wagtailsearch 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-07-26 10:42:06 +00:00
from feedler.models import Entry
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="")
trans_title = TranslatedField(
'title',
'title_fr',
)
intro_de = RichTextField(default='', blank=True)
intro_fr = RichTextField(default='', blank=True)
trans_intro = TranslatedField(
'intro_de',
'intro_fr',
)
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'),
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"
class ArticlePage(Page):
title_fr = models.CharField(max_length=255, default="")
trans_title = TranslatedField(
'title',
'title_fr',
)
intro_de = RichTextField(default='', blank=True)
intro_fr = RichTextField(default='', blank=True)
trans_intro = TranslatedField(
'intro_de',
'intro_fr',
)
body_de = StreamField([
('paragraph', RichTextBlock()),
2017-04-08 12:57:00 +00:00
('image', ImageChooserBlock()),
('section', CharBlock(classname="full title")),
2017-04-19 15:57:29 +00:00
('info', InfoBlock()),
2017-04-08 12:57:00 +00:00
], null=True, blank=True)
body_fr = StreamField([
('paragraph', RichTextBlock()),
2017-04-08 12:57:00 +00:00
('image', ImageChooserBlock()),
('section', CharBlock(classname="full title")),
2017-04-19 15:57:29 +00:00
('info', InfoBlock()),
2017-04-08 12:57:00 +00:00
], null=True, blank=True)
trans_body = TranslatedField(
'body_de',
'body_fr',
)
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),
index.SearchField('body_de', partial_match=True),
index.SearchField('body_fr', partial_match=True),
index.SearchField('intro_de', partial_match=True),
index.SearchField('intro_fr', partial_match=True),
]
2017-04-08 12:57:00 +00:00
content_panels = [
MultiFieldPanel([
FieldPanel('title'),
FieldPanel('intro_de'),
], heading="Deutsch"),
StreamFieldPanel('body_de'),
2017-04-08 12:57:00 +00:00
MultiFieldPanel([
FieldPanel('title_fr'),
FieldPanel('intro_fr'),
], heading="Français"),
StreamFieldPanel('body_fr'),
MultiFieldPanel([
ImageChooserPanel('feed_image'),
], heading="Images"),
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='')
trans_intro = TranslatedField(
'intro_de',
'intro_fr',
)
body_de = RichTextField(default='', blank=True)
body_fr = RichTextField(default='', blank=True)
trans_body = TranslatedField(
'body_de',
'body_fr',
)
infos_de = StreamField([
('info', InfoBlock())
], null=True, blank=True)
infos_fr = StreamField([
('info', InfoBlock())
], null=True, blank=True)
trans_infos = TranslatedField(
'infos_de',
'infos_fr',
)
content_panels = Page.content_panels + [
MultiFieldPanel([
FieldPanel('intro_de', classname="full"),
FieldPanel('body_de', classname="full"),
StreamFieldPanel('infos_de'),
], heading="Deutsch"),
MultiFieldPanel([
2017-04-08 12:57:00 +00:00
FieldPanel('intro_fr', classname="full"),
FieldPanel('body_fr', classname="full"),
StreamFieldPanel('infos_fr'),
], heading="Français"),
]
@property
def featured(self):
# Get list of live pages that are descendants of this page
articles = ArticlePage.objects.live() #.descendant_of(self)
2017-04-10 23:01:41 +00:00
articles = articles.filter(on_homepage=True)
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()
2017-05-31 08:36:09 +00:00
if not curlang in ['de', 'fr']: 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')
# Get news entries
entries = Entry.objects.all().order_by('-published')
return list(chain(entries[:3], posts[:3]))
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-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"