2
0

models.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. from __future__ import unicode_literals
  2. from django.db import models
  3. from django.db.utils import OperationalError
  4. from modelcluster.fields import ParentalKey
  5. from modelcluster.models import ClusterableModel
  6. from wagtail.wagtailcore.models import Page, Orderable, Collection
  7. from wagtail.wagtailsearch import index
  8. from wagtail.wagtailimages.edit_handlers import ImageChooserPanel
  9. from wagtail.wagtailcore.fields import StreamField, RichTextField
  10. from wagtail.wagtailadmin.edit_handlers import (
  11. FieldPanel,
  12. InlinePanel,
  13. FieldRowPanel,
  14. StreamFieldPanel,
  15. MultiFieldPanel,
  16. PageChooserPanel
  17. )
  18. from wagtail.wagtailsnippets.models import register_snippet
  19. from wagtail.wagtailsnippets.edit_handlers import SnippetChooserPanel
  20. from .blocks import BaseStreamBlock
  21. from wagtail.wagtailforms.models import AbstractEmailForm, AbstractFormField
  22. @register_snippet
  23. class People(ClusterableModel):
  24. first_name = models.CharField("First name", max_length=254)
  25. last_name = models.CharField("Last name", max_length=254)
  26. job_title = models.CharField("Job title", max_length=254)
  27. image = models.ForeignKey(
  28. 'wagtailimages.Image',
  29. null=True,
  30. blank=True,
  31. on_delete=models.SET_NULL,
  32. related_name='+'
  33. )
  34. panels = [
  35. FieldPanel('first_name', classname="col6"),
  36. FieldPanel('last_name', classname="col6"),
  37. FieldPanel('job_title'),
  38. ImageChooserPanel('image')
  39. ]
  40. search_fields = Page.search_fields + [
  41. index.SearchField('first_name'),
  42. index.SearchField('last_name'),
  43. ]
  44. def __str__(self):
  45. return self.first_name + " " + self.last_name
  46. class Meta:
  47. verbose_name = 'Person'
  48. verbose_name_plural = 'People'
  49. class AboutLocationRelationship(Orderable, models.Model):
  50. """
  51. This defines the relationship between the `LocationPage` within the `locations`
  52. app and the About page below allowing us to add locations to the about
  53. section.
  54. """
  55. page = ParentalKey(
  56. 'AboutPage', related_name='location_about_relationship'
  57. )
  58. locations = models.ForeignKey(
  59. 'locations.LocationPage', related_name='about_location_relationship'
  60. )
  61. panels = [
  62. PageChooserPanel('locations')
  63. ]
  64. class AboutPage(Page):
  65. """
  66. The About Page
  67. """
  68. image = models.ForeignKey(
  69. 'wagtailimages.Image',
  70. null=True,
  71. blank=True,
  72. on_delete=models.SET_NULL,
  73. related_name='+',
  74. help_text='Location image'
  75. )
  76. body = StreamField(
  77. BaseStreamBlock(), verbose_name="About page detail", blank=True
  78. )
  79. # We've defined the StreamBlock() within blocks.py that we've imported on
  80. # line 12. Defining it in a different file gives us consistency across the
  81. # site, though StreamFields _can_ be created on a per model basis if you
  82. # have a use case for it
  83. content_panels = Page.content_panels + [
  84. ImageChooserPanel('image'),
  85. StreamFieldPanel('body'),
  86. InlinePanel(
  87. 'location_about_relationship',
  88. label='Locations',
  89. min_num=None
  90. ),
  91. ]
  92. # parent_page_types = [
  93. # 'home.HomePage'
  94. # ]
  95. # Defining what content type can sit under the parent
  96. # The empty array means that no children can be placed under the
  97. # LocationPage page model
  98. subpage_types = []
  99. # api_fields = ['image', 'body']
  100. def getImageCollections():
  101. # We return all collections to a list that don't have the name root.
  102. try:
  103. collection_images = [(
  104. collection.id, collection.name
  105. ) for collection in Collection.objects.all().exclude(
  106. name='Root'
  107. )]
  108. return collection_images
  109. except:
  110. return [('', '')]
  111. def __str__(self):
  112. return self.title
  113. class HomePage(Page):
  114. """
  115. The Home Page
  116. """
  117. image = models.ForeignKey(
  118. 'wagtailimages.Image',
  119. null=True,
  120. blank=True,
  121. on_delete=models.SET_NULL,
  122. related_name='+',
  123. help_text='Location image'
  124. )
  125. body = StreamField(
  126. BaseStreamBlock(), verbose_name="Home page detail", blank=True
  127. )
  128. content_panels = Page.content_panels + [
  129. ImageChooserPanel('image'),
  130. StreamFieldPanel('body'),
  131. ]
  132. def __str__(self):
  133. return self.title
  134. class GalleryPage(Page):
  135. """
  136. This is a page to list all the locations on the site
  137. """
  138. choices = models.ForeignKey(
  139. Collection,
  140. limit_choices_to=~models.Q(name__in=['Root']),
  141. null=True,
  142. blank=True,
  143. on_delete=models.SET_NULL,
  144. )
  145. image = models.ForeignKey(
  146. 'wagtailimages.Image',
  147. null=True,
  148. blank=True,
  149. on_delete=models.SET_NULL,
  150. related_name='+',
  151. help_text='Location listing image'
  152. )
  153. introduction = models.TextField(
  154. help_text='Text to describe the index page',
  155. blank=True)
  156. content_panels = Page.content_panels + [
  157. FieldPanel('choices'),
  158. ImageChooserPanel('image'),
  159. FieldPanel('introduction')
  160. ]
  161. # parent_page_types = [
  162. # 'home.HomePage'
  163. # ]
  164. # Defining what content type can sit under the parent. Since it's a blank
  165. # array no subpage can be added
  166. subpage_types = [
  167. ]
  168. # api_fields = ['introduction']
  169. class FormField(AbstractFormField):
  170. page = ParentalKey('FormPage', related_name='form_fields')
  171. class FormPage(AbstractEmailForm):
  172. header_image = models.ForeignKey(
  173. 'wagtailimages.Image',
  174. null=True,
  175. blank=True,
  176. on_delete=models.SET_NULL,
  177. related_name='+'
  178. )
  179. body = StreamField(BaseStreamBlock())
  180. thank_you_text = RichTextField(blank=True)
  181. content_panels = AbstractEmailForm.content_panels + [
  182. ImageChooserPanel('header_image'),
  183. StreamFieldPanel('body'),
  184. InlinePanel('form_fields', label="Form fields"),
  185. FieldPanel('thank_you_text', classname="full"),
  186. MultiFieldPanel([
  187. FieldRowPanel([
  188. FieldPanel('from_address', classname="col6"),
  189. FieldPanel('to_address', classname="col6"),
  190. ]),
  191. FieldPanel('subject'),
  192. ], "Email"),
  193. ]