models.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. from wagtail.contrib.modeladmin.options import (
  23. ModelAdmin, ModelAdminGroup, modeladmin_register)
  24. @register_snippet
  25. class People(ClusterableModel):
  26. first_name = models.CharField("First name", max_length=254)
  27. last_name = models.CharField("Last name", max_length=254)
  28. job_title = models.CharField("Job title", max_length=254)
  29. image = models.ForeignKey(
  30. 'wagtailimages.Image',
  31. null=True,
  32. blank=True,
  33. on_delete=models.SET_NULL,
  34. related_name='+'
  35. )
  36. panels = [
  37. FieldPanel('first_name', classname="col6"),
  38. FieldPanel('last_name', classname="col6"),
  39. FieldPanel('job_title'),
  40. ImageChooserPanel('image')
  41. ]
  42. search_fields = Page.search_fields + [
  43. index.SearchField('first_name'),
  44. index.SearchField('last_name'),
  45. ]
  46. @property
  47. def thumb_image(self):
  48. # fail silently if there is no profile pic or the rendition file can't
  49. # be found. Note @richbrennan worked out how to do this...
  50. try:
  51. return self.image.get_rendition('fill-50x50').img_tag()
  52. except:
  53. return ''
  54. def __str__(self):
  55. return self.first_name + " " + self.last_name
  56. class Meta:
  57. verbose_name = 'Person'
  58. verbose_name_plural = 'People'
  59. class AboutLocationRelationship(Orderable, models.Model):
  60. """
  61. This defines the relationship between the `LocationPage` within the `locations`
  62. app and the About page below allowing us to add locations to the about
  63. section.
  64. """
  65. page = ParentalKey(
  66. 'AboutPage', related_name='location_about_relationship'
  67. )
  68. locations = models.ForeignKey(
  69. 'locations.LocationPage', related_name='about_location_relationship'
  70. )
  71. panels = [
  72. PageChooserPanel('locations')
  73. ]
  74. class AboutPage(Page):
  75. """
  76. The About Page
  77. """
  78. image = models.ForeignKey(
  79. 'wagtailimages.Image',
  80. null=True,
  81. blank=True,
  82. on_delete=models.SET_NULL,
  83. related_name='+',
  84. help_text='Location image'
  85. )
  86. body = StreamField(
  87. BaseStreamBlock(), verbose_name="About page detail", blank=True
  88. )
  89. # We've defined the StreamBlock() within blocks.py that we've imported on
  90. # line 12. Defining it in a different file gives us consistency across the
  91. # site, though StreamFields _can_ be created on a per model basis if you
  92. # have a use case for it
  93. content_panels = Page.content_panels + [
  94. ImageChooserPanel('image'),
  95. StreamFieldPanel('body'),
  96. InlinePanel(
  97. 'location_about_relationship',
  98. label='Locations',
  99. min_num=None
  100. ),
  101. ]
  102. # parent_page_types = [
  103. # 'home.HomePage'
  104. # ]
  105. # Defining what content type can sit under the parent
  106. # The empty array means that no children can be placed under the
  107. # LocationPage page model
  108. subpage_types = []
  109. # api_fields = ['image', 'body']
  110. def getImageCollections():
  111. # We return all collections to a list that don't have the name root.
  112. try:
  113. collection_images = [(
  114. collection.id, collection.name
  115. ) for collection in Collection.objects.all().exclude(
  116. name='Root'
  117. )]
  118. return collection_images
  119. except:
  120. return [('', '')]
  121. def __str__(self):
  122. return self.title
  123. class HomePage(Page):
  124. """
  125. The Home Page
  126. """
  127. image = models.ForeignKey(
  128. 'wagtailimages.Image',
  129. null=True,
  130. blank=True,
  131. on_delete=models.SET_NULL,
  132. related_name='+',
  133. help_text='Location image'
  134. )
  135. body = StreamField(
  136. BaseStreamBlock(), verbose_name="Home page detail", blank=True
  137. )
  138. content_panels = Page.content_panels + [
  139. ImageChooserPanel('image'),
  140. StreamFieldPanel('body'),
  141. ]
  142. def __str__(self):
  143. return self.title
  144. class GalleryPage(Page):
  145. """
  146. This is a page to list all the locations on the site
  147. """
  148. choices = models.ForeignKey(
  149. Collection,
  150. limit_choices_to=~models.Q(name__in=['Root']),
  151. null=True,
  152. blank=True,
  153. on_delete=models.SET_NULL,
  154. )
  155. image = models.ForeignKey(
  156. 'wagtailimages.Image',
  157. null=True,
  158. blank=True,
  159. on_delete=models.SET_NULL,
  160. related_name='+',
  161. help_text='Location listing image'
  162. )
  163. introduction = models.TextField(
  164. help_text='Text to describe the index page',
  165. blank=True)
  166. content_panels = Page.content_panels + [
  167. FieldPanel('choices'),
  168. ImageChooserPanel('image'),
  169. FieldPanel('introduction')
  170. ]
  171. # parent_page_types = [
  172. # 'home.HomePage'
  173. # ]
  174. # Defining what content type can sit under the parent. Since it's a blank
  175. # array no subpage can be added
  176. subpage_types = [
  177. ]
  178. # api_fields = ['introduction']
  179. class FormField(AbstractFormField):
  180. page = ParentalKey('FormPage', related_name='form_fields')
  181. class FormPage(AbstractEmailForm):
  182. header_image = models.ForeignKey(
  183. 'wagtailimages.Image',
  184. null=True,
  185. blank=True,
  186. on_delete=models.SET_NULL,
  187. related_name='+'
  188. )
  189. body = StreamField(BaseStreamBlock())
  190. thank_you_text = RichTextField(blank=True)
  191. content_panels = AbstractEmailForm.content_panels + [
  192. ImageChooserPanel('header_image'),
  193. StreamFieldPanel('body'),
  194. InlinePanel('form_fields', label="Form fields"),
  195. FieldPanel('thank_you_text', classname="full"),
  196. MultiFieldPanel([
  197. FieldRowPanel([
  198. FieldPanel('from_address', classname="col6"),
  199. FieldPanel('to_address', classname="col6"),
  200. ]),
  201. FieldPanel('subject'),
  202. ], "Email"),
  203. ]
  204. class PeopleModelAdmin(ModelAdmin):
  205. model = People
  206. menu_label = 'People' # ditch this to use verbose_name_plural from model
  207. menu_icon = 'fa-people' # change as required
  208. list_display = ('first_name', 'last_name', 'job_title', 'thumb_image')
  209. class MyModelAdminGroup(ModelAdminGroup):
  210. menu_label = 'WagtailBakery'
  211. menu_icon = 'folder-open-inverse' # change as required
  212. menu_order = 200 # will put in 3rd place (000 being 1st, 100 2nd)
  213. items = (PeopleModelAdmin,)
  214. # When using a ModelAdminGroup class to group several ModelAdmin classes together,
  215. # you only need to register the ModelAdminGroup class with Wagtail:
  216. modeladmin_register(MyModelAdminGroup)