models.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. from django.contrib import messages
  2. from django.db import models
  3. from django.shortcuts import redirect, render
  4. from modelcluster.contrib.taggit import ClusterTaggableManager
  5. from modelcluster.fields import ParentalKey
  6. from taggit.models import Tag, TaggedItemBase
  7. from wagtail.admin.panels import FieldPanel, MultipleChooserPanel
  8. from wagtail.api import APIField
  9. from wagtail.contrib.routable_page.models import RoutablePageMixin, route
  10. from wagtail.fields import StreamField
  11. from wagtail.models import Orderable, Page
  12. from wagtail.search import index
  13. from bakerydemo.base.blocks import BaseStreamBlock
  14. from bakerydemo.headless import CustomHeadlessMixin
  15. class BlogPersonRelationship(Orderable, models.Model):
  16. """
  17. This defines the relationship between the `Person` within the `base`
  18. app and the BlogPage below. This allows people to be added to a BlogPage.
  19. We have created a two way relationship between BlogPage and Person using
  20. the ParentalKey and ForeignKey
  21. """
  22. page = ParentalKey(
  23. "BlogPage", related_name="blog_person_relationship", on_delete=models.CASCADE
  24. )
  25. person = models.ForeignKey(
  26. "base.Person", related_name="person_blog_relationship", on_delete=models.CASCADE
  27. )
  28. panels = [FieldPanel("person")]
  29. api_fields = [
  30. APIField("page"),
  31. APIField("person"),
  32. ]
  33. class BlogPageTag(TaggedItemBase):
  34. """
  35. This model allows us to create a many-to-many relationship between
  36. the BlogPage object and tags. There's a longer guide on using it at
  37. https://docs.wagtail.org/en/stable/reference/pages/model_recipes.html#tagging
  38. """
  39. content_object = ParentalKey(
  40. "BlogPage", related_name="tagged_items", on_delete=models.CASCADE
  41. )
  42. class BlogPage(CustomHeadlessMixin, Page):
  43. """
  44. A Blog Page
  45. We access the Person object with an inline panel that references the
  46. ParentalKey's related_name in BlogPersonRelationship. More docs:
  47. https://docs.wagtail.org/en/stable/topics/pages.html#inline-models
  48. """
  49. introduction = models.TextField(help_text="Text to describe the page", blank=True)
  50. image = models.ForeignKey(
  51. "wagtailimages.Image",
  52. null=True,
  53. blank=True,
  54. on_delete=models.SET_NULL,
  55. related_name="+",
  56. help_text="Landscape mode only; horizontal width between 1000px and 3000px.",
  57. )
  58. body = StreamField(
  59. BaseStreamBlock(), verbose_name="Page body", blank=True, use_json_field=True
  60. )
  61. subtitle = models.CharField(blank=True, max_length=255)
  62. tags = ClusterTaggableManager(through=BlogPageTag, blank=True)
  63. date_published = models.DateField("Date article published", blank=True, null=True)
  64. content_panels = Page.content_panels + [
  65. FieldPanel("subtitle"),
  66. FieldPanel("introduction"),
  67. FieldPanel("image"),
  68. FieldPanel("body"),
  69. FieldPanel("date_published"),
  70. MultipleChooserPanel(
  71. "blog_person_relationship",
  72. chooser_field_name="person",
  73. heading="Authors",
  74. label="Author",
  75. panels=None,
  76. min_num=1,
  77. ),
  78. FieldPanel("tags"),
  79. ]
  80. search_fields = Page.search_fields + [
  81. index.SearchField("body"),
  82. ]
  83. api_fields = [
  84. APIField("introduction"),
  85. APIField("image"),
  86. APIField("body"),
  87. APIField("subtitle"),
  88. APIField("tags"),
  89. APIField("date_published"),
  90. APIField("blog_person_relationship"),
  91. ]
  92. def authors(self):
  93. """
  94. Returns the BlogPage's related people. Again note that we are using
  95. the ParentalKey's related_name from the BlogPersonRelationship model
  96. to access these objects. This allows us to access the Person objects
  97. with a loop on the template. If we tried to access the blog_person_
  98. relationship directly we'd print `blog.BlogPersonRelationship.None`
  99. """
  100. # Only return authors that are not in draft
  101. return [
  102. n.person
  103. for n in self.blog_person_relationship.filter(
  104. person__live=True
  105. ).select_related("person")
  106. ]
  107. @property
  108. def get_tags(self):
  109. """
  110. Similar to the authors function above we're returning all the tags that
  111. are related to the blog post into a list we can access on the template.
  112. We're additionally adding a URL to access BlogPage objects with that tag
  113. """
  114. tags = self.tags.all()
  115. base_url = self.get_parent().url
  116. for tag in tags:
  117. tag.url = f"{base_url}tags/{tag.slug}/"
  118. return tags
  119. # Specifies parent to BlogPage as being BlogIndexPages
  120. parent_page_types = ["BlogIndexPage"]
  121. # Specifies what content types can exist as children of BlogPage.
  122. # Empty list means that no child content types are allowed.
  123. subpage_types = []
  124. class BlogIndexPage(CustomHeadlessMixin, RoutablePageMixin, Page):
  125. """
  126. Index page for blogs.
  127. We need to alter the page model's context to return the child page objects,
  128. the BlogPage objects, so that it works as an index page
  129. RoutablePageMixin is used to allow for a custom sub-URL for the tag views
  130. defined above.
  131. """
  132. introduction = models.TextField(help_text="Text to describe the page", blank=True)
  133. image = models.ForeignKey(
  134. "wagtailimages.Image",
  135. null=True,
  136. blank=True,
  137. on_delete=models.SET_NULL,
  138. related_name="+",
  139. help_text="Landscape mode only; horizontal width between 1000px and 3000px.",
  140. )
  141. content_panels = Page.content_panels + [
  142. FieldPanel("introduction"),
  143. FieldPanel("image"),
  144. ]
  145. api_fields = [
  146. APIField("introduction"),
  147. APIField("image"),
  148. ]
  149. # Specifies that only BlogPage objects can live under this index page
  150. subpage_types = ["BlogPage"]
  151. # Defines a method to access the children of the page (e.g. BlogPage
  152. # objects). On the demo site we use this on the HomePage
  153. def children(self):
  154. return self.get_children().specific().live()
  155. # Overrides the context to list all child items, that are live, by the
  156. # date that they were published
  157. # https://docs.wagtail.org/en/stable/getting_started/tutorial.html#overriding-context
  158. def get_context(self, request):
  159. context = super(BlogIndexPage, self).get_context(request)
  160. context["posts"] = (
  161. BlogPage.objects.descendant_of(self).live().order_by("-date_published")
  162. )
  163. return context
  164. # This defines a Custom view that utilizes Tags. This view will return all
  165. # related BlogPages for a given Tag or redirect back to the BlogIndexPage.
  166. # More information on RoutablePages is at
  167. # https://docs.wagtail.org/en/stable/reference/contrib/routablepage.html
  168. @route(r"^tags/$", name="tag_archive")
  169. @route(r"^tags/([\w-]+)/$", name="tag_archive")
  170. def tag_archive(self, request, tag=None):
  171. try:
  172. tag = Tag.objects.get(slug=tag)
  173. except Tag.DoesNotExist:
  174. if tag:
  175. msg = 'There are no blog posts tagged with "{}"'.format(tag)
  176. messages.add_message(request, messages.INFO, msg)
  177. return redirect(self.url)
  178. posts = self.get_posts(tag=tag)
  179. context = {"self": self, "tag": tag, "posts": posts}
  180. return render(request, "blog/blog_index_page.html", context)
  181. def serve_preview(self, request, mode_name):
  182. # Needed for previews to work
  183. return self.serve(request)
  184. # Returns the child BlogPage objects for this BlogPageIndex.
  185. # If a tag is used then it will filter the posts by tag.
  186. def get_posts(self, tag=None):
  187. posts = BlogPage.objects.live().descendant_of(self)
  188. if tag:
  189. posts = posts.filter(tags=tag)
  190. return posts
  191. # Returns the list of Tags for all child posts of this BlogPage.
  192. def get_child_tags(self):
  193. tags = []
  194. for post in self.get_posts():
  195. # Not tags.append() because we don't want a list of lists
  196. tags += post.get_tags
  197. tags = sorted(set(tags))
  198. return tags