models.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 (
  10. FieldPanel, InlinePanel, StreamFieldPanel, MultiFieldPanel
  11. )
  12. from wagtail.wagtailcore.fields import StreamField
  13. from wagtail.wagtailcore.models import Page, Orderable
  14. from wagtail.wagtailimages.edit_handlers import ImageChooserPanel
  15. from wagtail.wagtailsearch import index
  16. from wagtail.wagtailsnippets.edit_handlers import SnippetChooserPanel
  17. from bakerydemo.base.blocks import BaseStreamBlock
  18. class BlogPeopleRelationship(Orderable, models.Model):
  19. """
  20. This defines the relationship between the `People` within the `base`
  21. app and the BlogPage below allowing us to add people to a BlogPage.
  22. """
  23. page = ParentalKey(
  24. 'BlogPage', related_name='blog_person_relationship'
  25. )
  26. people = models.ForeignKey(
  27. 'base.People', related_name='person_blog_relationship'
  28. )
  29. panels = [
  30. SnippetChooserPanel('people')
  31. ]
  32. class BlogPageTag(TaggedItemBase):
  33. content_object = ParentalKey('BlogPage', related_name='tagged_items')
  34. class BlogPage(Page):
  35. """
  36. A Blog Page (Post)
  37. """
  38. subtitle = models.CharField(blank=True, max_length=255)
  39. introduction = models.CharField(blank=True, max_length=255)
  40. image = models.ForeignKey(
  41. 'wagtailimages.Image',
  42. null=True,
  43. blank=True,
  44. on_delete=models.SET_NULL,
  45. related_name='+',
  46. help_text='Location image'
  47. )
  48. tags = ClusterTaggableManager(through=BlogPageTag, blank=True)
  49. date_published = models.DateField("Date article published", blank=True, null=True)
  50. body = StreamField(
  51. BaseStreamBlock(), verbose_name="Blog post", blank=True
  52. )
  53. content_panels = Page.content_panels + [
  54. MultiFieldPanel([
  55. FieldPanel('subtitle'),
  56. FieldPanel('introduction'),
  57. ]),
  58. ImageChooserPanel('image'),
  59. StreamFieldPanel('body'),
  60. FieldPanel('date_published'),
  61. InlinePanel(
  62. 'blog_person_relationship', label="Author(s)",
  63. panels=None, min_num=1),
  64. FieldPanel('tags'),
  65. ]
  66. search_fields = Page.search_fields + [
  67. index.SearchField('title'),
  68. index.SearchField('body'),
  69. ]
  70. def authors(self):
  71. """
  72. Returns the BlogPage's related People
  73. """
  74. authors = [
  75. n.people for n in self.blog_person_relationship.all()
  76. ]
  77. return authors
  78. @property
  79. def get_tags(self):
  80. """
  81. Returns the BlogPage's related list of Tags.
  82. Each Tag is modified to include a url attribute
  83. """
  84. tags = self.tags.all()
  85. for tag in tags:
  86. tag.url = '/'+'/'.join(s.strip('/') for s in [
  87. self.get_parent().url,
  88. 'tags',
  89. tag.slug
  90. ])
  91. return tags
  92. parent_page_types = ['BlogIndexPage']
  93. # Define 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(RoutablePageMixin, Page):
  97. """
  98. Index page for blogs.
  99. We need to alter the page model's context to return the child page objects - the
  100. BlogPage - so that it works as an index page
  101. RoutablePageMixin is used to allow for a custom sub-URL for tag views.
  102. """
  103. image = models.ForeignKey(
  104. 'wagtailimages.Image',
  105. null=True,
  106. blank=True,
  107. on_delete=models.SET_NULL,
  108. related_name='+',
  109. help_text='Location listing image'
  110. )
  111. introduction = models.TextField(
  112. help_text='Text to describe the index page',
  113. blank=True)
  114. content_panels = Page.content_panels + [
  115. ImageChooserPanel('image'),
  116. FieldPanel('introduction')
  117. ]
  118. # What pages types can live under this page type?
  119. subpage_types = ['BlogPage']
  120. def get_context(self, request):
  121. context = super(BlogIndexPage, self).get_context(request)
  122. context['blogs'] = BlogPage.objects.descendant_of(
  123. self).live().order_by(
  124. '-first_published_at')
  125. return context
  126. @route('^tags/$', name='tag_archive')
  127. @route('^tags/(\w+)/$', name='tag_archive')
  128. def tag_archive(self, request, tag=None):
  129. """
  130. A Custom view that utilizes Tags. This view will
  131. return all related BlogPages for a given Tag or redirect back to
  132. the BlogIndexPage
  133. """
  134. try:
  135. tag = Tag.objects.get(slug=tag)
  136. except Tag.DoesNotExist:
  137. if tag:
  138. msg = 'There are no blog posts tagged with "{}"'.format(tag)
  139. messages.add_message(request, messages.INFO, msg)
  140. return redirect(self.url)
  141. blogs = BlogPage.objects.filter(tags=tag).live().descendant_of(self)
  142. context = {
  143. 'tag': tag,
  144. 'blogs': blogs
  145. }
  146. return render(request, 'blog/blog_index_page.html', context)