2
0

models.py 15 KB

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