models.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. body = StreamField(
  32. BaseStreamBlock(), verbose_name="Page body", blank=True
  33. )
  34. content_panels = Page.content_panels + [
  35. FieldPanel('introduction', classname="full"),
  36. ImageChooserPanel('image'),
  37. StreamFieldPanel('body'),
  38. ]
  39. class Meta:
  40. abstract = True
  41. @register_snippet
  42. class People(ClusterableModel):
  43. """
  44. `People` snippets are secondary content objects that do not require their
  45. own full webpage to render.
  46. """
  47. first_name = models.CharField("First name", max_length=254)
  48. last_name = models.CharField("Last name", max_length=254)
  49. job_title = models.CharField("Job title", max_length=254)
  50. image = models.ForeignKey(
  51. 'wagtailimages.Image',
  52. null=True,
  53. blank=True,
  54. on_delete=models.SET_NULL,
  55. related_name='+'
  56. )
  57. panels = [
  58. FieldPanel('first_name', classname="col6"),
  59. FieldPanel('last_name', classname="col6"),
  60. FieldPanel('job_title'),
  61. ImageChooserPanel('image')
  62. ]
  63. search_fields = Page.search_fields + [
  64. index.SearchField('first_name'),
  65. index.SearchField('last_name'),
  66. ]
  67. @property
  68. def thumb_image(self):
  69. # fail silently if there is no profile pic or the rendition file can't
  70. # be found. Note @richbrennan worked out how to do this...
  71. try:
  72. return self.image.get_rendition('fill-50x50').img_tag()
  73. 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(models.Model):
  82. """
  83. This provides editable text for the site footer
  84. """
  85. body = RichTextField()
  86. panels = [
  87. FieldPanel('body'),
  88. ]
  89. def __str__(self):
  90. return "Footer text"
  91. class Meta:
  92. verbose_name_plural = 'Footer Text'
  93. class AboutPage(Page):
  94. """
  95. The About Page
  96. """
  97. image = models.ForeignKey(
  98. 'wagtailimages.Image',
  99. null=True,
  100. blank=True,
  101. on_delete=models.SET_NULL,
  102. related_name='+',
  103. help_text='About image'
  104. )
  105. body = StreamField(
  106. BaseStreamBlock(), verbose_name="About page detail", blank=True
  107. )
  108. # We've defined the StreamBlock() within blocks.py that we've imported on
  109. # line 12. Defining it in a different file gives us consistency across the
  110. # site, though StreamFields _can_ be created on a per model basis if you
  111. # have a use case for it
  112. content_panels = Page.content_panels + [
  113. ImageChooserPanel('image'),
  114. StreamFieldPanel('body'),
  115. ]
  116. # parent_page_types = [
  117. # 'home.HomePage'
  118. # ]
  119. # Defining what content type can sit under the parent
  120. # The empty array means that no children can be placed under the
  121. # LocationPage page model
  122. subpage_types = []
  123. # api_fields = ['image', 'body']
  124. class HomePage(Page):
  125. """
  126. The Home Page
  127. """
  128. image = models.ForeignKey(
  129. 'wagtailimages.Image',
  130. null=True,
  131. blank=True,
  132. on_delete=models.SET_NULL,
  133. related_name='+',
  134. help_text='Homepage image'
  135. )
  136. body = StreamField(
  137. BaseStreamBlock(), verbose_name="Home page detail", blank=True
  138. )
  139. content_panels = Page.content_panels + [
  140. ImageChooserPanel('image'),
  141. StreamFieldPanel('body'),
  142. ]
  143. def __str__(self):
  144. return self.title
  145. class GalleryPage(BasePageFieldsMixin, Page):
  146. """
  147. This is a page to list locations from the selected Collection
  148. """
  149. collection = models.ForeignKey(
  150. Collection,
  151. limit_choices_to=~models.Q(name__in=['Root']),
  152. null=True,
  153. blank=True,
  154. on_delete=models.SET_NULL,
  155. help_text='Select the image collection for this gallery.'
  156. )
  157. content_panels = BasePageFieldsMixin.content_panels + [
  158. FieldPanel('collection'),
  159. ]
  160. # Defining what content type can sit under the parent. Since it's a blank
  161. # array no subpage can be added
  162. subpage_types = []
  163. class FormField(AbstractFormField):
  164. page = ParentalKey('FormPage', related_name='form_fields')
  165. class FormPage(AbstractEmailForm):
  166. image = models.ForeignKey(
  167. 'wagtailimages.Image',
  168. null=True,
  169. blank=True,
  170. on_delete=models.SET_NULL,
  171. related_name='+'
  172. )
  173. body = StreamField(BaseStreamBlock())
  174. thank_you_text = RichTextField(blank=True)
  175. content_panels = AbstractEmailForm.content_panels + [
  176. ImageChooserPanel('image'),
  177. StreamFieldPanel('body'),
  178. InlinePanel('form_fields', label="Form fields"),
  179. FieldPanel('thank_you_text', classname="full"),
  180. MultiFieldPanel([
  181. FieldRowPanel([
  182. FieldPanel('from_address', classname="col6"),
  183. FieldPanel('to_address', classname="col6"),
  184. ]),
  185. FieldPanel('subject'),
  186. ], "Email"),
  187. ]