-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmodels.py
354 lines (299 loc) · 9.86 KB
/
models.py
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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import json
from django.db import models
from django.http import HttpResponse
from django.shortcuts import redirect, render
from django.template import loader
from wagtail.admin.panels import (
FieldPanel,
InlinePanel,
MultiFieldPanel,
ObjectList,
TabbedInterface,
TitleFieldPanel,
)
from wagtail.blocks import URLBlock
from wagtail.contrib.routable_page.models import RoutablePageMixin, route
from wagtail.documents.blocks import DocumentChooserBlock
from wagtail.fields import RichTextField, StreamField
from wagtail.models import Page
from wagtail.search import index
from home.models import AbstractHomeContentPage, Post, RedirectHeadlessPreviewMixin
from programs.models import AbstractContentPage
from .blocks import EndnoteBlock, FeaturedReportSectionBlock, ReportSectionBlock
from .tasks import (
generate_pdf,
generate_report_contents,
get_report_authors,
parse_pdf,
write_pdf,
)
class Report(RedirectHeadlessPreviewMixin, RoutablePageMixin, Post):
"""
Report class that inherits from the abstract
Post model and creates pages for Policy Papers.
"""
parent_page_types = ["ReportsHomepage"]
subpage_types = []
sections = StreamField(
[
(
"section",
ReportSectionBlock(
template="components/report_section_body.html", required=False
),
)
],
null=True,
blank=True,
use_json_field=True,
)
abstract = RichTextField(blank=True, null=True)
acknowledgements = RichTextField(
blank=True,
null=True,
features=[
"blockquote",
"bold",
"document-link",
"h2",
"h3",
"h4",
"h5",
"hr",
"italic",
"link",
"ol",
"pullquote",
"ul",
],
)
source_word_doc = models.ForeignKey(
"wagtaildocs.Document",
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name="+",
verbose_name="Source Word Document",
)
report_pdf = models.ForeignKey(
"wagtaildocs.Document",
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name="+",
verbose_name="Report PDF",
)
dataviz_src = models.CharField(blank=True, null=True, max_length=300, help_text="")
overwrite_sections_on_save = models.BooleanField(
default=False,
help_text="If checked, sections and endnote fields ⚠ will be overwritten ⚠ with Word document source on save. Use with CAUTION!",
)
generate_pdf_on_publish = models.BooleanField(
"Generate PDF on save",
default=False,
help_text='⚠ Save latest content before checking this ⚠\nIf checked, the "Report PDF" field will be filled with a generated pdf. Otherwise, leave this unchecked and upload a pdf to the "Report PDF" field.',
)
revising = False
featured_sections = StreamField(
[
("featured", FeaturedReportSectionBlock(required=False, null=True)),
],
null=True,
blank=True,
use_json_field=True,
)
endnotes = StreamField(
[
("endnote", EndnoteBlock(required=False, null=True)),
],
null=True,
blank=True,
use_json_field=True,
)
report_url = StreamField(
[
("report_url", URLBlock(required=False, null=True)),
],
null=True,
blank=True,
use_json_field=True,
)
attachment = StreamField(
[
("attachment", DocumentChooserBlock(required=False, null=True)),
],
null=True,
blank=True,
use_json_field=True,
)
partner_logo = models.ForeignKey(
"home.CustomImage",
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name="+",
)
partner_logo_alt = models.TextField(
default="",
blank=True,
verbose_name="Partner logo alternative text",
help_text="A concise description of the image for users of assistive technology.",
)
theme_full_bleed = models.BooleanField(
default=False, help_text="Display bleed image on landing page"
)
content_panels = [
MultiFieldPanel(
[
TitleFieldPanel("title"),
FieldPanel("subheading"),
FieldPanel("date"),
FieldPanel("story_image"),
FieldPanel("story_image_alt"),
]
),
InlinePanel("authors", label=("Authors")),
InlinePanel("programs", label=("Programs")),
InlinePanel("subprograms", label=("Subprograms")),
InlinePanel("topics", label=("Topics")),
InlinePanel("location", label=("Locations")),
MultiFieldPanel(
[
FieldPanel("abstract"),
FieldPanel("featured_sections"),
FieldPanel("acknowledgements"),
]
),
]
sections_panels = [FieldPanel("sections")]
endnote_panels = [FieldPanel("endnotes")]
settings_panels = Post.settings_panels + [
FieldPanel("theme_full_bleed"),
FieldPanel("dataviz_src"),
]
promote_panels = Page.promote_panels + [
FieldPanel("story_excerpt"),
]
pdf_panels = [
MultiFieldPanel(
[
FieldPanel("source_word_doc"),
FieldPanel("overwrite_sections_on_save"),
],
heading="Word Doc Import",
),
MultiFieldPanel(
[
FieldPanel("generate_pdf_on_publish"),
FieldPanel("report_pdf"),
FieldPanel("attachment"),
],
heading="PDF Generation",
),
FieldPanel("partner_logo"),
FieldPanel("partner_logo_alt"),
]
edit_handler = TabbedInterface(
[
ObjectList(content_panels, heading="Landing"),
ObjectList(sections_panels, heading="Sections"),
ObjectList(endnote_panels, heading="Endnotes"),
ObjectList(promote_panels, heading="Promote"),
ObjectList(settings_panels, heading="Settings", classname="settings"),
ObjectList(pdf_panels, heading="PDF Publishing"),
]
)
search_fields = Post.search_fields + [
index.SearchField("sections"),
index.AutocompleteField("sections"),
]
def get_context(self, request):
context = super().get_context(request)
if getattr(request, "is_preview", False):
import newamericadotorg.api.report
revision = self.get_latest_revision_as_object()
report_data = (
newamericadotorg.api.report.serializers.ReportDetailSerializer(
revision
).data
)
context["initial_state"] = json.dumps(report_data)
return context
def save(self, *args, **kwargs):
super(Report, self).save(*args, **kwargs)
# TODO: apply demorgan's laws
if not self.overwrite_sections_on_save and not self.generate_pdf_on_publish:
self.revising = False
if (
not self.revising
and self.source_word_doc is not None
and self.overwrite_sections_on_save
):
self.revising = True
parse_pdf(self)
self.overwrite_sections_on_save = False
self.save_revision()
if not self.revising and self.generate_pdf_on_publish:
generate_pdf.apply_async(args=(self.id,))
# Extra views
@route(r"pdf/$")
def pdf(self, request):
if not self.report_pdf:
return self.pdf_render(request)
url = "https://s3.amazonaws.com/newamericadotorg/" + self.report_pdf.file.name
return redirect(url)
@route(r"pdf/render/$")
def pdf_render(self, request):
response = HttpResponse(content_type="application/pdf;")
response["Content-Disposition"] = "inline; filename=%s.pdf" % self.title
response["Content-Transfer-Encoding"] = "binary"
protocol = "https://" if request.is_secure() else "http://"
base_url = protocol + request.get_host()
contents = generate_report_contents(self)
authors = get_report_authors(self)
html = loader.get_template("report/pdf.html").render(
{"page": self, "contents": contents, "authors": authors}
)
write_pdf(response, html, base_url)
return response
@route(r"print/$")
def print(self, request):
contents = generate_report_contents(self)
authors = get_report_authors(self)
return render(
request,
"report/pdf.html",
context={"page": self, "contents": contents, "authors": authors},
)
@route(r"[a-zA-Z0-9_\.\-]*/$")
def section(self, request):
# Serve the whole report, subsection routing is handled by React
return self.render(request)
class Meta:
verbose_name = "Report"
class AllReportsHomePage(AbstractHomeContentPage):
"""
A page which inherits from the abstract Page model and
returns every Report in the Report model
for the organization wide Report omepage
"""
parent_page_types = ["home.Homepage"]
subpage_types = []
@property
def content_model(self):
return Report
class Meta:
verbose_name = "Reports Homepage"
class ReportsHomepage(AbstractContentPage):
"""
A page which inherits from the abstract Page model and
returns all Reports associated with a specific
Program or Subprogram
"""
parent_page_types = ["programs.Program", "programs.Subprogram", "programs.Project"]
subpage_types = ["Report"]
@property
def content_model(self):
return Report
class Meta:
verbose_name = "Reports Homepage"