models.py 12 KB

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