Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
55
56
57
58
59
60
61
62
63
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.fields import StreamField
from wagtail.wagtailadmin.edit_handlers import (
StreamFieldPanel, FieldPanel, InlinePanel
)
from wagtail.wagtailsearch import index
from opentech.utils.models import BasePage, RelatedPage
from opentech.utils.blocks import StoryBlock
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 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 + [
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
FieldPanel('publication_date'),
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