2
0

models.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. from django.conf import settings
  2. from django.contrib.contenttypes.fields import GenericRelation
  3. from django.db import models
  4. from django.utils.translation import gettext as _
  5. from modelcluster.fields import ParentalKey
  6. from modelcluster.models import ClusterableModel
  7. from wagtail.admin.panels import (
  8. FieldPanel,
  9. FieldRowPanel,
  10. HelpPanel,
  11. InlinePanel,
  12. MultiFieldPanel,
  13. PublishingPanel,
  14. )
  15. from wagtail.api import APIField
  16. from wagtail.contrib.forms.models import AbstractEmailForm, AbstractFormField
  17. from wagtail.contrib.forms.panels import FormSubmissionsPanel
  18. from wagtail.contrib.settings.models import (
  19. BaseGenericSetting,
  20. BaseSiteSetting,
  21. register_setting,
  22. )
  23. from wagtail.fields import RichTextField, StreamField
  24. from wagtail.images.models import Image
  25. from wagtail.models import (
  26. Collection,
  27. DraftStateMixin,
  28. LockableMixin,
  29. Page,
  30. PreviewableMixin,
  31. RevisionMixin,
  32. Task,
  33. TaskState,
  34. TranslatableMixin,
  35. WorkflowMixin,
  36. )
  37. from wagtail.search import index
  38. from bakerydemo.headless import CustomHeadlessMixin
  39. from bakerydemo.serializers import RichTextSerializer
  40. from .blocks import BaseStreamBlock
  41. # Allow filtering by collection
  42. Image.api_fields = [APIField("collection")]
  43. class Person(
  44. WorkflowMixin,
  45. DraftStateMixin,
  46. LockableMixin,
  47. RevisionMixin,
  48. PreviewableMixin,
  49. index.Indexed,
  50. ClusterableModel,
  51. ):
  52. """
  53. A Django model to store Person objects.
  54. It is registered using `register_snippet` as a function in wagtail_hooks.py
  55. to allow it to have a menu item within a custom menu item group.
  56. `Person` uses the `ClusterableModel`, which allows the relationship with
  57. another model to be stored locally to the 'parent' model (e.g. a PageModel)
  58. until the parent is explicitly saved. This allows the editor to use the
  59. 'Preview' button, to preview the content, without saving the relationships
  60. to the database.
  61. https://github.com/wagtail/django-modelcluster
  62. """
  63. first_name = models.CharField("First name", max_length=254)
  64. last_name = models.CharField("Last name", max_length=254)
  65. job_title = models.CharField("Job title", max_length=254)
  66. image = models.ForeignKey(
  67. "wagtailimages.Image",
  68. null=True,
  69. blank=True,
  70. on_delete=models.SET_NULL,
  71. related_name="+",
  72. )
  73. workflow_states = GenericRelation(
  74. "wagtailcore.WorkflowState",
  75. content_type_field="base_content_type",
  76. object_id_field="object_id",
  77. related_query_name="person",
  78. for_concrete_model=False,
  79. )
  80. revisions = GenericRelation(
  81. "wagtailcore.Revision",
  82. content_type_field="base_content_type",
  83. object_id_field="object_id",
  84. related_query_name="person",
  85. for_concrete_model=False,
  86. )
  87. panels = [
  88. MultiFieldPanel(
  89. [
  90. FieldRowPanel(
  91. [
  92. FieldPanel("first_name"),
  93. FieldPanel("last_name"),
  94. ]
  95. )
  96. ],
  97. "Name",
  98. ),
  99. FieldPanel("job_title"),
  100. FieldPanel("image"),
  101. PublishingPanel(),
  102. ]
  103. search_fields = [
  104. index.SearchField("first_name"),
  105. index.SearchField("last_name"),
  106. index.FilterField("job_title"),
  107. index.AutocompleteField("first_name"),
  108. index.AutocompleteField("last_name"),
  109. ]
  110. api_fields = [
  111. APIField("first_name"),
  112. APIField("last_name"),
  113. APIField("job_title"),
  114. APIField("image"),
  115. ]
  116. @property
  117. def thumb_image(self):
  118. # Returns an empty string if there is no profile pic or the rendition
  119. # file can't be found.
  120. try:
  121. return self.image.get_rendition("fill-50x50").img_tag()
  122. except: # noqa: E722 FIXME: remove bare 'except:'
  123. return ""
  124. @property
  125. def preview_modes(self):
  126. return PreviewableMixin.DEFAULT_PREVIEW_MODES + [("blog_post", _("Blog post"))]
  127. def __str__(self):
  128. return "{} {}".format(self.first_name, self.last_name)
  129. def get_preview_template(self, request, mode_name):
  130. from bakerydemo.blog.models import BlogPage
  131. if mode_name == "blog_post":
  132. return BlogPage.template
  133. return "base/preview/person.html"
  134. def get_preview_context(self, request, mode_name):
  135. from bakerydemo.blog.models import BlogPage
  136. context = super().get_preview_context(request, mode_name)
  137. if mode_name == self.default_preview_mode:
  138. return context
  139. page = BlogPage.objects.filter(blog_person_relationship__person=self).first()
  140. if page:
  141. # Use the page authored by this person if available,
  142. # and replace the instance from the database with the edited instance
  143. page.authors = [
  144. self if author.pk == self.pk else author for author in page.authors()
  145. ]
  146. # The authors() method only shows live authors, so make sure the instance
  147. # is included even if it's not live as this is just a preview
  148. if not self.live:
  149. page.authors.append(self)
  150. else:
  151. # Otherwise, get the first page and simulate the person as the author
  152. page = BlogPage.objects.first()
  153. page.authors = [self]
  154. context["page"] = page
  155. return context
  156. class Meta:
  157. verbose_name = "person"
  158. verbose_name_plural = "people"
  159. class FooterText(
  160. DraftStateMixin,
  161. RevisionMixin,
  162. PreviewableMixin,
  163. TranslatableMixin,
  164. models.Model,
  165. ):
  166. """
  167. This provides editable text for the site footer. Again it is registered
  168. using `register_snippet` as a function in wagtail_hooks.py to be grouped
  169. together with the Person model inside the same main menu item. It is made
  170. accessible on the template via a template tag defined in base/templatetags/
  171. navigation_tags.py
  172. """
  173. body = RichTextField()
  174. revisions = GenericRelation(
  175. "wagtailcore.Revision",
  176. content_type_field="base_content_type",
  177. object_id_field="object_id",
  178. related_query_name="footer_text",
  179. for_concrete_model=False,
  180. )
  181. panels = [
  182. FieldPanel("body"),
  183. PublishingPanel(),
  184. ]
  185. api_fields = [
  186. APIField("body", serializer=RichTextSerializer()),
  187. ]
  188. def __str__(self):
  189. return "Footer text"
  190. def get_preview_template(self, request, mode_name):
  191. return "base.html"
  192. def get_preview_context(self, request, mode_name):
  193. return {"footer_text": self.body}
  194. class Meta(TranslatableMixin.Meta):
  195. verbose_name = "footer text"
  196. verbose_name_plural = "footer text"
  197. class StandardPage(CustomHeadlessMixin, Page):
  198. """
  199. A generic content page. On this demo site we use it for an about page but
  200. it could be used for any type of page content that only needs a title,
  201. image, introduction and body field
  202. """
  203. introduction = models.TextField(help_text="Text to describe the page", blank=True)
  204. image = models.ForeignKey(
  205. "wagtailimages.Image",
  206. null=True,
  207. blank=True,
  208. on_delete=models.SET_NULL,
  209. related_name="+",
  210. help_text="Landscape mode only; horizontal width between 1000px and 3000px.",
  211. )
  212. body = StreamField(
  213. BaseStreamBlock(), verbose_name="Page body", blank=True, use_json_field=True
  214. )
  215. content_panels = Page.content_panels + [
  216. FieldPanel("introduction"),
  217. FieldPanel("body"),
  218. FieldPanel("image"),
  219. ]
  220. api_fields = [
  221. APIField("introduction"),
  222. APIField("image"),
  223. APIField("body"),
  224. ]
  225. class HomePage(CustomHeadlessMixin, Page):
  226. """
  227. The Home Page. This looks slightly more complicated than it is. You can
  228. see if you visit your site and edit the homepage that it is split between
  229. a:
  230. - Hero area
  231. - Body area
  232. - A promotional area
  233. - Moveable featured site sections
  234. """
  235. # Hero section of HomePage
  236. image = models.ForeignKey(
  237. "wagtailimages.Image",
  238. null=True,
  239. blank=True,
  240. on_delete=models.SET_NULL,
  241. related_name="+",
  242. help_text="Homepage image",
  243. )
  244. hero_text = models.CharField(
  245. max_length=255, help_text="Write an introduction for the bakery"
  246. )
  247. hero_cta = models.CharField(
  248. verbose_name="Hero CTA",
  249. max_length=255,
  250. help_text="Text to display on Call to Action",
  251. )
  252. hero_cta_link = models.ForeignKey(
  253. "wagtailcore.Page",
  254. null=True,
  255. blank=True,
  256. on_delete=models.SET_NULL,
  257. related_name="+",
  258. verbose_name="Hero CTA link",
  259. help_text="Choose a page to link to for the Call to Action",
  260. )
  261. # Body section of the HomePage
  262. body = StreamField(
  263. BaseStreamBlock(),
  264. verbose_name="Home content block",
  265. blank=True,
  266. use_json_field=True,
  267. )
  268. # Promo section of the HomePage
  269. promo_image = models.ForeignKey(
  270. "wagtailimages.Image",
  271. null=True,
  272. blank=True,
  273. on_delete=models.SET_NULL,
  274. related_name="+",
  275. help_text="Promo image",
  276. )
  277. promo_title = models.CharField(
  278. blank=True, max_length=255, help_text="Title to display above the promo copy"
  279. )
  280. promo_text = RichTextField(
  281. null=True, blank=True, max_length=1000, help_text="Write some promotional copy"
  282. )
  283. # Featured sections on the HomePage
  284. # You will see on templates/base/home_page.html that these are treated
  285. # in different ways, and displayed in different areas of the page.
  286. # Each list their children items that we access via the children function
  287. # that we define on the individual Page models e.g. BlogIndexPage
  288. featured_section_1_title = models.CharField(
  289. blank=True, max_length=255, help_text="Title to display above the featured section 1"
  290. )
  291. featured_section_1 = models.ForeignKey(
  292. "wagtailcore.Page",
  293. null=True,
  294. blank=True,
  295. on_delete=models.SET_NULL,
  296. related_name="+",
  297. help_text="First featured section for the homepage. Will display up to "
  298. "three child items.",
  299. verbose_name="Featured section 1",
  300. )
  301. featured_section_2_title = models.CharField(
  302. blank=True, max_length=255, help_text="Title to display above the featured section 2"
  303. )
  304. featured_section_2 = models.ForeignKey(
  305. "wagtailcore.Page",
  306. null=True,
  307. blank=True,
  308. on_delete=models.SET_NULL,
  309. related_name="+",
  310. help_text="Second featured section for the homepage. Will display up to "
  311. "three child items.",
  312. verbose_name="Featured section 2",
  313. )
  314. featured_section_3_title = models.CharField(
  315. blank=True, max_length=255, help_text="Title to display above the featured section 3"
  316. )
  317. featured_section_3 = models.ForeignKey(
  318. "wagtailcore.Page",
  319. null=True,
  320. blank=True,
  321. on_delete=models.SET_NULL,
  322. related_name="+",
  323. help_text="Third featured section for the homepage. Will display up to "
  324. "six child items.",
  325. verbose_name="Featured section 3",
  326. )
  327. content_panels = Page.content_panels + [
  328. MultiFieldPanel(
  329. [
  330. FieldPanel("image"),
  331. FieldPanel("hero_text"),
  332. MultiFieldPanel(
  333. [
  334. FieldPanel("hero_cta"),
  335. FieldPanel("hero_cta_link"),
  336. ]
  337. ),
  338. ],
  339. heading="Hero section",
  340. ),
  341. HelpPanel("This is a help panel"),
  342. MultiFieldPanel(
  343. [
  344. FieldPanel("promo_image"),
  345. FieldPanel("promo_title"),
  346. FieldPanel("promo_text"),
  347. ],
  348. heading="Promo section",
  349. help_text="This is just a help text",
  350. ),
  351. FieldPanel("body"),
  352. MultiFieldPanel(
  353. [
  354. MultiFieldPanel(
  355. [
  356. FieldPanel("featured_section_1_title"),
  357. FieldPanel("featured_section_1"),
  358. ]
  359. ),
  360. MultiFieldPanel(
  361. [
  362. FieldPanel("featured_section_2_title"),
  363. FieldPanel("featured_section_2"),
  364. ]
  365. ),
  366. MultiFieldPanel(
  367. [
  368. FieldPanel("featured_section_3_title"),
  369. FieldPanel("featured_section_3"),
  370. ]
  371. ),
  372. ],
  373. heading="Featured homepage sections",
  374. ),
  375. ]
  376. api_fields = [
  377. APIField("image"),
  378. APIField("hero_text"),
  379. APIField("hero_cta"),
  380. APIField("hero_cta_link"),
  381. APIField("body"),
  382. APIField("promo_image"),
  383. APIField("promo_title"),
  384. APIField("promo_text", serializer=RichTextSerializer()),
  385. APIField("featured_section_1_title"),
  386. APIField("featured_section_1"),
  387. APIField("featured_section_2_title"),
  388. APIField("featured_section_2"),
  389. APIField("featured_section_3_title"),
  390. APIField("featured_section_3"),
  391. ]
  392. def __str__(self):
  393. return self.title
  394. class GalleryPage(CustomHeadlessMixin, Page):
  395. """
  396. This is a page to list locations from the selected Collection. We use a Q
  397. object to list any Collection created (/admin/collections/) even if they
  398. contain no items. In this demo we use it for a GalleryPage,
  399. and is intended to show the extensibility of this aspect of Wagtail
  400. """
  401. introduction = models.TextField(help_text="Text to describe the page", blank=True)
  402. image = models.ForeignKey(
  403. "wagtailimages.Image",
  404. null=True,
  405. blank=True,
  406. on_delete=models.SET_NULL,
  407. related_name="+",
  408. help_text="Landscape mode only; horizontal width between 1000px and 3000px.",
  409. )
  410. body = StreamField(
  411. BaseStreamBlock(), verbose_name="Page body", blank=True, use_json_field=True
  412. )
  413. collection = models.ForeignKey(
  414. Collection,
  415. limit_choices_to=~models.Q(name__in=["Root"]),
  416. null=True,
  417. blank=True,
  418. on_delete=models.SET_NULL,
  419. help_text="Select the image collection for this gallery.",
  420. )
  421. content_panels = Page.content_panels + [
  422. FieldPanel("introduction"),
  423. FieldPanel("body"),
  424. FieldPanel("image"),
  425. FieldPanel("collection"),
  426. ]
  427. # Defining what content type can sit under the parent. Since it's a blank
  428. # array no subpage can be added
  429. subpage_types = []
  430. api_fields = [
  431. APIField("introduction"),
  432. APIField("image"),
  433. APIField("body"),
  434. APIField("collection"),
  435. ]
  436. class FormField(AbstractFormField):
  437. """
  438. Wagtailforms is a module to introduce simple forms on a Wagtail site. It
  439. isn't intended as a replacement to Django's form support but as a quick way
  440. to generate a general purpose data-collection form or contact form
  441. without having to write code. We use it on the site for a contact form. You
  442. can read more about Wagtail forms at:
  443. https://docs.wagtail.org/en/stable/reference/contrib/forms/index.html
  444. """
  445. page = ParentalKey("FormPage", related_name="form_fields", on_delete=models.CASCADE)
  446. class FormPage(CustomHeadlessMixin, AbstractEmailForm):
  447. image = models.ForeignKey(
  448. "wagtailimages.Image",
  449. null=True,
  450. blank=True,
  451. on_delete=models.SET_NULL,
  452. related_name="+",
  453. )
  454. body = StreamField(BaseStreamBlock(), use_json_field=True)
  455. thank_you_text = RichTextField(blank=True)
  456. # Note how we include the FormField object via an InlinePanel using the
  457. # related_name value
  458. content_panels = AbstractEmailForm.content_panels + [
  459. FormSubmissionsPanel(),
  460. FieldPanel("image"),
  461. FieldPanel("body"),
  462. InlinePanel("form_fields", heading="Form fields", label="Field"),
  463. FieldPanel("thank_you_text"),
  464. MultiFieldPanel(
  465. [
  466. FieldRowPanel(
  467. [
  468. FieldPanel("from_address"),
  469. FieldPanel("to_address"),
  470. ]
  471. ),
  472. FieldPanel("subject"),
  473. ],
  474. "Email",
  475. ),
  476. ]
  477. api_fields = [
  478. APIField("form_fields"),
  479. APIField("from_address"),
  480. APIField("to_address"),
  481. APIField("subject"),
  482. APIField("image"),
  483. APIField("body"),
  484. APIField("thank_you_text", serializer=RichTextSerializer()),
  485. ]
  486. @register_setting(icon="cog")
  487. class GenericSettings(ClusterableModel, BaseGenericSetting):
  488. mastodon_url = models.URLField(verbose_name="Mastodon URL", blank=True)
  489. github_url = models.URLField(verbose_name="GitHub URL", blank=True)
  490. organisation_url = models.URLField(verbose_name="Organisation URL", blank=True)
  491. panels = [
  492. MultiFieldPanel(
  493. [
  494. FieldPanel("github_url"),
  495. FieldPanel("mastodon_url"),
  496. FieldPanel("organisation_url"),
  497. ],
  498. "Social settings",
  499. )
  500. ]
  501. @register_setting(icon="site")
  502. class SiteSettings(BaseSiteSetting):
  503. title_suffix = models.CharField(
  504. verbose_name="Title suffix",
  505. max_length=255,
  506. help_text="The suffix for the title meta tag e.g. ' | The Wagtail Bakery'",
  507. default="The Wagtail Bakery",
  508. )
  509. panels = [
  510. FieldPanel("title_suffix"),
  511. ]
  512. class UserApprovalTaskState(TaskState):
  513. pass
  514. class UserApprovalTask(Task):
  515. """
  516. Based on https://docs.wagtail.org/en/stable/extending/custom_tasks.html.
  517. """
  518. user = models.ForeignKey(
  519. settings.AUTH_USER_MODEL, on_delete=models.SET_NULL, null=True, blank=False
  520. )
  521. admin_form_fields = Task.admin_form_fields + ["user"]
  522. task_state_class = UserApprovalTaskState
  523. # prevent editing of `user` after the task is created
  524. # by default, this attribute contains the 'name' field to prevent tasks from being renamed
  525. admin_form_readonly_on_edit_fields = Task.admin_form_readonly_on_edit_fields + [
  526. "user"
  527. ]
  528. def user_can_access_editor(self, page, user):
  529. return user == self.user
  530. def page_locked_for_user(self, page, user):
  531. return user != self.user
  532. def get_actions(self, page, user):
  533. if user == self.user:
  534. return [
  535. ("approve", "Approve", False),
  536. ("reject", "Reject", False),
  537. ("cancel", "Cancel", False),
  538. ]
  539. else:
  540. return []
  541. def on_action(self, task_state, user, action_name, **kwargs):
  542. if action_name == "cancel":
  543. return task_state.workflow_state.cancel(user=user)
  544. else:
  545. return super().on_action(task_state, user, action_name, **kwargs)
  546. def get_task_states_user_can_moderate(self, user, **kwargs):
  547. if user == self.user:
  548. # get all task states linked to the (base class of) current task
  549. return TaskState.objects.filter(
  550. status=TaskState.STATUS_IN_PROGRESS, task=self.task_ptr
  551. )
  552. else:
  553. return TaskState.objects.none()
  554. @classmethod
  555. def get_description(cls):
  556. return _("Only a specific user can approve this task")