Merge pull request #2 from wagtail/blog-page-tag-redirects

View blog posts by tag
pull/20/head
David Ray 2017-02-10 07:11:32 -05:00 zatwierdzone przez GitHub
commit 9a2755637a
5 zmienionych plików z 102 dodań i 43 usunięć

Wyświetl plik

@ -1,31 +1,34 @@
from __future__ import unicode_literals
from django.contrib import messages
from django.db import models
from django.shortcuts import redirect, render
from modelcluster.fields import ParentalKey
from modelcluster.contrib.taggit import ClusterTaggableManager
from taggit.models import TaggedItemBase
from wagtail.wagtailcore.models import Page, Orderable, Collection
from wagtail.wagtailsearch import index
from wagtail.wagtailimages.edit_handlers import ImageChooserPanel
from wagtail.wagtailcore.fields import StreamField, RichTextField
from taggit.models import Tag, TaggedItemBase
from wagtail.contrib.wagtailroutablepage.models import RoutablePageMixin, route
from wagtail.wagtailadmin.edit_handlers import (
FieldPanel,
InlinePanel,
FieldRowPanel,
StreamFieldPanel,
MultiFieldPanel
)
FieldPanel,
InlinePanel,
StreamFieldPanel,
)
from wagtail.wagtailimages.edit_handlers import ImageChooserPanel
from wagtail.wagtailcore.fields import StreamField
from wagtail.wagtailcore.models import Page, Orderable
from wagtail.wagtailsnippets.edit_handlers import SnippetChooserPanel
from bakerydemo.base.blocks import BaseStreamBlock
class BlogPeopleRelationship(Orderable, models.Model):
"""
This defines the relationship between the `LocationPage` within the `locations`
app and the About page below allowing us to add locations to the about
section.
"""
'''
This defines the relationship between the `People` within the `base`
app and the BlogPage below allowing us to add people to a BlogPage.
'''
page = ParentalKey(
'BlogPage', related_name='blog_person_relationship'
)
@ -42,9 +45,9 @@ class BlogPageTag(TaggedItemBase):
class BlogPage(Page):
"""
The About Page
"""
'''
A Blog Page (Post)
'''
image = models.ForeignKey(
'wagtailimages.Image',
null=True,
@ -59,8 +62,8 @@ class BlogPage(Page):
date_published = models.DateField("Date article published", blank=True, null=True)
body = StreamField(
BaseStreamBlock(), verbose_name="About page detail", blank=True
)
BaseStreamBlock(), verbose_name="Blog post", blank=True
)
content_panels = Page.content_panels + [
ImageChooserPanel('image'),
@ -73,19 +76,31 @@ class BlogPage(Page):
]
def authors(self):
'''
Returns the BlogPage's related People
'''
authors = [
n.people for n in self.blog_person_relationship.all()
n.people for n in self.blog_person_relationship.all()
]
return authors
# def tags(self):
# tags = self.tags.all()
# return tags
@property
def get_tags(self):
'''
Returns the BlogPage's related list of Tags.
Each Tag is modified to include a url attribute
'''
tags = self.tags.all()
for tag in tags:
tag.url = '/'+'/'.join(s.strip('/') for s in [
self.get_parent().url,
'tags',
tag.slug
])
return tags
parent_page_types = [
'BlogIndexPage'
]
parent_page_types = ['BlogIndexPage']
# Defining what content type can sit under the parent
# The empty array means that no children can be placed under the
@ -95,10 +110,14 @@ class BlogPage(Page):
# api_fields = ['image', 'body']
class BlogIndexPage(Page):
"""
"""
class BlogIndexPage(RoutablePageMixin, Page):
'''
Index page for blogs.
We need to alter the page model's context to return the child page objects - the
BlogPage - so that it works as an index page
The RoutablePageMixin is used to allow for a custom sub-URL
'''
image = models.ForeignKey(
'wagtailimages.Image',
null=True,
@ -123,14 +142,37 @@ class BlogIndexPage(Page):
# Defining what content type can sit under the parent. Since it's a blank
# array no subpage can be added
subpage_types = [
'BlogPage'
]
subpage_types = ['BlogPage']
def get_context(self, request):
context = super(BlogIndexPage, self).get_context(request)
context['posts'] = BlogPage.objects.descendant_of(
context['blogs'] = BlogPage.objects.descendant_of(
self).live().order_by(
'-first_published_at')
return context
@route('^tags/$', name='tag_archive')
@route('^tags/(\w+)/$', name='tag_archive')
def tag_archive(self, request, tag=None):
'''
A Custom view that utilizes Tags. This view will
return all related BlogPages for a given Tag or redirect back to
the BlogIndexPage
'''
try:
tag = Tag.objects.get(slug=tag)
except Tag.DoesNotExist:
if tag:
msg = 'There are no blog posts tagged with "{}"'.format(tag)
messages.add_message(request, messages.INFO, msg)
return redirect(self.url)
blogs = BlogPage.objects.filter(tags=tag).live().descendant_of(self)
context = {
'title': 'Posts tagged with: {}'.format(tag.name),
'blogs': blogs
}
return render(request, 'blog/blog_index_page.html', context)
# api_fields = ['introduction']

Wyświetl plik

@ -97,7 +97,7 @@ class BreadPage(Page):
class BreadsIndexPage(Page):
'''
Index page for breads. We don't have any fields within our model but we need
to alter the page models context to return the child page objects - the
to alter the page model's context to return the child page objects - the
BreadPage - so that it works as an index page
'''

Wyświetl plik

@ -112,6 +112,16 @@
{# breadcrumbs is defined in base/templatetags/navigation_tags.py #}
{% endblock %}
{% if messages %}
<div>
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}
</ul>
</div>
{% endif %}
{% block content %}
{% endblock %}

Wyświetl plik

@ -4,7 +4,14 @@
{% block content %}
{{ page.title }}
{% for post in posts %}
<div><a href="{{ post.slug }}">{{ post.title }}</a></div>
{% for blog in blogs %}
<div>
<h2><a href="{{ blog.full_url }}">{{ blog.title }}</a></h2>
{{ blog.body|truncatewords_html:10 }}
{% for tag in blog.get_tags %}
<a href="{{ tag.url }}">{{ tag }}</a>
{% endfor %}
</div>
{% endfor %}
{% endblock content %}
{% endblock content %}

Wyświetl plik

@ -7,15 +7,15 @@
{% image self.image fill-600x600 %}
</figure>
{% for tag in page.tags.all %}
{{ tag }}
{% for tag in page.get_tags %}
<a href="{{ tag.url }}">{{ tag }}</a>
{% endfor %}
<date>{{ page.date_published }}</date>
{% for author in page.authors %}
<li>{{ author }}</li>
{% endfor %}
{{ page.body }}
{% endblock content %}
{% endblock content %}