models.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 StandardPage(Page, BasePageFieldsMixin):
  94. """
  95. A fairly generic site page, to be used for About, etc.
  96. Defines no fields of its own - just a wrapper for the fields defined in BasePageFieldsMixin.
  97. """
  98. # Inherit the content panels from BasePageFieldsMixin
  99. content_panels = BasePageFieldsMixin.content_panels + []
  100. class HomePage(Page):
  101. """
  102. The Home Page
  103. """
  104. image = models.ForeignKey(
  105. 'wagtailimages.Image',
  106. null=True,
  107. blank=True,
  108. on_delete=models.SET_NULL,
  109. related_name='+',
  110. help_text='Homepage image'
  111. )
  112. body = StreamField(
  113. BaseStreamBlock(), verbose_name="Home page detail", blank=True
  114. )
  115. content_panels = Page.content_panels + [
  116. ImageChooserPanel('image'),
  117. StreamFieldPanel('body'),
  118. ]
  119. def __str__(self):
  120. return self.title
  121. class GalleryPage(BasePageFieldsMixin, Page):
  122. """
  123. This is a page to list locations from the selected Collection
  124. """
  125. collection = models.ForeignKey(
  126. Collection,
  127. limit_choices_to=~models.Q(name__in=['Root']),
  128. null=True,
  129. blank=True,
  130. on_delete=models.SET_NULL,
  131. help_text='Select the image collection for this gallery.'
  132. )
  133. content_panels = BasePageFieldsMixin.content_panels + [
  134. FieldPanel('collection'),
  135. ]
  136. # Defining what content type can sit under the parent. Since it's a blank
  137. # array no subpage can be added
  138. subpage_types = []
  139. class FormField(AbstractFormField):
  140. page = ParentalKey('FormPage', related_name='form_fields')
  141. class FormPage(AbstractEmailForm):
  142. image = models.ForeignKey(
  143. 'wagtailimages.Image',
  144. null=True,
  145. blank=True,
  146. on_delete=models.SET_NULL,
  147. related_name='+'
  148. )
  149. body = StreamField(BaseStreamBlock())
  150. thank_you_text = RichTextField(blank=True)
  151. content_panels = AbstractEmailForm.content_panels + [
  152. ImageChooserPanel('image'),
  153. StreamFieldPanel('body'),
  154. InlinePanel('form_fields', label="Form fields"),
  155. FieldPanel('thank_you_text', classname="full"),
  156. MultiFieldPanel([
  157. FieldRowPanel([
  158. FieldPanel('from_address', classname="col6"),
  159. FieldPanel('to_address', classname="col6"),
  160. ]),
  161. FieldPanel('subject'),
  162. ], "Email"),
  163. ]