models.py 14 KB

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