2
0

models.py 19 KB

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