models.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. from __future__ import unicode_literals
  2. from django.contrib.contenttypes.fields import GenericRelation
  3. from django.db import models
  4. from django.utils.translation import gettext as _
  5. from modelcluster.fields import ParentalKey
  6. from modelcluster.models import ClusterableModel
  7. from wagtail.admin.panels import (
  8. FieldPanel,
  9. FieldRowPanel,
  10. InlinePanel,
  11. MultiFieldPanel,
  12. PublishingPanel,
  13. )
  14. from wagtail.contrib.forms.models import AbstractEmailForm, AbstractFormField
  15. from wagtail.contrib.settings.models import (
  16. BaseGenericSetting,
  17. BaseSiteSetting,
  18. register_setting,
  19. )
  20. from wagtail.fields import RichTextField, StreamField
  21. from wagtail.models import (
  22. Collection,
  23. DraftStateMixin,
  24. LockableMixin,
  25. Page,
  26. PreviewableMixin,
  27. RevisionMixin,
  28. TranslatableMixin,
  29. WorkflowMixin,
  30. )
  31. from wagtail.search import index
  32. from .blocks import BaseStreamBlock
  33. class Person(
  34. WorkflowMixin,
  35. DraftStateMixin,
  36. LockableMixin,
  37. RevisionMixin,
  38. PreviewableMixin,
  39. index.Indexed,
  40. ClusterableModel,
  41. ):
  42. """
  43. A Django model to store Person objects.
  44. It is registered using `register_snippet` as a function in wagtail_hooks.py
  45. to allow it to have a menu item within a custom menu item group.
  46. `Person` uses the `ClusterableModel`, which allows the relationship with
  47. another model to be stored locally to the 'parent' model (e.g. a PageModel)
  48. until the parent is explicitly saved. This allows the editor to use the
  49. 'Preview' button, to preview the content, without saving the relationships
  50. to the database.
  51. https://github.com/wagtail/django-modelcluster
  52. """
  53. first_name = models.CharField("First name", max_length=254)
  54. last_name = models.CharField("Last name", max_length=254)
  55. job_title = models.CharField("Job title", max_length=254)
  56. image = models.ForeignKey(
  57. "wagtailimages.Image",
  58. null=True,
  59. blank=True,
  60. on_delete=models.SET_NULL,
  61. related_name="+",
  62. )
  63. workflow_states = GenericRelation(
  64. "wagtailcore.WorkflowState",
  65. content_type_field="base_content_type",
  66. object_id_field="object_id",
  67. related_query_name="person",
  68. for_concrete_model=False,
  69. )
  70. revisions = GenericRelation(
  71. "wagtailcore.Revision",
  72. content_type_field="base_content_type",
  73. object_id_field="object_id",
  74. related_query_name="person",
  75. for_concrete_model=False,
  76. )
  77. panels = [
  78. MultiFieldPanel(
  79. [
  80. FieldRowPanel(
  81. [
  82. FieldPanel("first_name"),
  83. FieldPanel("last_name"),
  84. ]
  85. )
  86. ],
  87. "Name",
  88. ),
  89. FieldPanel("job_title"),
  90. FieldPanel("image"),
  91. PublishingPanel(),
  92. ]
  93. search_fields = [
  94. index.SearchField("first_name"),
  95. index.SearchField("last_name"),
  96. index.FilterField("job_title"),
  97. index.AutocompleteField("first_name"),
  98. index.AutocompleteField("last_name"),
  99. ]
  100. @property
  101. def thumb_image(self):
  102. # Returns an empty string if there is no profile pic or the rendition
  103. # file can't be found.
  104. try:
  105. return self.image.get_rendition("fill-50x50").img_tag()
  106. except: # noqa: E722 FIXME: remove bare 'except:'
  107. return ""
  108. @property
  109. def preview_modes(self):
  110. return PreviewableMixin.DEFAULT_PREVIEW_MODES + [("blog_post", _("Blog post"))]
  111. def __str__(self):
  112. return "{} {}".format(self.first_name, self.last_name)
  113. def get_preview_template(self, request, mode_name):
  114. from bakerydemo.blog.models import BlogPage
  115. if mode_name == "blog_post":
  116. return BlogPage.template
  117. return "base/preview/person.html"
  118. def get_preview_context(self, request, mode_name):
  119. from bakerydemo.blog.models import BlogPage
  120. context = super().get_preview_context(request, mode_name)
  121. if mode_name == self.default_preview_mode:
  122. return context
  123. page = BlogPage.objects.filter(blog_person_relationship__person=self).first()
  124. if page:
  125. # Use the page authored by this person if available,
  126. # and replace the instance from the database with the edited instance
  127. page.authors = [
  128. self if author.pk == self.pk else author for author in page.authors()
  129. ]
  130. # The authors() method only shows live authors, so make sure the instance
  131. # is included even if it's not live as this is just a preview
  132. if not self.live:
  133. page.authors.append(self)
  134. else:
  135. # Otherwise, get the first page and simulate the person as the author
  136. page = BlogPage.objects.first()
  137. page.authors = [self]
  138. context["page"] = page
  139. return context
  140. class Meta:
  141. verbose_name = "Person"
  142. verbose_name_plural = "People"
  143. class FooterText(
  144. DraftStateMixin,
  145. RevisionMixin,
  146. PreviewableMixin,
  147. TranslatableMixin,
  148. models.Model,
  149. ):
  150. """
  151. This provides editable text for the site footer. Again it is registered
  152. using `register_snippet` as a function in wagtail_hooks.py to be grouped
  153. together with the Person model inside the same main menu item. It is made
  154. accessible on the template via a template tag defined in base/templatetags/
  155. navigation_tags.py
  156. """
  157. body = RichTextField()
  158. revisions = GenericRelation(
  159. "wagtailcore.Revision",
  160. content_type_field="base_content_type",
  161. object_id_field="object_id",
  162. related_query_name="footer_text",
  163. for_concrete_model=False,
  164. )
  165. panels = [
  166. FieldPanel("body"),
  167. PublishingPanel(),
  168. ]
  169. def __str__(self):
  170. return "Footer text"
  171. def get_preview_template(self, request, mode_name):
  172. return "base.html"
  173. def get_preview_context(self, request, mode_name):
  174. return {"footer_text": self.body}
  175. class Meta(TranslatableMixin.Meta):
  176. verbose_name_plural = "Footer Text"
  177. class StandardPage(Page):
  178. """
  179. A generic content page. On this demo site we use it for an about page but
  180. it could be used for any type of page content that only needs a title,
  181. image, introduction and body field
  182. """
  183. introduction = models.TextField(help_text="Text to describe the page", blank=True)
  184. image = models.ForeignKey(
  185. "wagtailimages.Image",
  186. null=True,
  187. blank=True,
  188. on_delete=models.SET_NULL,
  189. related_name="+",
  190. help_text="Landscape mode only; horizontal width between 1000px and 3000px.",
  191. )
  192. body = StreamField(
  193. BaseStreamBlock(), verbose_name="Page body", blank=True, use_json_field=True
  194. )
  195. content_panels = Page.content_panels + [
  196. FieldPanel("introduction"),
  197. FieldPanel("body"),
  198. FieldPanel("image"),
  199. ]
  200. class HomePage(Page):
  201. """
  202. The Home Page. This looks slightly more complicated than it is. You can
  203. see if you visit your site and edit the homepage that it is split between
  204. a:
  205. - Hero area
  206. - Body area
  207. - A promotional area
  208. - Moveable featured site sections
  209. """
  210. # Hero section of HomePage
  211. image = models.ForeignKey(
  212. "wagtailimages.Image",
  213. null=True,
  214. blank=True,
  215. on_delete=models.SET_NULL,
  216. related_name="+",
  217. help_text="Homepage image",
  218. )
  219. hero_text = models.CharField(
  220. max_length=255, help_text="Write an introduction for the bakery"
  221. )
  222. hero_cta = models.CharField(
  223. verbose_name="Hero CTA",
  224. max_length=255,
  225. help_text="Text to display on Call to Action",
  226. )
  227. hero_cta_link = models.ForeignKey(
  228. "wagtailcore.Page",
  229. null=True,
  230. blank=True,
  231. on_delete=models.SET_NULL,
  232. related_name="+",
  233. verbose_name="Hero CTA link",
  234. help_text="Choose a page to link to for the Call to Action",
  235. )
  236. # Body section of the HomePage
  237. body = StreamField(
  238. BaseStreamBlock(),
  239. verbose_name="Home content block",
  240. blank=True,
  241. use_json_field=True,
  242. )
  243. # Promo section of the HomePage
  244. promo_image = models.ForeignKey(
  245. "wagtailimages.Image",
  246. null=True,
  247. blank=True,
  248. on_delete=models.SET_NULL,
  249. related_name="+",
  250. help_text="Promo image",
  251. )
  252. promo_title = models.CharField(
  253. blank=True, max_length=255, help_text="Title to display above the promo copy"
  254. )
  255. promo_text = RichTextField(
  256. null=True, blank=True, max_length=1000, help_text="Write some promotional copy"
  257. )
  258. # Featured sections on the HomePage
  259. # You will see on templates/base/home_page.html that these are treated
  260. # in different ways, and displayed in different areas of the page.
  261. # Each list their children items that we access via the children function
  262. # that we define on the individual Page models e.g. BlogIndexPage
  263. featured_section_1_title = models.CharField(
  264. blank=True, max_length=255, help_text="Title to display above the promo copy"
  265. )
  266. featured_section_1 = models.ForeignKey(
  267. "wagtailcore.Page",
  268. null=True,
  269. blank=True,
  270. on_delete=models.SET_NULL,
  271. related_name="+",
  272. help_text="First featured section for the homepage. Will display up to "
  273. "three child items.",
  274. verbose_name="Featured section 1",
  275. )
  276. featured_section_2_title = models.CharField(
  277. blank=True, max_length=255, help_text="Title to display above the promo copy"
  278. )
  279. featured_section_2 = models.ForeignKey(
  280. "wagtailcore.Page",
  281. null=True,
  282. blank=True,
  283. on_delete=models.SET_NULL,
  284. related_name="+",
  285. help_text="Second featured section for the homepage. Will display up to "
  286. "three child items.",
  287. verbose_name="Featured section 2",
  288. )
  289. featured_section_3_title = models.CharField(
  290. blank=True, max_length=255, help_text="Title to display above the promo copy"
  291. )
  292. featured_section_3 = models.ForeignKey(
  293. "wagtailcore.Page",
  294. null=True,
  295. blank=True,
  296. on_delete=models.SET_NULL,
  297. related_name="+",
  298. help_text="Third featured section for the homepage. Will display up to "
  299. "six child items.",
  300. verbose_name="Featured section 3",
  301. )
  302. content_panels = Page.content_panels + [
  303. MultiFieldPanel(
  304. [
  305. FieldPanel("image"),
  306. FieldPanel("hero_text"),
  307. MultiFieldPanel(
  308. [
  309. FieldPanel("hero_cta"),
  310. FieldPanel("hero_cta_link"),
  311. ]
  312. ),
  313. ],
  314. heading="Hero section",
  315. ),
  316. MultiFieldPanel(
  317. [
  318. FieldPanel("promo_image"),
  319. FieldPanel("promo_title"),
  320. FieldPanel("promo_text"),
  321. ],
  322. heading="Promo section",
  323. ),
  324. FieldPanel("body"),
  325. MultiFieldPanel(
  326. [
  327. MultiFieldPanel(
  328. [
  329. FieldPanel("featured_section_1_title"),
  330. FieldPanel("featured_section_1"),
  331. ]
  332. ),
  333. MultiFieldPanel(
  334. [
  335. FieldPanel("featured_section_2_title"),
  336. FieldPanel("featured_section_2"),
  337. ]
  338. ),
  339. MultiFieldPanel(
  340. [
  341. FieldPanel("featured_section_3_title"),
  342. FieldPanel("featured_section_3"),
  343. ]
  344. ),
  345. ],
  346. heading="Featured homepage sections",
  347. ),
  348. ]
  349. def __str__(self):
  350. return self.title
  351. class GalleryPage(Page):
  352. """
  353. This is a page to list locations from the selected Collection. We use a Q
  354. object to list any Collection created (/admin/collections/) even if they
  355. contain no items. In this demo we use it for a GalleryPage,
  356. and is intended to show the extensibility of this aspect of Wagtail
  357. """
  358. introduction = models.TextField(help_text="Text to describe the page", blank=True)
  359. image = models.ForeignKey(
  360. "wagtailimages.Image",
  361. null=True,
  362. blank=True,
  363. on_delete=models.SET_NULL,
  364. related_name="+",
  365. help_text="Landscape mode only; horizontal width between 1000px and " "3000px.",
  366. )
  367. body = StreamField(
  368. BaseStreamBlock(), verbose_name="Page body", blank=True, use_json_field=True
  369. )
  370. collection = models.ForeignKey(
  371. Collection,
  372. limit_choices_to=~models.Q(name__in=["Root"]),
  373. null=True,
  374. blank=True,
  375. on_delete=models.SET_NULL,
  376. help_text="Select the image collection for this gallery.",
  377. )
  378. content_panels = Page.content_panels + [
  379. FieldPanel("introduction"),
  380. FieldPanel("body"),
  381. FieldPanel("image"),
  382. FieldPanel("collection"),
  383. ]
  384. # Defining what content type can sit under the parent. Since it's a blank
  385. # array no subpage can be added
  386. subpage_types = []
  387. class FormField(AbstractFormField):
  388. """
  389. Wagtailforms is a module to introduce simple forms on a Wagtail site. It
  390. isn't intended as a replacement to Django's form support but as a quick way
  391. to generate a general purpose data-collection form or contact form
  392. without having to write code. We use it on the site for a contact form. You
  393. can read more about Wagtail forms at:
  394. https://docs.wagtail.org/en/stable/reference/contrib/forms/index.html
  395. """
  396. page = ParentalKey("FormPage", related_name="form_fields", on_delete=models.CASCADE)
  397. class FormPage(AbstractEmailForm):
  398. image = models.ForeignKey(
  399. "wagtailimages.Image",
  400. null=True,
  401. blank=True,
  402. on_delete=models.SET_NULL,
  403. related_name="+",
  404. )
  405. body = StreamField(BaseStreamBlock(), use_json_field=True)
  406. thank_you_text = RichTextField(blank=True)
  407. # Note how we include the FormField object via an InlinePanel using the
  408. # related_name value
  409. content_panels = AbstractEmailForm.content_panels + [
  410. FieldPanel("image"),
  411. FieldPanel("body"),
  412. InlinePanel("form_fields", heading="Form fields", label="Field"),
  413. FieldPanel("thank_you_text"),
  414. MultiFieldPanel(
  415. [
  416. FieldRowPanel(
  417. [
  418. FieldPanel("from_address"),
  419. FieldPanel("to_address"),
  420. ]
  421. ),
  422. FieldPanel("subject"),
  423. ],
  424. "Email",
  425. ),
  426. ]
  427. @register_setting(icon="cog")
  428. class GenericSettings(ClusterableModel, BaseGenericSetting):
  429. twitter_url = models.URLField(verbose_name="Twitter URL", blank=True)
  430. github_url = models.URLField(verbose_name="GitHub URL", blank=True)
  431. organisation_url = models.URLField(verbose_name="Organisation URL", blank=True)
  432. panels = [
  433. MultiFieldPanel(
  434. [
  435. FieldPanel("github_url"),
  436. FieldPanel("twitter_url"),
  437. FieldPanel("organisation_url"),
  438. ],
  439. "Social settings",
  440. )
  441. ]
  442. @register_setting(icon="site")
  443. class SiteSettings(BaseSiteSetting):
  444. title_suffix = models.CharField(
  445. verbose_name="Title suffix",
  446. max_length=255,
  447. help_text="The suffix for the title meta tag e.g. ' | The Wagtail Bakery'",
  448. default="The Wagtail Bakery",
  449. )
  450. panels = [
  451. FieldPanel("title_suffix"),
  452. ]