-
Notifications
You must be signed in to change notification settings - Fork 112
/
molecules.py
578 lines (495 loc) · 17.6 KB
/
molecules.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
from django.core.exceptions import ValidationError
from django.core.validators import RegexValidator
from django.utils.safestring import mark_safe
from wagtail import blocks
from wagtail.blocks.struct_block import StructBlockValidationError
from wagtail.images.blocks import ImageChooserBlock
from v1.atomic_elements import atoms
from v1.blocks import AnchorLink, HeadingBlock
from v1.feeds import get_appropriate_rss_feed_url_for_page
class InfoUnit(blocks.StructBlock):
image = atoms.ImageBasic(
required=False,
)
heading = HeadingBlock(required=False, default={"level": "h3"})
body = blocks.RichTextBlock(blank=True, required=False)
links = blocks.ListBlock(atoms.Hyperlink(), required=False)
class Meta:
icon = "image"
class TextIntroduction(blocks.StructBlock):
eyebrow = blocks.CharBlock(
required=False,
help_text=(
"Optional: Adds an H5 eyebrow above H1 heading text. "
"Only use in conjunction with heading."
),
label="Pre-heading",
)
heading = blocks.CharBlock(required=False)
intro = blocks.RichTextBlock(required=False)
body = blocks.RichTextBlock(required=False)
links = blocks.ListBlock(atoms.Hyperlink(required=False), required=False)
has_rule = blocks.BooleanBlock(
required=False,
label="Has bottom rule",
help_text=(
"Check this to add a horizontal rule line to bottom of "
"text introduction."
),
)
def clean(self, value):
cleaned = super().clean(value)
# Eyebrow requires a heading.
if cleaned.get("eyebrow") and not cleaned.get("heading"):
raise StructBlockValidationError(
block_errors={
"heading": ValidationError(
"Required if a pre-heading is entered."
)
}
)
return cleaned
class Meta:
icon = "title"
template = "v1/includes/molecules/text-introduction.html"
classname = "block--flush-top"
class AbstractHero(blocks.StructBlock):
heading = blocks.CharBlock(
required=False,
help_text=mark_safe(
"For guidelines on creating heroes, visit our "
'<a href="https://cfpb.github.io/design-system/patterns/heroes">'
"Design System</a>. "
"Character counts (including spaces) at largest breakpoint:"
'<ul class="help">'
" <li>• 41 characters max (one-line heading)</li>"
" <li>• 82 characters max (two-line heading)</li>"
"</ul>"
),
)
heading_continued = blocks.CharBlock(
required=False,
help_text=mark_safe(
"Use if the heading needs to break to a second line"
"at a specific point in the text."
),
)
body = blocks.RichTextBlock(
label="Sub-heading",
required=False,
help_text=mark_safe(
"Character counts (including spaces) at largest breakpoint:"
'<ul class="help">'
" <li>• 165-186 characters (after a one-line heading)</li>"
" <li>• 108-124 characters (after a two-line heading)</li>"
"</ul>"
),
)
image = ImageChooserBlock(
label="Large image",
required=False,
help_text=mark_safe(
"When saving illustrations, use a transparent background. "
),
)
small_image = ImageChooserBlock(
required=False,
help_text=mark_safe(
"<b>Optional.</b> Provides an alternate image for "
"small displays when using a photo or bleeding hero. "
"Not required for the standard illustration. "
),
)
background_color = blocks.CharBlock(
required=False,
help_text=mark_safe(
"Specify a hex value (including the # sign) from our "
'<a href="https://cfpb.github.io/design-system/foundation/color">'
"official color palette</a>."
),
)
is_white_text = blocks.BooleanBlock(
label="White text",
required=False,
help_text=mark_safe(
"<b>Optional.</b> Turns the hero text white. Useful if using "
"a dark background color or background image."
),
)
class Meta:
template = "v1/includes/molecules/hero.html"
classname = "block--flush-top block--flush-bottom"
icon = "image"
class Hero(AbstractHero):
is_overlay = blocks.BooleanBlock(
label="Photo",
required=False,
help_text=mark_safe(
"<b>Optional.</b> Uses the large image as a background under "
'the entire hero, creating the "Photo" style of hero (see '
'<a href="https://cfpb.github.io/design-system/patterns/heroes">'
"Design System</a> for details). When using this option, "
"make sure to specify a background color (above) for the "
"left/right margins that appear when screens are wider than "
"1200px and for the text section when the photo and text "
"stack at mobile sizes."
),
)
is_bleeding = blocks.BooleanBlock(
label="Bleed",
required=False,
help_text=mark_safe(
"<b>Optional.</b> Select if you want the illustration to bleed "
"vertically off the top and bottom of the hero space."
),
)
class JumboHeroValue(blocks.StructValue):
def is_jumbo(self):
return True
class JumboHero(AbstractHero):
is_50_50 = blocks.BooleanBlock(
label="50/50 layout",
required=False,
help_text=mark_safe(
"<b>Optional.</b> Set both the hero text and image to 50% width, "
"prevent the background color from appearing in the left/right "
"margins of the hero, and also add a border to the hero area. "
"Requires the uploaded large image to be 755px x 575px."
),
)
class Meta:
value_class = JumboHeroValue
class Notification(blocks.StructBlock):
type = blocks.ChoiceBlock(
choices=[
("information", "Information"),
("warning", "Warning"),
],
required=True,
default="warning",
)
message = blocks.CharBlock(
required=True, help_text="The main notification message to display."
)
explanation = blocks.TextBlock(
required=False,
help_text=(
"Explanation text appears below the message in smaller type."
),
)
links = blocks.ListBlock(
atoms.Hyperlink(required=False),
required=False,
help_text="Links appear on their own lines below the explanation.",
)
class Meta:
icon = "warning"
template = "v1/includes/molecules/notification.html"
class CallToAction(blocks.StructBlock):
slug_text = blocks.CharBlock(required=False)
paragraph_text = blocks.RichTextBlock(required=False)
button = atoms.Hyperlink()
class Meta:
template = "v1/includes/molecules/call-to-action.html"
icon = "grip"
label = "Call to action"
class ContactAddress(blocks.StructBlock):
label = blocks.CharBlock(required=False)
title = blocks.CharBlock(required=False)
street = blocks.CharBlock(required=False)
city = blocks.CharBlock(max_length=50, required=False)
state = blocks.CharBlock(max_length=25, required=False)
zip_code = blocks.CharBlock(max_length=15, required=False)
class Meta:
template = "v1/includes/molecules/contact-address.html"
icon = "mail"
label = "Address"
class ContactEmail(blocks.StructBlock):
emails = blocks.ListBlock(
blocks.StructBlock(
[
("url", blocks.EmailBlock(label="Email address")),
(
"text",
blocks.CharBlock(
required=False, label="Link text (optional)"
),
),
]
)
)
def clean(self, value):
cleaned = super().clean(value)
if not cleaned.get("emails"):
raise StructBlockValidationError(
block_errors={
"emails": ValidationError(
"At least one email address is required."
)
}
)
return cleaned
class Meta:
icon = "mail"
template = "v1/includes/molecules/contact-email.html"
label = "Email"
def phone_number_format_validator():
return RegexValidator(
regex=r"^\d*$",
message="Enter a numeric phone number, without punctuation.",
)
class ContactPhone(blocks.StructBlock):
fax = blocks.BooleanBlock(
default=False, required=False, label="Is this number a fax?"
)
phones = blocks.ListBlock(
blocks.StructBlock(
[
(
"number",
blocks.CharBlock(
max_length=15,
help_text=(
"Do not include spaces or dashes. "
"Ex. 8554112372"
),
validators=[phone_number_format_validator()],
),
),
("extension", blocks.CharBlock(max_length=4, required=False)),
(
"vanity",
blocks.CharBlock(
max_length=15,
required=False,
help_text="A phoneword version of the above number. "
"Include any formatting. Ex. (555) 222-CFPB",
),
),
(
"tty",
blocks.CharBlock(
max_length=15,
required=False,
label="TTY",
help_text=(
"Do not include spaces or dashes. "
"Ex. 8554112372"
),
validators=[phone_number_format_validator()],
),
),
(
"tty_ext",
blocks.CharBlock(
max_length=4, required=False, label="TTY Extension"
),
),
]
)
)
class Meta:
icon = "mail"
template = "v1/includes/molecules/contact-phone.html"
label = "Phone"
class ContactHyperlink(blocks.StructBlock):
url = blocks.URLBlock()
text = blocks.CharBlock(required=False)
class Meta:
icon = "link"
template = "v1/includes/molecules/contact-hyperlink.html"
label = "Hyperlink"
class ContentImage(blocks.StructBlock):
image = atoms.ImageBasic()
image_width = blocks.ChoiceBlock(
choices=[
("full", "Full width"),
(470, "470px"),
(270, "270px"),
(170, "170px"),
],
default="full",
)
image_position = blocks.ChoiceBlock(
choices=[("right", "right"), ("left", "left")],
default="right",
help_text="Does not apply if the image is full-width",
)
text = blocks.RichTextBlock(required=False, label="Caption")
is_bottom_rule = blocks.BooleanBlock(
required=False,
default=True,
label="Has bottom rule line",
help_text="Check to add a horizontal rule line to bottom of inset.",
)
class Meta:
icon = "image"
template = "v1/includes/molecules/content-image.html"
label = "Image"
class RelatedLinks(blocks.StructBlock):
heading = blocks.CharBlock(required=False)
paragraph = blocks.RichTextBlock(required=False)
links = blocks.ListBlock(atoms.Hyperlink())
class Meta:
icon = "grip"
template = "v1/includes/molecules/related-content.html"
label = "Related content"
class Quote(blocks.StructBlock):
body = blocks.TextBlock()
citation = blocks.TextBlock(required=False)
class Meta:
icon = "openquote"
template = "v1/includes/molecules/quote.html"
class RelatedMetadata(blocks.StructBlock):
slug = blocks.CharBlock(max_length=100)
content = blocks.StreamBlock(
[
(
"text",
blocks.StructBlock(
[
("heading", blocks.CharBlock(max_length=100)),
("blob", blocks.RichTextBlock()),
],
icon="pilcrow",
),
),
(
"list",
blocks.StructBlock(
[
("heading", blocks.CharBlock(max_length=100)),
("links", blocks.ListBlock(atoms.Hyperlink())),
],
icon="list-ul",
),
),
(
"date",
blocks.StructBlock(
[
("heading", blocks.CharBlock(max_length=100)),
("date", blocks.DateBlock()),
],
icon="date",
),
),
(
"topics",
blocks.StructBlock(
[
(
"heading",
blocks.CharBlock(max_length=100, default="Topics"),
),
(
"show_topics",
blocks.BooleanBlock(default=True, required=False),
),
],
icon="tag",
),
),
]
)
class Meta:
icon = "grip"
template = "v1/includes/molecules/related-metadata.html"
label = "Related metadata"
class RSSFeed(blocks.StaticBlock):
class Meta:
icon = "plus"
template = "v1/includes/molecules/rss-feed.html"
label = "RSS feed"
admin_text = mark_safe(
"<h3>RSS Feed</h3>"
"If this page or one of its ancestors provides an RSS feed, "
"this block renders a link to that feed. If not, this block "
"renders nothing."
)
def get_context(self, value, parent_context=None):
context = super().get_context(value, parent_context=parent_context)
page = context.get("page")
if page:
context["value"] = get_appropriate_rss_feed_url_for_page(page)
return context
class SocialMedia(blocks.StructBlock):
is_share_view = blocks.BooleanBlock(
default=True,
required=False,
label="Desired action: share this page",
help_text="If unchecked, social media icons will link users to "
"official CFPB accounts. Do not fill in any further fields.",
)
blurb = blocks.CharBlock(
required=False,
default="Look what I found on the CFPB's site!",
help_text="Sets the tweet text, email subject line, "
"and LinkedIn post text.",
)
twitter_text = blocks.CharBlock(
required=False,
max_length=100,
help_text="(Optional) Custom text for Twitter shares. If blank, "
"will default to value of blurb field above.",
)
twitter_related = blocks.CharBlock(
required=False,
help_text="(Optional) A comma-separated list of accounts related "
"to the content of the shared URL. Do not enter the "
' @ symbol. If blank, it will default to just "cfpb".',
)
twitter_hashtags = blocks.CharBlock(
required=False,
help_text="(Optional) A comma-separated list of hashtags to be "
"appended to default tweet text.",
)
twitter_lang = blocks.CharBlock(
required=False,
help_text="(Optional) Loads text components in the specified "
'language, if other than English. E.g., use "es" '
" for Spanish. "
"See https://dev.twitter.com/web/overview/languages "
"for a list of supported language codes.",
)
email_title = blocks.CharBlock(
required=False,
help_text="(Optional) Custom subject for email shares. If blank, "
"will default to value of blurb field above.",
)
email_text = blocks.CharBlock(
required=False,
help_text="(Optional) Custom text for email shares. If blank, will "
'default to "Check out this page from the CFPB".',
)
email_signature = blocks.CharBlock(
required=False,
help_text="(Optional) Adds a custom signature line to email shares. ",
)
linkedin_title = blocks.CharBlock(
required=False,
help_text="(Optional) Custom title for LinkedIn shares. If blank, "
"will default to value of blurb field above.",
)
linkedin_text = blocks.CharBlock(
required=False, help_text="(Optional) Custom text for LinkedIn shares."
)
class Meta:
icon = "site"
template = "v1/includes/molecules/social-media.html"
class ContentWithAnchor(blocks.StructBlock):
content_block = blocks.RichTextBlock()
anchor_link = AnchorLink()
class Meta:
icon = "edit"
template = "v1/includes/molecules/full-width-text-anchor.html"
class FeatureCard(blocks.StructBlock):
category = atoms.Hyperlink()
icon = blocks.TextBlock()
footer_label = blocks.TextBlock()
links = blocks.ListBlock(atoms.Hyperlink())
class Meta:
icon = "edit"
template = "v1/includes/molecules/feature-card.html"
class Media:
css = ["partially-styled-link.css"]