2
0

models.py 17 KB

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