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