models.py 14 KB

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