-
Notifications
You must be signed in to change notification settings - Fork 108
/
organisms.py
1064 lines (892 loc) · 33.7 KB
/
organisms.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from urllib.parse import urlencode
from django.apps import apps
from django.core.exceptions import ValidationError
from django.db.models import Q
from django.utils.safestring import mark_safe
from wagtail import blocks
from wagtail.blocks.struct_block import StructBlockValidationError
from wagtail.images import blocks as images_blocks
from wagtail.models import Page
from wagtail.snippets.blocks import SnippetChooserBlock
from wagtailmedia.blocks import AbstractMediaChooserBlock
from v1 import blocks as v1_blocks
from v1.atomic_elements import atoms, molecules
# Bring tables into this module to maintain import structure across the project
from v1.atomic_elements.tables import ( # noqa: F401
CaseDocketTable,
ConsumerReportingCompanyTable,
ContactUsTable,
Table,
)
from v1.util import ref
class Well(blocks.StructBlock):
content = v1_blocks.UnescapedRichTextBlock(required=False, label="Well")
class Meta:
icon = "placeholder"
template = "v1/includes/organisms/well.html"
class InfoUnitGroup(blocks.StructBlock):
format = blocks.ChoiceBlock(
choices=[
("50-50", "50/50"),
("33-33-33", "33/33/33"),
("25-75", "25/75"),
],
default="50-50",
label="Format",
help_text="Choose the number and width of info unit columns.",
)
heading = v1_blocks.HeadingBlock(required=False)
intro = blocks.RichTextBlock(
required=False,
)
link_image_and_heading = blocks.BooleanBlock(
default=True,
required=False,
help_text=(
"Check this to link all images and headings to the URL of "
"the first link in their unit's list, if there is a link."
),
)
has_top_rule_line = blocks.BooleanBlock(
default=False,
required=False,
help_text=(
"Check this to add a horizontal rule line to top of "
"info unit group."
),
)
lines_between_items = blocks.BooleanBlock(
default=False,
required=False,
label="Show rule lines between items",
help_text=(
"Check this to show horizontal rule lines between info " "units."
),
)
border_radius_image = blocks.ChoiceBlock(
choices=[
("none", "None"),
("rounded", "Rounded corners"),
("circle", "Circle"),
],
default="none",
required=False,
label="Border radius for images?",
help_text=(
"Adds a <em>border-radius</em> class to images in this "
"group, allowing for a rounded or circular border."
),
)
info_units = blocks.ListBlock(molecules.InfoUnit(), default=list())
def clean(self, value):
cleaned = super().clean(value)
# If 25/75, info units must have images.
if cleaned.get("format") == "25-75":
for unit in cleaned.get("info_units"):
if not unit["image"]["upload"]:
raise StructBlockValidationError(
block_errors={
"format": ValidationError(
"Info units must include images when using "
'the 25/75 format. Search for an "FPO" image '
"if you need a temporary placeholder."
)
}
)
return cleaned
class Meta:
icon = "list-ul"
template = "v1/includes/organisms/info-unit-group-2.html"
class PostPreviewSnapshot(blocks.StructBlock):
limit = blocks.CharBlock(
default="3",
label="Limit",
help_text="How many posts do you want to show?",
)
post_date_description = blocks.CharBlock(default="Published")
class Meta:
icon = "order"
template = "v1/includes/organisms/post-preview-snapshot.html"
class RelatedPosts(blocks.StructBlock):
limit = blocks.CharBlock(
default="3",
help_text=(
"This limit applies to EACH TYPE of post this module "
"retrieves, not the total number of retrieved posts."
),
)
show_heading = blocks.BooleanBlock(
required=False,
default=True,
label="Show Heading and Icon?",
help_text=(
"This toggles the heading and " "icon for the related types."
),
)
header_title = blocks.CharBlock(
default="Further reading", label="Slug Title"
)
relate_posts = blocks.BooleanBlock(
required=False, default=True, label="Blog Posts", editable=False
)
relate_newsroom = blocks.BooleanBlock(
required=False, default=True, label="Newsroom", editable=False
)
relate_events = blocks.BooleanBlock(
required=False, default=True, label="Events"
)
specific_categories = blocks.ListBlock(
blocks.ChoiceBlock(
choices=ref.related_posts_categories, required=False
),
required=False,
)
tag_filtering = blocks.ChoiceBlock(
choices=[
(
"any",
"Include related posts that match ANY topic tags on this page",
),
(
"all",
"Include related posts that match ALL topic tags on this page",
),
(
"ignore",
"IGNORE topic tags when selecting related posts",
),
],
required=True,
default="any",
)
alternate_view_more_url = blocks.CharBlock(
required=False,
label='Alternate "View more" URL',
help_text=(
'By default, the "View more" link will go to the Activity '
"Log, filtered based on the above parameters. Enter a URL "
"in this field to override that link destination."
),
)
def get_context(self, value, parent_context=None):
context = super().get_context(value, parent_context=parent_context)
page = context["page"]
request = context["request"]
context.update(
{
"posts": self.related_posts(page, value),
"view_more_url": (
value["alternate_view_more_url"]
or self.view_more_url(page, request)
),
}
)
return context
@staticmethod
def related_posts(page, value):
from v1.models.learn_page import AbstractFilterPage
def tag_set(related_page):
return set([tag.pk for tag in related_page.tags.all()])
def match_all_topic_tags(queryset, page_tags):
"""Return pages that share every one of the current page's tags."""
current_tag_set = set([tag.pk for tag in page_tags])
return [
page
for page in queryset
if current_tag_set.issubset(tag_set(page))
]
related_types = []
related_items = []
if value.get("relate_posts"):
related_types.append("blog")
if value.get("relate_newsroom"):
related_types.append("newsroom")
if value.get("relate_events"):
related_types.append("events")
if not related_types:
return related_items
tags = page.tags.all()
tag_filtering = value["tag_filtering"]
specific_categories = value["specific_categories"]
limit = int(value["limit"])
queryset = (
AbstractFilterPage.objects.live()
.exclude(id=page.id)
.filter(language=page.language)
.order_by("-date_published")
.distinct()
.specific()
)
for parent in related_types: # blog, newsroom or events
# Include children of this slug that match at least 1 tag
children = Page.objects.child_of_q(Page.objects.get(slug=parent))
if tag_filtering == "ignore":
filters = children
else:
filters = children & Q(("tags__in", tags))
if parent == "events":
# Include archived events matches
archive = Page.objects.get(slug="archive-past-events")
children = Page.objects.child_of_q(archive)
if tag_filtering == "ignore":
filters |= children
else:
filters |= children & Q(("tags__in", tags))
if specific_categories:
# Filter by any additional categories specified
categories = ref.get_appropriate_categories(
specific_categories=specific_categories, page_type=parent
)
if categories:
filters &= Q(("categories__name__in", categories))
related_queryset = queryset.filter(filters)
if tag_filtering == "all":
# By default, we need to match at least one tag
# If specified in the admin, change this to match ALL tags
related_queryset = match_all_topic_tags(related_queryset, tags)
if related_queryset:
related_items.append(
{
"title": parent.title(),
"icon": ref.get_category_icon(parent),
"posts": related_queryset[:limit],
}
)
return related_items
@staticmethod
def view_more_url(page, request):
"""Generate a URL to see more pages like this one.
This method generates a link to the Activity Log page (which must
exist and must have a unique site-wide slug of "activity-log") with
filters set by the tags assigned to this page, like this:
/activity-log/?topics=foo&topics=bar&topics=baz
If for some reason a page with slug "activity-log" does not exist,
this method will raise Page.DoesNotExist.
"""
activity_log = Page.objects.get(slug="activity-log")
url = activity_log.get_url(request)
tags = urlencode([("topics", tag) for tag in page.tags.slugs()])
if tags:
url += "?" + tags
return url
class Meta:
icon = "link"
template = "v1/includes/molecules/related-posts.html"
class MainContactInfo(blocks.StructBlock):
contact = SnippetChooserBlock("v1.Contact")
has_top_rule_line = blocks.BooleanBlock(
default=False,
required=False,
help_text="Add a horizontal rule line to top of contact block.",
)
class Meta:
icon = "wagtail"
template = "v1/includes/organisms/main-contact-info.html"
class SidebarContactInfo(MainContactInfo):
class Meta:
template = "v1/includes/organisms/sidebar-contact-info.html"
class Media:
css = ["sidebar-contact-info.css"]
class ModelBlock(blocks.StructBlock):
"""Abstract StructBlock that provides Django model instances to subclasses.
This class inherits from the standard Wagtail StructBlock but adds helper
methods that allow subclasses to dynamically render Django model instances.
This is useful if, for example, a widget needs to show a list of all model
instances meeting a certain criteria.
Subclasses must override the 'model' class attribute with the fully-
qualified name of the model to be used, for example 'my.app.Modelname'.
Subclasses may optionally override the 'filter_queryset' method to do
filtering on the model QuerySet.
Subclasses may optionally override either the class attributes 'ordering'
(providing a Django-style string or tuple of orderings to use) and 'limit'
(providing an integer to use to slice the model QuerySet), or provide
methods 'get_ordering' and 'get_limit' that do the same thing.
"""
model = None
ordering = None
limit = None
def get_queryset(self, value):
model_cls = apps.get_model(self.model)
qs = model_cls.objects.all()
qs = self.filter_queryset(qs, value)
ordering = self.get_ordering(value)
if ordering:
if isinstance(ordering, str):
ordering = (ordering,)
qs = qs.order_by(*ordering)
limit = self.get_limit(value)
if limit:
qs = qs[:limit]
return qs
def filter_queryset(self, qs, value):
return qs
def get_ordering(self, value):
return self.ordering
def get_limit(self, value):
return self.limit
class SimpleChart(blocks.StructBlock):
title = blocks.CharBlock(required=True)
subtitle = blocks.TextBlock(required=False)
description = blocks.TextBlock(
required=True, help_text="Accessible description of the chart content"
)
figure_number = blocks.CharBlock(required=False)
chart_type = blocks.ChoiceBlock(
choices=[
("bar", "Bar"),
("datetime", "Date/time"),
("line", "Line"),
("tilemap", "Tile grid map"),
],
default="datetime",
required=True,
)
data_source = blocks.TextBlock(
required=True,
help_text="URL of the chart's data source or an array of JSON data",
rows=2,
)
data_series = blocks.TextBlock(
required=False,
help_text="For charts pulling from a separate source file, "
"include a list of the column headers (from a CSV file) or "
"keys (from a JSON file) to include in the chart as "
' ["HEADER/KEY1", "HEADER/KEY2"]. '
"To change how the data is labeled in the chart, include the correct "
'labels with the format [{"key": "HEADER/KEY1", "label": "NEWLABEL"}, '
'{"key": "HEADER/KEY2", "label": "NEWLABEL2"}]',
)
show_all_series_by_default = blocks.BooleanBlock(
default=True,
required=False,
help_text="Uncheck this option to initially only show the first data "
" series in the chart. Leave checked to show all data "
" series by default. Users can always turn data series on "
" or off by interacting with the chart legend. ",
)
x_axis_source = blocks.TextBlock(
required=False,
help_text="The column header (CSV), key or data array (JSON) "
"to include as the source of x-axis values.",
)
transform = blocks.CharBlock(
required=False,
help_text="Name the javascript function in chart-hooks.js to run "
"on the provided data before handing it to the chart. Can also "
"provide '___'-separated arguments to this function "
"which are passed in as arguments 2 to n",
)
x_axis_label = blocks.CharBlock(required=False)
y_axis_label = blocks.CharBlock(required=False)
filters = blocks.TextBlock(
required=False,
help_text="If the chart needs the option for users to filter "
"the data shown, for example by date or geographic region, "
"provide the JSON objects to filter on, in the format "
' {key: "KEY", "label": "LABEL"}',
)
style_overrides = blocks.TextBlock(
required=False,
help_text="A JSON object with style overrides for the underlying "
"Highcharts chart. No object merging is done, nested objects should "
'be referenced with dot notation: {"tooltip.shape": "circle"}',
)
projected_months = blocks.IntegerBlock(
blank=True,
null=True,
min_value=0,
max_value=12,
help_text="A number to determine how many months of the "
"data are projected values",
required=False,
)
source_credits = blocks.CharBlock(
required=False, help_text="Attribution for the data source"
)
date_published = blocks.CharBlock(
required=False, help_text="When the underlying data was published"
)
download_text = blocks.CharBlock(
required=False,
help_text="Custom text for the chart download field. Required to "
"display a download link.",
)
download_file = blocks.CharBlock(
required=False,
help_text="Location of a file to download, if different from the "
"data source",
)
notes = blocks.TextBlock(
required=False, help_text="General chart information"
)
class Meta:
label = "Simple Chart"
icon = "image"
template = "v1/includes/organisms/simple-chart.html"
form_classname = "struct-block simple-chart-block"
class Media:
js = ["simple-chart/simple-chart.js"]
css = ["simple-chart.css"]
class FullWidthText(blocks.StreamBlock):
content = v1_blocks.UnescapedRichTextBlock(icon="edit")
content_with_anchor = molecules.ContentWithAnchor()
heading = v1_blocks.HeadingBlock(required=False)
image = molecules.ContentImage()
table = Table()
quote = molecules.Quote()
cta = molecules.CallToAction()
related_links = molecules.RelatedLinks()
reusable_text = v1_blocks.ReusableTextChooserBlock("v1.ReusableText")
reusable_notification = v1_blocks.ReusableNotificationChooserBlock(
"v1.ReusableNotification"
)
email_signup = v1_blocks.EmailSignUpChooserBlock()
well = Well()
class Meta:
icon = "edit"
template = "v1/includes/organisms/full-width-text.html"
class BaseExpandable(blocks.StructBlock):
label = blocks.CharBlock(required=False)
icon = blocks.CharBlock(required=False)
is_bordered = blocks.BooleanBlock(required=False)
is_midtone = blocks.BooleanBlock(required=False)
is_expanded = blocks.BooleanBlock(required=False)
is_expanded_padding = blocks.BooleanBlock(required=False)
class Meta:
icon = "list-ul"
template = "v1/includes/organisms/expandable.html"
form_classname = "struct-block base-expandable"
label = "Expandable"
class Media:
js = ["expandable.js"]
class Expandable(BaseExpandable):
content = blocks.StreamBlock(
[
("paragraph", blocks.RichTextBlock(required=False)),
("well", Well()),
("links", atoms.Hyperlink()),
("info_unit_group", InfoUnitGroup()),
],
blank=True,
)
class ExpandableGroup(blocks.StructBlock):
heading = blocks.CharBlock(
required=False,
help_text=mark_safe(
"Added as an <code><h3></code> at the top of this block. "
"Also adds a wrapping <code><div></code> whose "
"<code>id</code> attribute comes from a slugified version of this "
"heading, creating an anchor that can be used when linking to "
"this part of the page."
),
)
body = blocks.RichTextBlock(required=False)
is_accordion = blocks.BooleanBlock(required=False)
has_top_rule_line = blocks.BooleanBlock(
default=False,
required=False,
help_text=(
"Check this to add a horizontal rule line to top of "
"expandable group."
),
)
is_faq = blocks.BooleanBlock(
default=False,
required=False,
help_text=("Check this to add FAQ schema markup to expandables."),
label="Uses FAQ schema",
)
expandables = blocks.ListBlock(Expandable())
class Meta:
icon = "list-ul"
template = "v1/includes/organisms/expandable-group.html"
class Media:
js = ["expandable-group.js"]
class ItemIntroduction(blocks.StructBlock):
show_category = blocks.BooleanBlock(
required=False,
default=True,
help_text=(
"Whether to show the category or not "
"(category must be set in 'Configuration')."
),
)
heading = blocks.CharBlock(required=False)
paragraph = blocks.RichTextBlock(required=False)
date = blocks.DateBlock(required=False)
has_social = blocks.BooleanBlock(
required=False, help_text="Whether to show the share icons or not."
)
class Meta:
icon = "form"
template = "v1/includes/organisms/item-introduction.html"
classname = "block--flush-top"
class FilterableList(BaseExpandable):
filter_by_keyword = blocks.BooleanBlock(
default=True,
required=False,
help_text='Whether to include a "Search by keyword" filter '
"in the filter controls.",
)
filter_by_category = blocks.BooleanBlock(
default=True,
required=False,
help_text='Whether to include a "Category" filter '
"in the filter controls.",
)
category_choices = blocks.ChoiceBlock(
choices=ref.filterable_list_page_types, required=False
)
filter_by_topics = blocks.BooleanBlock(
required=False,
help_text=(
'Whether to include a "Topics" filter in the filter controls'
),
)
filter_by_enforcement_statuses = blocks.BooleanBlock(
default=False,
required=False,
help_text='Whether to include a "Status" filter '
"in the filter controls. "
"Only enable if using on an "
"enforcement actions filterable list.",
)
filter_by_enforcement_products = blocks.BooleanBlock(
default=False,
required=False,
help_text='Whether to include a "Product" filter '
"in the filter controls. "
"Only enable if using on an "
"enforcement actions filterable list.",
)
filter_by_language = blocks.BooleanBlock(
default=False,
required=False,
help_text='Whether to include a "Language" filter '
"in the filter controls."
"Only enable if there are non-english "
"filterable results available.",
)
filter_by_date_range = blocks.BooleanBlock(
default=True,
required=False,
help_text='Whether to include a set of "Date range" filters '
"in the filter controls.",
)
class Meta:
label = "Filterable List"
icon = "form"
template = "v1/includes/organisms/filterable-list-controls.html"
class Media:
js = ["filterable-list-controls.js"]
class VideoPlayerStructValue(blocks.StructValue):
@property
def thumbnail_url(self):
thumbnail_image = self.get("thumbnail_image")
if thumbnail_image:
return thumbnail_image.get_rendition("original").url
class VideoPlayer(blocks.StructBlock):
YOUTUBE_ID_HELP_TEXT = (
"Enter the YouTube video ID, which is located at the end of the video "
'URL, after "v=". For example, the video ID for '
"https://www.youtube.com/watch?v=1V0Ax9OIc84 is 1V0Ax9OIc84."
)
video_id = blocks.RegexBlock(
label="YouTube video ID",
# Set required=False to allow for non-required VideoPlayers.
# See https://github.com/wagtail/wagtail/issues/2665.
required=False,
# This is a reasonable but not official regex for YouTube video IDs.
# https://webapps.stackexchange.com/a/54448
regex=r"^[\w-]{11}$",
error_messages={
"invalid": "The YouTube video ID is in the wrong format.",
},
help_text=YOUTUBE_ID_HELP_TEXT,
)
thumbnail_image = images_blocks.ImageChooserBlock(
required=False,
help_text=mark_safe(
"Optional thumbnail image to show before and after the video "
"plays. If the thumbnail image is not set here, the video player "
"will default to showing the thumbnail that was set in (or "
"automatically chosen by) YouTube."
),
)
def clean(self, value):
cleaned = super().clean(value)
errors = {}
if not cleaned["video_id"]:
if getattr(self.meta, "required", True):
errors["video_id"] = ValidationError("This field is required.")
elif cleaned["thumbnail_image"]:
errors["thumbnail_image"] = ValidationError(
"This field should not be used if YouTube video ID is "
"not set."
)
if errors:
raise StructBlockValidationError(block_errors=errors)
return cleaned
class Meta:
icon = "media"
template = "v1/includes/organisms/video-player.html"
value_class = VideoPlayerStructValue
class Media:
js = ["video-player.js", "https://www.youtube.com/iframe_api"]
class AudioPlayer(blocks.StructBlock):
heading = v1_blocks.HeadingBlock(required=False)
body = blocks.RichTextBlock(required=False)
audio_file = AbstractMediaChooserBlock(
help_text=mark_safe(
"Spoken word audio files should be in MP3 format with a 44.1 kHz "
"sample rate, 96 kbps (CBR) bitrate, in mono. See "
'<a href="https://help.libsynsupport.com/hc/en-us/articles/'
'360040796152-Recommended-Audio-File-Formats-Encoding">Libsyn’s '
"guidance</a> for details. Note that the thumbnail and tag fields "
"will not be used for audio files."
)
)
additional_details = blocks.RichTextBlock(
required=False,
help_text=(
"If you have anything you want to appear below the audio player, "
"such as a download link, put it in this field."
),
)
class Meta:
icon = "media"
template = "v1/includes/organisms/audio-player.html"
class Media:
js = ["audio-player.js"]
class FeaturedContentStructValue(blocks.StructValue):
@property
def links(self):
# We want to pass a single list of links to the template when the
# FeaturedContent organism is rendered. So we consolidate any links
# that have been specified: the post link and any other links. We
# also normalize them each to have URL, text,
# and (optionally) aria-label attributes.
links = []
# We want to pass the post URL into the template so that it can be
# rendered without needing to call back to any Wagtail template tags.
post = self.get("post")
if post and self.get("show_post_link"):
links.append(
{
# Unfortunately, we don't have access to the request
# context here, so we can't do post.get_url(request).
"url": post.url,
"text": self.get("post_link_text") or post.title,
}
)
# Normalize any child Hyperlink atoms and filter empty links.
for hyperlink in self.get("links") or []:
url = hyperlink.get("url")
text = hyperlink.get("text")
aria_label = hyperlink.get("aria_label")
if url and text:
links.append(
{"url": url, "text": text, "aria_label": aria_label}
)
return links
class FeaturedContent(blocks.StructBlock):
heading = blocks.CharBlock()
body = blocks.TextBlock(help_text="Line breaks will be ignored.")
post = blocks.PageChooserBlock(required=False)
show_post_link = blocks.BooleanBlock(
required=False, label="Render post link?"
)
post_link_text = blocks.CharBlock(required=False)
image = atoms.ImageBasic(required=False)
links = blocks.ListBlock(
atoms.Hyperlink(required=False), label="Additional Links"
)
video = VideoPlayer(required=False)
class Meta:
template = "v1/includes/organisms/featured-content.html"
icon = "doc-full-inverse"
label = "Featured Content"
classname = "block--flush"
value_class = FeaturedContentStructValue
class Media:
js = ["featured-content-module.js"]
class ChartBlock(blocks.StructBlock):
title = blocks.CharBlock(required=True)
# todo: make radio buttons
chart_type = blocks.ChoiceBlock(
choices=[
("bar", "Bar | % y-axis values"),
("line", "Line | millions/billions y-axis values"),
("line-index", "Line-Index | integer y-axis values"),
("tile_map", "Tile Map | grid-like USA map"),
],
required=True,
)
color_scheme = blocks.ChoiceBlock(
choices=[
("blue", "Blue"),
("gold", "Gold"),
("green", "Green"),
("navy", "Navy"),
("neutral", "Neutral"),
("purple", "Purple"),
("teal", "Teal"),
],
required=False,
help_text="Chart's color scheme. See "
'"https://github.com/cfpb/cfpb-chart-builder'
'#createchart-options-".',
)
data_source = blocks.CharBlock(
required=True,
help_text="Location of the chart's data source relative to "
'"https://files.consumerfinance.gov/data/". For example,'
'"consumer-credit-trends/auto-loans/num_data_AUT.csv".',
)
date_published = blocks.DateBlock(
help_text="Automatically generated when CCT cron job runs"
)
description = blocks.CharBlock(
required=True,
help_text="Briefly summarize the chart for visually impaired users.",
)
has_top_rule_line = blocks.BooleanBlock(
default=False,
required=False,
help_text=(
"Check this to add a horizontal rule line to top of "
"chart block."
),
)
last_updated_projected_data = blocks.DateBlock(
help_text="Month of latest entry in dataset"
)
metadata = blocks.CharBlock(
required=False,
help_text="Optional metadata for the chart to use. "
'For example, with CCT this would be the chart\'s "group".',
)
note = blocks.CharBlock(
required=False,
help_text="Text to display as a footnote. For example, "
'"Data from the last six months are not final."',
)
y_axis_label = blocks.CharBlock(
required=False,
help_text="Custom y-axis label. "
"NOTE: Line-Index chart y-axis "
"is not overridable with this field!",
)
class Meta:
label = "Chart Block"
icon = "image"
template = "v1/includes/organisms/chart.html"
class Media:
js = ["chart.js"]
css = ["chart.css"]
class MortgageChartBlock(blocks.StructBlock):
content_block = blocks.RichTextBlock()
title = blocks.CharBlock(required=True, classname="title")
description = blocks.CharBlock(
required=False, help_text="Chart summary for visually impaired users."
)
note = blocks.CharBlock(
required=False, help_text='Text for "Note" section of footnotes.'
)
has_top_rule_line = blocks.BooleanBlock(
default=False,
required=False,
help_text=(
"Check this to add a horizontal rule line to top of "
"chart block."
),
)
class Meta:
label = "Mortgage Chart Block"
icon = "image"
template = "v1/includes/organisms/mortgage-chart.html"
class Media:
js = ["mortgage-performance-trends.js"]
css = ["mortgage-performance-trends.css", "chart.css"]
class MortgageMapBlock(MortgageChartBlock):
class Meta:
label = "Mortgage Map Block"
icon = "image"
template = "v1/includes/organisms/mortgage-map.html"
class Media:
js = ["mortgage-performance-trends.js"]
css = ["mortgage-performance-trends.css"]
class DataSnapshot(blocks.StructBlock):
"""A basic Data Snapshot object."""
# Market key corresponds to market short name for lookup
market_key = blocks.CharBlock(
max_length=20, required=True, help_text="Market identifier, e.g. AUT"
)
num_originations = blocks.CharBlock(
max_length=20, help_text="Number of originations, e.g. 1.2 million"
)
value_originations = blocks.CharBlock(
max_length=20,
help_text="Total dollar value of originations, e.g. $3.4 billion",
)
year_over_year_change = blocks.CharBlock(
max_length=20, help_text="Percentage change, e.g. 5.6% increase"
)