models.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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.wagtailadmin.edit_handlers import (
  6. FieldPanel, FieldRowPanel, InlinePanel, MultiFieldPanel, PageChooserPanel, StreamFieldPanel,
  7. )
  8. from wagtail.wagtailcore.fields import RichTextField, StreamField
  9. from wagtail.wagtailcore.models import Collection, Page
  10. from wagtail.wagtailforms.models import AbstractEmailForm, AbstractFormField
  11. from wagtail.wagtailimages.edit_handlers import ImageChooserPanel
  12. from wagtail.wagtailsearch import index
  13. from wagtail.wagtailsnippets.models import register_snippet
  14. from .blocks import BaseStreamBlock
  15. class BasePageFieldsMixin(models.Model):
  16. """
  17. An abstract base class for common fields
  18. """
  19. introduction = models.TextField(
  20. help_text='Text to describe the page',
  21. blank=True)
  22. image = models.ForeignKey(
  23. 'wagtailimages.Image',
  24. null=True,
  25. blank=True,
  26. on_delete=models.SET_NULL,
  27. related_name='+',
  28. help_text='Landscape mode only; horizontal width between 1000px and 3000px.'
  29. )
  30. body = StreamField(
  31. BaseStreamBlock(), verbose_name="Page body", blank=True
  32. )
  33. content_panels = Page.content_panels + [
  34. FieldPanel('introduction', classname="full"),
  35. ImageChooserPanel('image'),
  36. StreamFieldPanel('body'),
  37. ]
  38. class Meta:
  39. abstract = True
  40. @register_snippet
  41. class People(ClusterableModel):
  42. """
  43. `People` snippets are secondary content objects that do not require their
  44. own full webpage to render.
  45. """
  46. first_name = models.CharField("First name", max_length=254)
  47. last_name = models.CharField("Last name", max_length=254)
  48. job_title = models.CharField("Job title", max_length=254)
  49. image = models.ForeignKey(
  50. 'wagtailimages.Image',
  51. null=True,
  52. blank=True,
  53. on_delete=models.SET_NULL,
  54. related_name='+'
  55. )
  56. panels = [
  57. FieldPanel('first_name', classname="col6"),
  58. FieldPanel('last_name', classname="col6"),
  59. FieldPanel('job_title'),
  60. ImageChooserPanel('image')
  61. ]
  62. search_fields = Page.search_fields + [
  63. index.SearchField('first_name'),
  64. index.SearchField('last_name'),
  65. ]
  66. @property
  67. def thumb_image(self):
  68. # fail silently if there is no profile pic or the rendition file can't
  69. # be found. Note @richbrennan worked out how to do this...
  70. try:
  71. return self.image.get_rendition('fill-50x50').img_tag()
  72. except:
  73. return ''
  74. def __str__(self):
  75. return '{} {}'.format(self.first_name, self.last_name)
  76. class Meta:
  77. verbose_name = 'Person'
  78. verbose_name_plural = 'People'
  79. @register_snippet
  80. class FooterText(models.Model):
  81. """
  82. This provides editable text for the site footer
  83. """
  84. body = RichTextField()
  85. panels = [
  86. FieldPanel('body'),
  87. ]
  88. def __str__(self):
  89. return "Footer text"
  90. class Meta:
  91. verbose_name_plural = 'Footer Text'
  92. class StandardPage(Page, BasePageFieldsMixin):
  93. """
  94. A fairly generic site page, to be used for About, etc.
  95. Defines no fields of its own - just a wrapper for the fields defined in BasePageFieldsMixin.
  96. """
  97. # Inherit the content panels from BasePageFieldsMixin
  98. content_panels = BasePageFieldsMixin.content_panels + []
  99. class HomePage(Page):
  100. """
  101. The Home Page
  102. """
  103. image = models.ForeignKey(
  104. 'wagtailimages.Image',
  105. null=True,
  106. blank=True,
  107. on_delete=models.SET_NULL,
  108. related_name='+',
  109. help_text='Homepage image'
  110. )
  111. hero_text = models.CharField(
  112. max_length=255,
  113. help_text='Write an introduction for the bakery'
  114. )
  115. hero_cta = models.CharField(
  116. verbose_name='Hero CTA',
  117. max_length=255,
  118. help_text='Text to display on CTA'
  119. )
  120. hero_cta_link = models.ForeignKey(
  121. 'wagtailcore.Page',
  122. null=True,
  123. blank=True,
  124. on_delete=models.SET_NULL,
  125. related_name='+',
  126. verbose_name='Hero CTA link',
  127. help_text='Choose a page to link to for the CTA'
  128. )
  129. body = StreamField(
  130. BaseStreamBlock(), verbose_name="Home content block", blank=True
  131. )
  132. promo_image = models.ForeignKey(
  133. 'wagtailimages.Image',
  134. null=True,
  135. blank=True,
  136. on_delete=models.SET_NULL,
  137. related_name='+',
  138. help_text='Promo image'
  139. )
  140. promo_title = models.CharField(
  141. null=True,
  142. blank=True,
  143. max_length=255,
  144. help_text='Title to display above the promo copy'
  145. )
  146. promo_text = RichTextField(
  147. null=True,
  148. blank=True,
  149. help_text='Write some promotional copy'
  150. )
  151. featured_section_1_title = models.CharField(
  152. null=True,
  153. blank=True,
  154. max_length=255,
  155. help_text='Title to display above the promo copy'
  156. )
  157. featured_section_1 = models.ForeignKey(
  158. 'wagtailcore.Page',
  159. null=True,
  160. blank=True,
  161. on_delete=models.SET_NULL,
  162. related_name='+',
  163. help_text='First featured section for the homepage. Will display up to three child items.',
  164. verbose_name='Featured section 1'
  165. )
  166. featured_section_2_title = models.CharField(
  167. null=True,
  168. blank=True,
  169. max_length=255,
  170. help_text='Title to display above the promo copy'
  171. )
  172. featured_section_2 = models.ForeignKey(
  173. 'wagtailcore.Page',
  174. null=True,
  175. blank=True,
  176. on_delete=models.SET_NULL,
  177. related_name='+',
  178. help_text='Second featured section for the homepage. Will display up to three child items.',
  179. verbose_name='Featured section 2'
  180. )
  181. featured_section_3_title = models.CharField(
  182. null=True,
  183. blank=True,
  184. max_length=255,
  185. help_text='Title to display above the promo copy'
  186. )
  187. featured_section_3 = models.ForeignKey(
  188. 'wagtailcore.Page',
  189. null=True,
  190. blank=True,
  191. on_delete=models.SET_NULL,
  192. related_name='+',
  193. help_text='Third featured section for the homepage. Will display up to six child items.',
  194. verbose_name='Featured section 3'
  195. )
  196. content_panels = Page.content_panels + [
  197. MultiFieldPanel([
  198. ImageChooserPanel('image'),
  199. FieldPanel('hero_text', classname="full"),
  200. MultiFieldPanel([
  201. FieldPanel('hero_cta'),
  202. PageChooserPanel('hero_cta_link'),
  203. ])
  204. ], heading="Hero section"),
  205. MultiFieldPanel([
  206. ImageChooserPanel('promo_image'),
  207. FieldPanel('promo_title'),
  208. FieldPanel('promo_text'),
  209. ], heading="Promo section"),
  210. StreamFieldPanel('body'),
  211. MultiFieldPanel([
  212. MultiFieldPanel([
  213. FieldPanel('featured_section_1_title'),
  214. PageChooserPanel('featured_section_1'),
  215. ]),
  216. MultiFieldPanel([
  217. FieldPanel('featured_section_2_title'),
  218. PageChooserPanel('featured_section_2'),
  219. ]),
  220. MultiFieldPanel([
  221. FieldPanel('featured_section_3_title'),
  222. PageChooserPanel('featured_section_3'),
  223. ])
  224. ], heading="Featured homepage sections", classname="collapsible")
  225. ]
  226. def __str__(self):
  227. return self.title
  228. class GalleryPage(BasePageFieldsMixin, Page):
  229. """
  230. This is a page to list locations from the selected Collection
  231. """
  232. collection = models.ForeignKey(
  233. Collection,
  234. limit_choices_to=~models.Q(name__in=['Root']),
  235. null=True,
  236. blank=True,
  237. on_delete=models.SET_NULL,
  238. help_text='Select the image collection for this gallery.'
  239. )
  240. content_panels = BasePageFieldsMixin.content_panels + [
  241. FieldPanel('collection'),
  242. ]
  243. # Defining what content type can sit under the parent. Since it's a blank
  244. # array no subpage can be added
  245. subpage_types = []
  246. class FormField(AbstractFormField):
  247. page = ParentalKey('FormPage', related_name='form_fields')
  248. class FormPage(AbstractEmailForm):
  249. image = models.ForeignKey(
  250. 'wagtailimages.Image',
  251. null=True,
  252. blank=True,
  253. on_delete=models.SET_NULL,
  254. related_name='+'
  255. )
  256. body = StreamField(BaseStreamBlock())
  257. thank_you_text = RichTextField(blank=True)
  258. content_panels = AbstractEmailForm.content_panels + [
  259. ImageChooserPanel('image'),
  260. StreamFieldPanel('body'),
  261. InlinePanel('form_fields', label="Form fields"),
  262. FieldPanel('thank_you_text', classname="full"),
  263. MultiFieldPanel([
  264. FieldRowPanel([
  265. FieldPanel('from_address', classname="col6"),
  266. FieldPanel('to_address', classname="col6"),
  267. ]),
  268. FieldPanel('subject'),
  269. ], "Email"),
  270. ]