models.py 12 KB

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