models.py 14 KB

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