2
0

models.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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, StreamFieldPanel,
  7. )
  8. from wagtail.wagtailcore.fields import RichTextField, StreamField
  9. from wagtail.wagtailcore.models import Collection, Page
  10. from wagtail.wagtailforms.models import AbstractEmailForm, AbstractFormField
  11. from wagtail.wagtailimages.edit_handlers import ImageChooserPanel
  12. from wagtail.wagtailsearch import index
  13. from wagtail.wagtailsnippets.models import register_snippet
  14. from .blocks import BaseStreamBlock
  15. class BasePageFieldsMixin(models.Model):
  16. """
  17. An abstract base class for common fields
  18. """
  19. introduction = models.TextField(
  20. help_text='Text to describe the page',
  21. blank=True)
  22. image = models.ForeignKey(
  23. 'wagtailimages.Image',
  24. null=True,
  25. blank=True,
  26. on_delete=models.SET_NULL,
  27. related_name='+',
  28. help_text='Landscape mode only; horizontal width between 1000px and 3000px.'
  29. )
  30. body = StreamField(
  31. BaseStreamBlock(), verbose_name="Page body", blank=True
  32. )
  33. content_panels = Page.content_panels + [
  34. FieldPanel('introduction', classname="full"),
  35. ImageChooserPanel('image'),
  36. StreamFieldPanel('body'),
  37. ]
  38. class Meta:
  39. abstract = True
  40. @register_snippet
  41. class People(ClusterableModel):
  42. """
  43. `People` snippets are secondary content objects that do not require their
  44. own full webpage to render.
  45. """
  46. first_name = models.CharField("First name", max_length=254)
  47. last_name = models.CharField("Last name", max_length=254)
  48. job_title = models.CharField("Job title", max_length=254)
  49. image = models.ForeignKey(
  50. 'wagtailimages.Image',
  51. null=True,
  52. blank=True,
  53. on_delete=models.SET_NULL,
  54. related_name='+'
  55. )
  56. panels = [
  57. FieldPanel('first_name', classname="col6"),
  58. FieldPanel('last_name', classname="col6"),
  59. FieldPanel('job_title'),
  60. ImageChooserPanel('image')
  61. ]
  62. search_fields = Page.search_fields + [
  63. index.SearchField('first_name'),
  64. index.SearchField('last_name'),
  65. ]
  66. @property
  67. def thumb_image(self):
  68. # fail silently if there is no profile pic or the rendition file can't
  69. # be found. Note @richbrennan worked out how to do this...
  70. try:
  71. return self.image.get_rendition('fill-50x50').img_tag()
  72. except:
  73. return ''
  74. def __str__(self):
  75. return '{} {}'.format(self.first_name, self.last_name)
  76. class Meta:
  77. verbose_name = 'Person'
  78. verbose_name_plural = 'People'
  79. @register_snippet
  80. class FooterText(models.Model):
  81. """
  82. This provides editable text for the site footer
  83. """
  84. body = RichTextField()
  85. panels = [
  86. FieldPanel('body'),
  87. ]
  88. def __str__(self):
  89. return "Footer text"
  90. class Meta:
  91. verbose_name_plural = 'Footer Text'
  92. class StandardPage(Page, BasePageFieldsMixin):
  93. """
  94. A fairly generic site page, to be used for About, etc.
  95. Defines no fields of its own - just a wrapper for the fields defined in BasePageFieldsMixin.
  96. """
  97. # Inherit the content panels from BasePageFieldsMixin
  98. content_panels = BasePageFieldsMixin.content_panels + []
  99. class HomePage(Page):
  100. """
  101. The Home Page
  102. """
  103. image = models.ForeignKey(
  104. 'wagtailimages.Image',
  105. null=True,
  106. blank=True,
  107. on_delete=models.SET_NULL,
  108. related_name='+',
  109. help_text='Homepage image'
  110. )
  111. body = StreamField(
  112. BaseStreamBlock(), verbose_name="Home page detail", blank=True
  113. )
  114. content_panels = Page.content_panels + [
  115. ImageChooserPanel('image'),
  116. StreamFieldPanel('body'),
  117. ]
  118. def __str__(self):
  119. return self.title
  120. class GalleryPage(BasePageFieldsMixin, Page):
  121. """
  122. This is a page to list locations from the selected Collection
  123. """
  124. collection = models.ForeignKey(
  125. Collection,
  126. limit_choices_to=~models.Q(name__in=['Root']),
  127. null=True,
  128. blank=True,
  129. on_delete=models.SET_NULL,
  130. help_text='Select the image collection for this gallery.'
  131. )
  132. content_panels = BasePageFieldsMixin.content_panels + [
  133. FieldPanel('collection'),
  134. ]
  135. # Defining what content type can sit under the parent. Since it's a blank
  136. # array no subpage can be added
  137. subpage_types = []
  138. class FormField(AbstractFormField):
  139. page = ParentalKey('FormPage', related_name='form_fields')
  140. class FormPage(AbstractEmailForm):
  141. image = models.ForeignKey(
  142. 'wagtailimages.Image',
  143. null=True,
  144. blank=True,
  145. on_delete=models.SET_NULL,
  146. related_name='+'
  147. )
  148. body = StreamField(BaseStreamBlock())
  149. thank_you_text = RichTextField(blank=True)
  150. content_panels = AbstractEmailForm.content_panels + [
  151. ImageChooserPanel('image'),
  152. StreamFieldPanel('body'),
  153. InlinePanel('form_fields', label="Form fields"),
  154. FieldPanel('thank_you_text', classname="full"),
  155. MultiFieldPanel([
  156. FieldRowPanel([
  157. FieldPanel('from_address', classname="col6"),
  158. FieldPanel('to_address', classname="col6"),
  159. ]),
  160. FieldPanel('subject'),
  161. ], "Email"),
  162. ]