From 1a64d2a5132d1603802afdf3c8e118dfc7f3e9e2 Mon Sep 17 00:00:00 2001 From: Jake Howard Date: Wed, 1 Aug 2018 09:01:16 +0100 Subject: [PATCH] Add dedicated blog index page to group articles by date --- project/blog/migrations/0003_blogindexpage.py | 38 ++++++++++++++++ .../migrations/0004_auto_20180730_1721.py | 21 +++++++++ project/blog/models.py | 22 ++++++++++ project/common/models.py | 1 + .../migrations/0005_auto_20180730_1721.py | 17 ++++++++ .../migrations/0003_auto_20180730_1721.py | 21 +++++++++ templates/blog/blog_index_page.html | 43 +++++++++++++++++++ 7 files changed, 163 insertions(+) create mode 100644 project/blog/migrations/0003_blogindexpage.py create mode 100644 project/blog/migrations/0004_auto_20180730_1721.py create mode 100644 project/home/migrations/0005_auto_20180730_1721.py create mode 100644 project/pages/migrations/0003_auto_20180730_1721.py create mode 100644 templates/blog/blog_index_page.html diff --git a/project/blog/migrations/0003_blogindexpage.py b/project/blog/migrations/0003_blogindexpage.py new file mode 100644 index 0000000..bf5cd35 --- /dev/null +++ b/project/blog/migrations/0003_blogindexpage.py @@ -0,0 +1,38 @@ +# Generated by Django 2.0.7 on 2018-07-30 08:03 + +from django.db import migrations, models +import django.db.models.deletion +import wagtail.core.blocks +import wagtail.core.fields +import wagtail.documents.blocks +import wagtail.embeds.blocks +import wagtail.images.blocks +import wagtailmarkdown.blocks +import wagtailmetadata.models + + +class Migration(migrations.Migration): + + dependencies = [ + ('wagtailimages', '0020_add-verbose-name'), + ('wagtailcore', '0040_page_draft_title'), + ('blog', '0002_auto_20180726_2038'), + ] + + operations = [ + migrations.CreateModel( + name='BlogIndexPage', + fields=[ + ('page_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='wagtailcore.Page')), + ('created', models.DateTimeField(auto_now_add=True)), + ('modified', models.DateTimeField(auto_now=True)), + ('post_date', models.DateTimeField(blank=True, null=True)), + ('body', wagtail.core.fields.StreamField([('document', wagtail.documents.blocks.DocumentChooserBlock()), ('heading', wagtail.core.blocks.StructBlock([('size', wagtail.core.blocks.ChoiceBlock(choices=[('h1', 'H1'), ('h2', 'H2'), ('h3', 'H3'), ('h4', 'H4'), ('h5', 'H5')])), ('value', wagtail.core.blocks.CharBlock())])), ('image', wagtail.images.blocks.ImageChooserBlock()), ('markdown', wagtailmarkdown.blocks.MarkdownBlock()), ('ol', wagtail.core.blocks.ListBlock(wagtail.core.blocks.CharBlock(label='List Item'), icon='list-ol', label='Ordered List', template='blocks/ordered-list.html')), ('paragraph', wagtail.core.blocks.RichTextBlock()), ('raw_html', wagtail.core.blocks.RawHTMLBlock(label='Raw HTML')), ('ul', wagtail.core.blocks.ListBlock(wagtail.core.blocks.CharBlock(label='List Item'), icon='list-ul', label='Unordered List')), ('video', wagtail.core.blocks.StructBlock([('video', wagtail.embeds.blocks.EmbedBlock()), ('caption', wagtail.core.blocks.CharBlock())]))])), + ('search_image', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='+', to='wagtailimages.Image')), + ], + options={ + 'abstract': False, + }, + bases=(wagtailmetadata.models.MetadataMixin, 'wagtailcore.page', models.Model), + ), + ] diff --git a/project/blog/migrations/0004_auto_20180730_1721.py b/project/blog/migrations/0004_auto_20180730_1721.py new file mode 100644 index 0000000..872092a --- /dev/null +++ b/project/blog/migrations/0004_auto_20180730_1721.py @@ -0,0 +1,21 @@ +# Generated by Django 2.0.7 on 2018-07-30 17:21 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('blog', '0003_blogindexpage'), + ] + + operations = [ + migrations.AlterModelOptions( + name='blogindexpage', + options={'ordering': ('title',)}, + ), + migrations.AlterModelOptions( + name='blogpage', + options={'ordering': ('post_date',)}, + ), + ] diff --git a/project/blog/models.py b/project/blog/models.py index 8f48de5..1e8ec24 100644 --- a/project/blog/models.py +++ b/project/blog/models.py @@ -30,3 +30,25 @@ class BlogPage(Entity): StreamFieldPanel('body'), FieldPanel('tags') ] + + class Meta: + ordering = ('post_date',) + + def get_date_group(self): + if self.post_date: + return self.post_date.strftime("%Y-%m") + + +class BlogIndexPage(Entity): + body = build_stream_field() + + subpage_types = [BlogPage] + + content_panels = Entity.content_panels + [ + StreamFieldPanel('body') + ] + + def get_context(self, request): + context = super().get_context(request) + context['blogs'] = self.get_children().specific().live() + return context diff --git a/project/common/models.py b/project/common/models.py index 64c7d85..213a504 100644 --- a/project/common/models.py +++ b/project/common/models.py @@ -40,3 +40,4 @@ class Entity(MetadataPageMixin, Page): class Meta: abstract = True + ordering = ('title',) diff --git a/project/home/migrations/0005_auto_20180730_1721.py b/project/home/migrations/0005_auto_20180730_1721.py new file mode 100644 index 0000000..da26967 --- /dev/null +++ b/project/home/migrations/0005_auto_20180730_1721.py @@ -0,0 +1,17 @@ +# Generated by Django 2.0.7 on 2018-07-30 17:21 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('home', '0004_auto_20180725_0741'), + ] + + operations = [ + migrations.AlterModelOptions( + name='homepage', + options={'ordering': ('title',)}, + ), + ] diff --git a/project/pages/migrations/0003_auto_20180730_1721.py b/project/pages/migrations/0003_auto_20180730_1721.py new file mode 100644 index 0000000..2cb89f6 --- /dev/null +++ b/project/pages/migrations/0003_auto_20180730_1721.py @@ -0,0 +1,21 @@ +# Generated by Django 2.0.7 on 2018-07-30 17:21 + +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('pages', '0002_auto_20180726_0732'), + ] + + operations = [ + migrations.AlterModelOptions( + name='sectionindexpage', + options={'ordering': ('title',)}, + ), + migrations.AlterModelOptions( + name='simplecontentpage', + options={'ordering': ('title',)}, + ), + ] diff --git a/templates/blog/blog_index_page.html b/templates/blog/blog_index_page.html new file mode 100644 index 0000000..1c3f542 --- /dev/null +++ b/templates/blog/blog_index_page.html @@ -0,0 +1,43 @@ +{% extends "base.html" %} + +{% load static wagtailimages_tags %} + +{% block content %} +
+
+ {% include "common/content.html" %} + + {% if page.body %} +
+ {% endif %} + + {% for child in blogs %} + {% ifchanged %} +

+ + + +

+ {% endifchanged %} + +
+
+ {% if child.image %} + + {% image child.image width-300 as photo %} +
+
+ {% endif %} +
+
+ +
{{ child.title }}
+
+ {% include "common/content-details.html" with page=child %} +

{{ child.get_short_body }}

+
+
+ {% endfor %} +
+
+{% endblock %}