models.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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.admin.panels import (
  6. FieldPanel,
  7. FieldRowPanel,
  8. InlinePanel,
  9. MultiFieldPanel,
  10. )
  11. from wagtail.fields import RichTextField, StreamField
  12. from wagtail.models import Collection, Page
  13. from wagtail.contrib.forms.models import AbstractEmailForm, AbstractFormField
  14. from wagtail.search import index
  15. from wagtail.snippets.models import register_snippet
  16. from .blocks import BaseStreamBlock
  17. @register_snippet
  18. class People(index.Indexed, ClusterableModel):
  19. """
  20. A Django model to store People objects.
  21. It uses the `@register_snippet` decorator to allow it to be accessible
  22. via the Snippets UI (e.g. /admin/snippets/base/people/)
  23. `People` uses the `ClusterableModel`, which allows the relationship with
  24. another model to be stored locally to the 'parent' model (e.g. a PageModel)
  25. until the parent is explicitly saved. This allows the editor to use the
  26. 'Preview' button, to preview the content, without saving the relationships
  27. to the database.
  28. https://github.com/wagtail/django-modelcluster
  29. """
  30. first_name = models.CharField("First name", max_length=254)
  31. last_name = models.CharField("Last name", max_length=254)
  32. job_title = models.CharField("Job title", max_length=254)
  33. image = models.ForeignKey(
  34. 'wagtailimages.Image',
  35. null=True,
  36. blank=True,
  37. on_delete=models.SET_NULL,
  38. related_name='+'
  39. )
  40. panels = [
  41. MultiFieldPanel([
  42. FieldRowPanel([
  43. FieldPanel('first_name', classname="col6"),
  44. FieldPanel('last_name', classname="col6"),
  45. ])
  46. ], "Name"),
  47. FieldPanel('job_title'),
  48. FieldPanel('image')
  49. ]
  50. search_fields = [
  51. index.SearchField('first_name'),
  52. index.SearchField('last_name'),
  53. ]
  54. @property
  55. def thumb_image(self):
  56. # Returns an empty string if there is no profile pic or the rendition
  57. # file can't be found.
  58. try:
  59. return self.image.get_rendition('fill-50x50').img_tag()
  60. except: # noqa: E722 FIXME: remove bare 'except:'
  61. return ''
  62. def __str__(self):
  63. return '{} {}'.format(self.first_name, self.last_name)
  64. class Meta:
  65. verbose_name = 'Person'
  66. verbose_name_plural = 'People'
  67. @register_snippet
  68. class FooterText(models.Model):
  69. """
  70. This provides editable text for the site footer. Again it uses the decorator
  71. `register_snippet` to allow it to be accessible via the admin. It is made
  72. accessible on the template via a template tag defined in base/templatetags/
  73. navigation_tags.py
  74. """
  75. body = RichTextField()
  76. panels = [
  77. FieldPanel('body'),
  78. ]
  79. def __str__(self):
  80. return "Footer text"
  81. class Meta:
  82. verbose_name_plural = 'Footer Text'
  83. class StandardPage(Page):
  84. """
  85. A generic content page. On this demo site we use it for an about page but
  86. it could be used for any type of page content that only needs a title,
  87. image, introduction and body field
  88. """
  89. introduction = models.TextField(
  90. help_text='Text to describe the page',
  91. blank=True)
  92. image = models.ForeignKey(
  93. 'wagtailimages.Image',
  94. null=True,
  95. blank=True,
  96. on_delete=models.SET_NULL,
  97. related_name='+',
  98. help_text='Landscape mode only; horizontal width between 1000px and 3000px.'
  99. )
  100. body = StreamField(
  101. BaseStreamBlock(), verbose_name="Page body", blank=True, use_json_field=True
  102. )
  103. content_panels = Page.content_panels + [
  104. FieldPanel('introduction', classname="full"),
  105. FieldPanel('body'),
  106. FieldPanel('image'),
  107. ]
  108. class HomePage(Page):
  109. """
  110. The Home Page. This looks slightly more complicated than it is. You can
  111. see if you visit your site and edit the homepage that it is split between
  112. a:
  113. - Hero area
  114. - Body area
  115. - A promotional area
  116. - Moveable featured site sections
  117. """
  118. # Hero section of HomePage
  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='Homepage image'
  126. )
  127. hero_text = models.CharField(
  128. max_length=255,
  129. help_text='Write an introduction for the bakery'
  130. )
  131. hero_cta = models.CharField(
  132. verbose_name='Hero CTA',
  133. max_length=255,
  134. help_text='Text to display on Call to Action'
  135. )
  136. hero_cta_link = models.ForeignKey(
  137. 'wagtailcore.Page',
  138. null=True,
  139. blank=True,
  140. on_delete=models.SET_NULL,
  141. related_name='+',
  142. verbose_name='Hero CTA link',
  143. help_text='Choose a page to link to for the Call to Action'
  144. )
  145. # Body section of the HomePage
  146. body = StreamField(
  147. BaseStreamBlock(), verbose_name="Home content block", blank=True, use_json_field=True
  148. )
  149. # Promo section of the HomePage
  150. promo_image = models.ForeignKey(
  151. 'wagtailimages.Image',
  152. null=True,
  153. blank=True,
  154. on_delete=models.SET_NULL,
  155. related_name='+',
  156. help_text='Promo image'
  157. )
  158. promo_title = models.CharField(
  159. blank=True,
  160. max_length=255,
  161. help_text='Title to display above the promo copy'
  162. )
  163. promo_text = RichTextField(
  164. null=True,
  165. blank=True,
  166. help_text='Write some promotional copy'
  167. )
  168. # Featured sections on the HomePage
  169. # You will see on templates/base/home_page.html that these are treated
  170. # in different ways, and displayed in different areas of the page.
  171. # Each list their children items that we access via the children function
  172. # that we define on the individual Page models e.g. BlogIndexPage
  173. featured_section_1_title = models.CharField(
  174. blank=True,
  175. max_length=255,
  176. help_text='Title to display above the promo copy'
  177. )
  178. featured_section_1 = models.ForeignKey(
  179. 'wagtailcore.Page',
  180. null=True,
  181. blank=True,
  182. on_delete=models.SET_NULL,
  183. related_name='+',
  184. help_text='First featured section for the homepage. Will display up to '
  185. 'three child items.',
  186. verbose_name='Featured section 1'
  187. )
  188. featured_section_2_title = models.CharField(
  189. blank=True,
  190. max_length=255,
  191. help_text='Title to display above the promo copy'
  192. )
  193. featured_section_2 = models.ForeignKey(
  194. 'wagtailcore.Page',
  195. null=True,
  196. blank=True,
  197. on_delete=models.SET_NULL,
  198. related_name='+',
  199. help_text='Second featured section for the homepage. Will display up to '
  200. 'three child items.',
  201. verbose_name='Featured section 2'
  202. )
  203. featured_section_3_title = models.CharField(
  204. blank=True,
  205. max_length=255,
  206. help_text='Title to display above the promo copy'
  207. )
  208. featured_section_3 = models.ForeignKey(
  209. 'wagtailcore.Page',
  210. null=True,
  211. blank=True,
  212. on_delete=models.SET_NULL,
  213. related_name='+',
  214. help_text='Third featured section for the homepage. Will display up to '
  215. 'six child items.',
  216. verbose_name='Featured section 3'
  217. )
  218. content_panels = Page.content_panels + [
  219. MultiFieldPanel([
  220. FieldPanel('image'),
  221. FieldPanel('hero_text', classname="full"),
  222. MultiFieldPanel([
  223. FieldPanel('hero_cta'),
  224. FieldPanel('hero_cta_link'),
  225. ]),
  226. ], heading="Hero section"),
  227. MultiFieldPanel([
  228. FieldPanel('promo_image'),
  229. FieldPanel('promo_title'),
  230. FieldPanel('promo_text'),
  231. ], heading="Promo section"),
  232. FieldPanel('body'),
  233. MultiFieldPanel([
  234. MultiFieldPanel([
  235. FieldPanel('featured_section_1_title'),
  236. FieldPanel('featured_section_1'),
  237. ]),
  238. MultiFieldPanel([
  239. FieldPanel('featured_section_2_title'),
  240. FieldPanel('featured_section_2'),
  241. ]),
  242. MultiFieldPanel([
  243. FieldPanel('featured_section_3_title'),
  244. FieldPanel('featured_section_3'),
  245. ]),
  246. ], heading="Featured homepage sections", classname="collapsible")
  247. ]
  248. def __str__(self):
  249. return self.title
  250. class GalleryPage(Page):
  251. """
  252. This is a page to list locations from the selected Collection. We use a Q
  253. object to list any Collection created (/admin/collections/) even if they
  254. contain no items. In this demo we use it for a GalleryPage,
  255. and is intended to show the extensibility of this aspect of Wagtail
  256. """
  257. introduction = models.TextField(
  258. help_text='Text to describe the page',
  259. blank=True)
  260. image = models.ForeignKey(
  261. 'wagtailimages.Image',
  262. null=True,
  263. blank=True,
  264. on_delete=models.SET_NULL,
  265. related_name='+',
  266. help_text='Landscape mode only; horizontal width between 1000px and '
  267. '3000px.'
  268. )
  269. body = StreamField(
  270. BaseStreamBlock(), verbose_name="Page body", blank=True, use_json_field=True
  271. )
  272. collection = models.ForeignKey(
  273. Collection,
  274. limit_choices_to=~models.Q(name__in=['Root']),
  275. null=True,
  276. blank=True,
  277. on_delete=models.SET_NULL,
  278. help_text='Select the image collection for this gallery.'
  279. )
  280. content_panels = Page.content_panels + [
  281. FieldPanel('introduction', classname="full"),
  282. FieldPanel('body'),
  283. FieldPanel('image'),
  284. FieldPanel('collection'),
  285. ]
  286. # Defining what content type can sit under the parent. Since it's a blank
  287. # array no subpage can be added
  288. subpage_types = []
  289. class FormField(AbstractFormField):
  290. """
  291. Wagtailforms is a module to introduce simple forms on a Wagtail site. It
  292. isn't intended as a replacement to Django's form support but as a quick way
  293. to generate a general purpose data-collection form or contact form
  294. without having to write code. We use it on the site for a contact form. You
  295. can read more about Wagtail forms at:
  296. https://docs.wagtail.org/en/stable/reference/contrib/forms/index.html
  297. """
  298. page = ParentalKey('FormPage', related_name='form_fields', on_delete=models.CASCADE)
  299. class FormPage(AbstractEmailForm):
  300. image = models.ForeignKey(
  301. 'wagtailimages.Image',
  302. null=True,
  303. blank=True,
  304. on_delete=models.SET_NULL,
  305. related_name='+'
  306. )
  307. body = StreamField(BaseStreamBlock(), use_json_field=True)
  308. thank_you_text = RichTextField(blank=True)
  309. # Note how we include the FormField object via an InlinePanel using the
  310. # related_name value
  311. content_panels = AbstractEmailForm.content_panels + [
  312. FieldPanel('image'),
  313. FieldPanel('body'),
  314. InlinePanel('form_fields', label="Form fields"),
  315. FieldPanel('thank_you_text', classname="full"),
  316. MultiFieldPanel([
  317. FieldRowPanel([
  318. FieldPanel('from_address', classname="col6"),
  319. FieldPanel('to_address', classname="col6"),
  320. ]),
  321. FieldPanel('subject'),
  322. ], "Email"),
  323. ]