models.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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 AboutLocationRelationship(Orderable, models.Model):
  90. """
  91. This defines the relationship between the `LocationPage` within the `locations`
  92. app and the About page below allowing us to add locations to the about
  93. section.
  94. """
  95. page = ParentalKey(
  96. 'AboutPage', related_name='location_about_relationship'
  97. )
  98. locations = models.ForeignKey(
  99. 'locations.LocationPage', related_name='about_location_relationship'
  100. )
  101. panels = [
  102. PageChooserPanel('locations')
  103. ]
  104. class AboutPage(Page):
  105. """
  106. The About Page
  107. """
  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='About image'
  115. )
  116. body = StreamField(
  117. BaseStreamBlock(), verbose_name="About page detail", blank=True
  118. )
  119. # We've defined the StreamBlock() within blocks.py that we've imported on
  120. # line 12. Defining it in a different file gives us consistency across the
  121. # site, though StreamFields _can_ be created on a per model basis if you
  122. # have a use case for it
  123. content_panels = Page.content_panels + [
  124. ImageChooserPanel('image'),
  125. StreamFieldPanel('body'),
  126. InlinePanel(
  127. 'location_about_relationship',
  128. label='Locations',
  129. min_num=None
  130. ),
  131. ]
  132. # parent_page_types = [
  133. # 'home.HomePage'
  134. # ]
  135. # Defining what content type can sit under the parent
  136. # The empty array means that no children can be placed under the
  137. # LocationPage page model
  138. subpage_types = []
  139. # api_fields = ['image', 'body']
  140. class HomePage(Page):
  141. """
  142. The Home Page
  143. """
  144. image = models.ForeignKey(
  145. 'wagtailimages.Image',
  146. null=True,
  147. blank=True,
  148. on_delete=models.SET_NULL,
  149. related_name='+',
  150. help_text='Homepage image'
  151. )
  152. body = StreamField(
  153. BaseStreamBlock(), verbose_name="Home page detail", blank=True
  154. )
  155. content_panels = Page.content_panels + [
  156. ImageChooserPanel('image'),
  157. StreamFieldPanel('body'),
  158. ]
  159. def __str__(self):
  160. return self.title
  161. class GalleryPage(BasePageFieldsMixin, Page):
  162. """
  163. This is a page to list locations from the selected Collection
  164. """
  165. collection = models.ForeignKey(
  166. Collection,
  167. limit_choices_to=~models.Q(name__in=['Root']),
  168. null=True,
  169. blank=True,
  170. on_delete=models.SET_NULL,
  171. help_text='Select the image collection for this gallery.'
  172. )
  173. content_panels = BasePageFieldsMixin.content_panels + [
  174. FieldPanel('collection'),
  175. ]
  176. # Defining what content type can sit under the parent. Since it's a blank
  177. # array no subpage can be added
  178. subpage_types = [
  179. ]
  180. class FormField(AbstractFormField):
  181. page = ParentalKey('FormPage', related_name='form_fields')
  182. class FormPage(AbstractEmailForm):
  183. header_image = models.ForeignKey(
  184. 'wagtailimages.Image',
  185. null=True,
  186. blank=True,
  187. on_delete=models.SET_NULL,
  188. related_name='+'
  189. )
  190. body = StreamField(BaseStreamBlock())
  191. thank_you_text = RichTextField(blank=True)
  192. content_panels = AbstractEmailForm.content_panels + [
  193. ImageChooserPanel('header_image'),
  194. StreamFieldPanel('body'),
  195. InlinePanel('form_fields', label="Form fields"),
  196. FieldPanel('thank_you_text', classname="full"),
  197. MultiFieldPanel([
  198. FieldRowPanel([
  199. FieldPanel('from_address', classname="col6"),
  200. FieldPanel('to_address', classname="col6"),
  201. ]),
  202. FieldPanel('subject'),
  203. ], "Email"),
  204. ]