2
0

models.py 14 KB

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