models.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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.fields import ParentalKey
  6. from modelcluster.contrib.taggit import ClusterTaggableManager
  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,
  11. InlinePanel,
  12. StreamFieldPanel,
  13. )
  14. from wagtail.wagtailimages.edit_handlers import ImageChooserPanel
  15. from wagtail.wagtailcore.fields import StreamField
  16. from wagtail.wagtailcore.models import Page, Orderable
  17. from wagtail.wagtailsnippets.edit_handlers import SnippetChooserPanel
  18. from bakerydemo.base.blocks import BaseStreamBlock
  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. image = models.ForeignKey(
  40. 'wagtailimages.Image',
  41. null=True,
  42. blank=True,
  43. on_delete=models.SET_NULL,
  44. related_name='+',
  45. help_text='Location image'
  46. )
  47. tags = ClusterTaggableManager(through=BlogPageTag, blank=True)
  48. date_published = models.DateField("Date article published", blank=True, null=True)
  49. body = StreamField(
  50. BaseStreamBlock(), verbose_name="Blog post", blank=True
  51. )
  52. content_panels = Page.content_panels + [
  53. ImageChooserPanel('image'),
  54. StreamFieldPanel('body'),
  55. FieldPanel('date_published'),
  56. InlinePanel(
  57. 'blog_person_relationship', label="Author(s)",
  58. panels=None, min_num=1),
  59. FieldPanel('tags'),
  60. ]
  61. def authors(self):
  62. '''
  63. Returns the BlogPage's related People
  64. '''
  65. authors = [
  66. n.people for n in self.blog_person_relationship.all()
  67. ]
  68. return authors
  69. @property
  70. def get_tags(self):
  71. '''
  72. Returns the BlogPage's related list of Tags.
  73. Each Tag is modified to include a url attribute
  74. '''
  75. tags = self.tags.all()
  76. for tag in tags:
  77. tag.url = '/'+'/'.join(s.strip('/') for s in [
  78. self.get_parent().url,
  79. 'tags',
  80. tag.slug
  81. ])
  82. return tags
  83. parent_page_types = ['BlogIndexPage']
  84. # Defining what content type can sit under the parent
  85. # The empty array means that no children can be placed under the
  86. # LocationPage page model
  87. subpage_types = []
  88. # api_fields = ['image', 'body']
  89. class BlogIndexPage(RoutablePageMixin, Page):
  90. '''
  91. Index page for blogs.
  92. We need to alter the page model's context to return the child page objects - the
  93. BlogPage - so that it works as an index page
  94. The RoutablePageMixin is used to allow for a custom sub-URL
  95. '''
  96. image = models.ForeignKey(
  97. 'wagtailimages.Image',
  98. null=True,
  99. blank=True,
  100. on_delete=models.SET_NULL,
  101. related_name='+',
  102. help_text='Location listing image'
  103. )
  104. introduction = models.TextField(
  105. help_text='Text to describe the index page',
  106. blank=True)
  107. content_panels = Page.content_panels + [
  108. ImageChooserPanel('image'),
  109. FieldPanel('introduction')
  110. ]
  111. # parent_page_types = [
  112. # 'home.HomePage'
  113. # ]
  114. # Defining what content type can sit under the parent. Since it's a blank
  115. # array no subpage can be added
  116. subpage_types = ['BlogPage']
  117. def get_context(self, request):
  118. context = super(BlogIndexPage, self).get_context(request)
  119. context['blogs'] = BlogPage.objects.descendant_of(
  120. self).live().order_by(
  121. '-first_published_at')
  122. return context
  123. @route('^tags/$', name='tag_archive')
  124. @route('^tags/(\w+)/$', name='tag_archive')
  125. def tag_archive(self, request, tag=None):
  126. '''
  127. A Custom view that utilizes Tags. This view will
  128. return all related BlogPages for a given Tag or redirect back to
  129. the BlogIndexPage
  130. '''
  131. try:
  132. tag = Tag.objects.get(slug=tag)
  133. except Tag.DoesNotExist:
  134. if tag:
  135. msg = 'There are no blog posts tagged with "{}"'.format(tag)
  136. messages.add_message(request, messages.INFO, msg)
  137. return redirect(self.url)
  138. blogs = BlogPage.objects.filter(tags=tag).live().descendant_of(self)
  139. context = {
  140. 'title': 'Posts tagged with: {}'.format(tag.name),
  141. 'blogs': blogs
  142. }
  143. return render(request, 'blog/blog_index_page.html', context)
  144. # api_fields = ['introduction']