models.py 9.5 KB

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