models.py 13 KB

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