-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathmenu_link.py
More file actions
94 lines (75 loc) · 2.6 KB
/
menu_link.py
File metadata and controls
94 lines (75 loc) · 2.6 KB
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
from django.db import models
from wagtail import blocks
from wagtail.blocks.struct_block import StructValue
from wagtail.models import Page
from cms.dashboard.models import UKHSAPage
from cms.snippets.models.menu_builder import help_texts
BOLD: str = "bold"
ITALIC: str = "italic"
LINKS: str = "link"
AVAILABLE_RICH_TEXT_FEATURES: list[str] = [
BOLD,
ITALIC,
LINKS,
]
class MenuLink(blocks.StructBlock):
title = blocks.TextBlock(
required=True,
help_text=help_texts.MENU_LINK_HELP_TEXT,
)
body = blocks.RichTextBlock(
required=False,
help_text=help_texts.MENU_LINK_BODY,
features=AVAILABLE_RICH_TEXT_FEATURES,
)
page = blocks.PageChooserBlock(
"wagtailcore.Page",
related_name="+",
on_delete=models.CASCADE,
)
class Meta:
icon = "link"
def get_prep_value(self, value: StructValue) -> dict[str, str | int]:
"""Adds the `html_url` of each page to the returned value
Args:
`value`: The inbound enriched `StructValue`
containing the values associated with
this `MenuLink` object
Returns:
Dict containing the keys as dictated by the `MenuLink`.
With the addition of the injected `html_url` value
for the selected page.
"""
prep_value: dict[str, str | int] = super().get_prep_value(value=value)
page: Page = value["page"]
page: type[UKHSAPage] = page.specific
prep_value["html_url"] = page.full_url
return prep_value
class SimpleMenuLink(blocks.StructBlock):
title = blocks.TextBlock(
required=True,
help_text=help_texts.MENU_LINK_HELP_TEXT,
)
page = blocks.PageChooserBlock(
"wagtailcore.Page",
related_name="+",
on_delete=models.CASCADE,
)
class Meta:
icon = "link"
def get_prep_value(self, value: StructValue) -> dict[str, str | int]:
"""Adds the `html_url` of each page to the returned value
Args:
`value`: The inbound enriched `StructValue`
containing the values associated with
this `SimpleMenuLink` object
Returns:
Dict containing the keys as dictated by the
`SimpleMenuLink`. With the addition of the injected
`html_url` value for the selected page.
"""
prep_value: dict[str, str | int] = super().get_prep_value(value=value)
page: Page = value["page"]
page: type[UKHSAPage] = page.specific
prep_value["html_url"] = page.full_url
return prep_value