models.py 4.9 KB

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