models.py 17 KB

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