2018-07-26 08:44:14 +01:00
|
|
|
from project.common.blocks import build_stream_field
|
2018-07-26 21:48:30 +01:00
|
|
|
from wagtail.admin.edit_handlers import StreamFieldPanel, FieldPanel
|
2018-07-26 08:44:14 +01:00
|
|
|
from wagtail.search import index
|
|
|
|
from project.common.models import Entity
|
2018-07-26 21:48:30 +01:00
|
|
|
from django.db import models
|
|
|
|
from modelcluster.fields import ParentalKey
|
|
|
|
from modelcluster.contrib.taggit import ClusterTaggableManager
|
2018-08-03 22:36:58 +01:00
|
|
|
from taggit.models import TaggedItemBase
|
2018-07-26 21:48:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
class BlogPageTag(TaggedItemBase):
|
|
|
|
content_object = ParentalKey(
|
|
|
|
'BlogPage',
|
|
|
|
related_name='tagged_items',
|
|
|
|
on_delete=models.CASCADE
|
|
|
|
)
|
2018-07-26 08:44:14 +01:00
|
|
|
|
|
|
|
|
|
|
|
class BlogPage(Entity):
|
|
|
|
body = build_stream_field()
|
|
|
|
|
2018-07-26 21:48:30 +01:00
|
|
|
tags = ClusterTaggableManager(through=BlogPageTag, blank=True)
|
|
|
|
|
2018-07-26 08:44:14 +01:00
|
|
|
search_fields = Entity.search_fields + [
|
|
|
|
index.SearchField('body'),
|
|
|
|
]
|
|
|
|
|
|
|
|
content_panels = Entity.content_panels + [
|
|
|
|
StreamFieldPanel('body'),
|
2018-07-26 21:48:30 +01:00
|
|
|
FieldPanel('tags')
|
|
|
|
]
|
2018-08-01 09:01:16 +01:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
ordering = ('post_date',)
|
|
|
|
|
|
|
|
def get_date_group(self):
|
|
|
|
if self.post_date:
|
|
|
|
return self.post_date.strftime("%Y-%m")
|
|
|
|
|
|
|
|
|
2018-08-03 20:28:10 +01:00
|
|
|
class BlogIndexPage(Entity):
|
2018-08-01 09:01:16 +01:00
|
|
|
body = build_stream_field()
|
|
|
|
|
|
|
|
subpage_types = [BlogPage]
|
|
|
|
|
|
|
|
content_panels = Entity.content_panels + [
|
|
|
|
StreamFieldPanel('body')
|
|
|
|
]
|
|
|
|
|
2018-08-03 13:20:33 +01:00
|
|
|
def get_context(self, request, *args, **kwargs):
|
2018-08-01 09:01:16 +01:00
|
|
|
context = super().get_context(request)
|
2018-08-03 13:20:33 +01:00
|
|
|
child_pages = self.get_children().specific().live().values_list('id', flat=True)
|
|
|
|
context['blogs'] = BlogPage.objects.filter(id__in=child_pages)
|
2018-08-03 20:28:10 +01:00
|
|
|
tag_filter = request.GET.get('tag')
|
|
|
|
if tag_filter:
|
|
|
|
context['tag'] = tag_filter
|
|
|
|
context['blogs'] = context['blogs'].filter(tags__name=tag_filter)
|
2018-08-01 09:01:16 +01:00
|
|
|
return context
|