models.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. from __future__ import unicode_literals
  2. from django.contrib import messages
  3. from django.db import models
  4. from django.shortcuts import redirect, render
  5. from modelcluster.contrib.taggit import ClusterTaggableManager
  6. from modelcluster.fields import ParentalKey
  7. from taggit.models import Tag, TaggedItemBase
  8. from wagtail.contrib.wagtailroutablepage.models import RoutablePageMixin, route
  9. from wagtail.wagtailadmin.edit_handlers import FieldPanel, InlinePanel
  10. from wagtail.wagtailcore.models import Page, Orderable
  11. from wagtail.wagtailimages.edit_handlers import ImageChooserPanel
  12. from wagtail.wagtailsearch import index
  13. from wagtail.wagtailsnippets.edit_handlers import SnippetChooserPanel
  14. from bakerydemo.base.models import BasePageFieldsMixin
  15. class BlogPeopleRelationship(Orderable, models.Model):
  16. """
  17. This defines the relationship between the `People` 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 People using
  20. the ParentalKey and ForeignKey
  21. """
  22. page = ParentalKey(
  23. 'BlogPage', related_name='blog_person_relationship'
  24. )
  25. people = models.ForeignKey(
  26. 'base.People', related_name='person_blog_relationship'
  27. )
  28. panels = [
  29. SnippetChooserPanel('people')
  30. ]
  31. class BlogPageTag(TaggedItemBase):
  32. """
  33. This model allows us to creates a many-to-many relationship between
  34. the BlogPage object and tags. There's a longer guide on using it at
  35. http://docs.wagtail.io/en/v1.9/reference/pages/model_recipes.html#tagging
  36. """
  37. content_object = ParentalKey('BlogPage', related_name='tagged_items')
  38. class BlogPage(BasePageFieldsMixin, Page):
  39. """
  40. A Blog Page
  41. We access the People object with an inline panel that references the
  42. ParentalKey's related_name in BlogPeopleRelationship. More docs:
  43. http://docs.wagtail.io/en/v1.9/topics/pages.html#inline-models
  44. """
  45. subtitle = models.CharField(blank=True, max_length=255)
  46. tags = ClusterTaggableManager(through=BlogPageTag, blank=True)
  47. date_published = models.DateField(
  48. "Date article published", blank=True, null=True
  49. )
  50. content_panels = BasePageFieldsMixin.content_panels + [
  51. FieldPanel('date_published'),
  52. InlinePanel(
  53. 'blog_person_relationship', label="Author(s)",
  54. panels=None, min_num=1),
  55. FieldPanel('tags'),
  56. ]
  57. # Inject subtitle panel after title field
  58. title_index = next((i for i, panel in enumerate(content_panels) if panel.field_name == 'title'), -1) # noqa
  59. content_panels.insert(title_index + 1, FieldPanel('subtitle'))
  60. search_fields = Page.search_fields + [
  61. index.SearchField('title'),
  62. index.SearchField('body'),
  63. ]
  64. def authors(self):
  65. """
  66. Returns the BlogPage's related People. Again note that we are using
  67. the ParentalKey's related_name from the BlogPeopleRelationship model
  68. to access these objects. This allows us to access the People objects
  69. with a loop on the template. If we tried to access the blog_person_
  70. relationship directly we'd print `blog.BlogPeopleRelationship.None`
  71. """
  72. authors = [
  73. n.people for n in self.blog_person_relationship.all()
  74. ]
  75. return authors
  76. @property
  77. def get_tags(self):
  78. """
  79. Similar to the authors function above we're returning all the tags that
  80. are related to the blog post into a list we can access on the template.
  81. We're additionally adding a URL to access BlogPage objects with that tag
  82. """
  83. tags = self.tags.all()
  84. for tag in tags:
  85. tag.url = '/'+'/'.join(s.strip('/') for s in [
  86. self.get_parent().url,
  87. 'tags',
  88. tag.slug
  89. ])
  90. return tags
  91. # Defines parent to BlogPage as being BlogIndexPages
  92. parent_page_types = ['BlogIndexPage']
  93. # Defines what content types can exist as children of BlogPage.
  94. # Empty list means that no child content types are allowed.
  95. subpage_types = []
  96. class BlogIndexPage(BasePageFieldsMixin, RoutablePageMixin, Page):
  97. """
  98. Index page for blogs.
  99. We need to alter the page model's context to return the child page objects,
  100. the BlogPage objects, so that it works as an index page
  101. RoutablePageMixin is used to allow for a custom sub-URL for the tag views
  102. defined above.
  103. """
  104. content_panels = Page.content_panels + [
  105. FieldPanel('introduction', classname="full"),
  106. ImageChooserPanel('image'),
  107. ]
  108. # Defines what pages types can live under this page type
  109. subpage_types = ['BlogPage']
  110. # Defines a function to access the children of the page (e.g. BlogPage
  111. # objects). On the demo site we use this on the HomePage
  112. def children(self):
  113. return self.get_children().specific().live()
  114. # Overrides the context to list all child items, that are live, by the
  115. # date that they were published
  116. # http://docs.wagtail.io/en/v1.9/getting_started/tutorial.html#overriding-context
  117. def get_context(self, request):
  118. context = super(BlogIndexPage, self).get_context(request)
  119. context['posts'] = BlogPage.objects.descendant_of(
  120. self).live().order_by(
  121. '-date_published')
  122. return context
  123. # This defines a Custom view that utilizes Tags. This view will return all
  124. # related BlogPages for a given Tag or redirect back to the BlogIndexPage.
  125. # More information on RoutablePages is at
  126. # http://docs.wagtail.io/en/v1.9/reference/contrib/routablepage.html
  127. @route('^tags/$', name='tag_archive')
  128. @route('^tags/(\w+)/$', name='tag_archive')
  129. def tag_archive(self, request, tag=None):
  130. try:
  131. tag = Tag.objects.get(slug=tag)
  132. except Tag.DoesNotExist:
  133. if tag:
  134. msg = 'There are no blog posts tagged with "{}"'.format(tag)
  135. messages.add_message(request, messages.INFO, msg)
  136. return redirect(self.url)
  137. posts = self.get_posts(tag=tag)
  138. context = {
  139. 'tag': tag,
  140. 'posts': posts
  141. }
  142. return render(request, 'blog/blog_index_page.html', context)
  143. # Returns the child BlogPage objects for this BlogPageIndex.
  144. # If a tag is used then it will filter the posts by tag.
  145. def get_posts(self, tag=None):
  146. posts = BlogPage.objects.live().descendant_of(self)
  147. if tag:
  148. posts = posts.filter(tags=tag)
  149. return posts
  150. # Returns the list of Tags for all child posts of this BlogPage.
  151. def get_child_tags(self):
  152. tags = []
  153. for post in self.get_posts():
  154. # Not tags.append() because we don't want a list of lists
  155. tags += post.get_tags
  156. tags = sorted(set(tags))
  157. return tags