models.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. body = StreamField(
  133. BaseStreamBlock(), verbose_name="Home page detail", blank=True
  134. )
  135. content_panels = Page.content_panels + [
  136. ImageChooserPanel('image'),
  137. StreamFieldPanel('body'),
  138. ]
  139. def __str__(self):
  140. return self.title
  141. class GalleryPage(BasePageFieldsMixin, Page):
  142. """
  143. This is a page to list locations from the selected Collection
  144. """
  145. collection = models.ForeignKey(
  146. Collection,
  147. limit_choices_to=~models.Q(name__in=['Root']),
  148. null=True,
  149. blank=True,
  150. on_delete=models.SET_NULL,
  151. help_text='Select the image collection for this gallery.'
  152. )
  153. content_panels = BasePageFieldsMixin.content_panels + [
  154. FieldPanel('collection'),
  155. ]
  156. # Defining what content type can sit under the parent. Since it's a blank
  157. # array no subpage can be added
  158. subpage_types = []
  159. class FormField(AbstractFormField):
  160. page = ParentalKey('FormPage', related_name='form_fields')
  161. class FormPage(AbstractEmailForm):
  162. header_image = models.ForeignKey(
  163. 'wagtailimages.Image',
  164. null=True,
  165. blank=True,
  166. on_delete=models.SET_NULL,
  167. related_name='+'
  168. )
  169. body = StreamField(BaseStreamBlock())
  170. thank_you_text = RichTextField(blank=True)
  171. content_panels = AbstractEmailForm.content_panels + [
  172. ImageChooserPanel('header_image'),
  173. StreamFieldPanel('body'),
  174. InlinePanel('form_fields', label="Form fields"),
  175. FieldPanel('thank_you_text', classname="full"),
  176. MultiFieldPanel([
  177. FieldRowPanel([
  178. FieldPanel('from_address', classname="col6"),
  179. FieldPanel('to_address', classname="col6"),
  180. ]),
  181. FieldPanel('subject'),
  182. ], "Email"),
  183. ]