models.py 19 KB

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