Newer
Older
from django.core.paginator import EmptyPage, PageNotAnInteger, Paginator
from wagtail.wagtailadmin.edit_handlers import (
FieldPanel,
PageChooserPanel,
StreamFieldPanel,
)
from wagtail.wagtailcore.fields import StreamField
from opentech.public.utils.models import BasePage
from .blocks import FundBlock
class FundPage(BasePage):
subpage_types = []
parent_page_types = ['FundIndex']
introduction = models.TextField(blank=True)
fund_type = models.ForeignKey(
'wagtailcore.Page',
on_delete=models.SET_NULL,
related_name='+',
)
body = StreamField(FundBlock())
content_panels = BasePage.content_panels + [
FieldPanel('introduction'),
PageChooserPanel('fund_type', 'funds.FundType'),
subpage_types = ['FundPage']
parent_page_types = ['home.HomePage']
def get_context(self, request, *args, **kwargs):
funds = FundPage.objects.live().public().descendant_of(self)
# Pagination
page = request.GET.get('page', 1)
paginator = Paginator(funds, 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)
return context
60
61
62
63
64
65
66
67
68
69
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
116
117
118
119
120
121
122
123
124
125
126
class LabPage(BasePage):
subpage_types = []
parent_page_types = ['LabIndex']
introduction = models.TextField(blank=True)
lab_type = models.ForeignKey(
'wagtailcore.Page',
blank=True,
null=True,
on_delete=models.SET_NULL,
related_name='+',
)
lab_link = models.URLField(blank=True, verbose_name="External link")
body = StreamField(FundBlock())
content_panels = BasePage.content_panels + [
FieldPanel('introduction'),
MultiFieldPanel([
# Limit to lab pages once created
PageChooserPanel('lab_type'),
FieldPanel('lab_link'),
], heading='Link for lab application'),
StreamFieldPanel('body'),
]
def clean(self):
if self.lab_type and self.lab_link:
raise ValidationError({
'lab_type': 'Cannot link to both a Lab page and external link',
'lab_link': 'Cannot link to both a Lab page and external link',
})
if not self.lab_type and not self.lab_link:
raise ValidationError({
'lab_type': 'Please provide a way for applicants to apply',
'lab_link': 'Please provide a way for applicants to apply',
})
class LabIndex(BasePage):
subpage_types = ['LabPage']
parent_page_types = ['home.HomePage']
introduction = models.TextField(blank=True)
content_panels = BasePage.content_panels + [
FieldPanel('introduction')
]
def get_context(self, request, *args, **kwargs):
funds = LabPage.objects.live().public().descendant_of(self)
# Pagination
page = request.GET.get('page', 1)
paginator = Paginator(funds, 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)
return context