Newer
Older
from django.db import models
from django.db.models.functions import Coalesce
from django.conf import settings
from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
from modelcluster.fields import ParentalKey
from wagtail.wagtailcore.models import Orderable
from wagtail.wagtailcore.fields import StreamField
from wagtail.wagtailadmin.edit_handlers import (
InlinePanel,
FieldPanel,
PageChooserPanel,
StreamFieldPanel,
)
from wagtail.wagtailsearch import index
from opentech.public.utils.models import BasePage, RelatedPage
from opentech.public.utils.blocks import StoryBlock
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
class NewsType(models.Model):
title = models.CharField(max_length=128)
def __str__(self):
return self.title
class NewsPageNewsType(models.Model):
page = ParentalKey(
'news.NewsPage',
related_name='news_types'
)
news_type = models.ForeignKey(
'NewsType',
related_name='+',
on_delete=models.CASCADE
)
panels = [
FieldPanel('news_type')
]
def __str__(self):
return self.news_type.title
class NewsPageRelatedPage(RelatedPage):
source_page = ParentalKey(
'news.NewsPage',
related_name='related_pages'
)
class NewsPageAuthor(Orderable):
source_page = ParentalKey(
'news.NewsPage',
related_name='authors'
)
author = models.ForeignKey(
'wagtailcore.Page',
related_name='+',
)
panels = [
PageChooserPanel('author', 'people.PersonPage')
]
class NewsPage(BasePage):
subpage_types = []
parent_page_types = ['NewsIndex']
# It's datetime for easy comparison with first_published_at
publication_date = models.DateTimeField(
null=True, blank=True,
help_text="Use this field to override the date that the "
"news item appears to have been published."
)
introduction = models.TextField(blank=True)
body = StreamField(StoryBlock())
search_fields = BasePage.search_fields + [
index.SearchField('introduction'),
index.SearchField('body')
]
content_panels = BasePage.content_panels + [
FieldPanel('publication_date'),
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
FieldPanel('introduction'),
StreamFieldPanel('body'),
InlinePanel('news_types', label="News types"),
InlinePanel('related_pages', label="Related pages"),
]
@property
def display_date(self):
if self.publication_date:
return self.publication_date
else:
return self.first_published_at
class NewsIndex(BasePage):
subpage_types = ['NewsPage']
parent_page_types = ['home.HomePage']
def get_context(self, request, *args, **kwargs):
news = NewsPage.objects.live().public().descendant_of(self).annotate(
date=Coalesce('publication_date', 'first_published_at')
).order_by('-date')
if request.GET.get('news_type'):
news = news.filter(news_types__news_type=request.GET.get('news_type'))
# Pagination
page = request.GET.get('page', 1)
paginator = Paginator(news, settings.DEFAULT_PER_PAGE)
try:
news = paginator.page(page)
except PageNotAnInteger:
news = paginator.page(1)
except EmptyPage:
news = paginator.page(paginator.num_pages)
context = super().get_context(request, *args, **kwargs)
context.update(
news=news,
# Only show news types that have been used
news_types=NewsPageNewsType.objects.all().values_list(
'news_type__pk', 'news_type__title'
).distinct()
)
return context