Skip to content

Commit

Permalink
Squash migrations and add wagtail_headless_preview
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaudcolas committed Sep 2, 2024
1 parent 654ca8e commit 2051409
Show file tree
Hide file tree
Showing 22 changed files with 648 additions and 675 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
# low-carbon-template
# Low-carbon Wagtail template

A Wagtail website template optimized for energy efficiency and low carbon emissions. Initially built as a headless website with Django REST Framework and Next.js, with deployment as a static site.
3 changes: 0 additions & 3 deletions blog/admin.py

This file was deleted.

132 changes: 37 additions & 95 deletions blog/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
# Generated by Django 4.2.13 on 2024-08-21 12:54
# Generated by Django 5.1 on 2024-09-01 16:48

from django.db import migrations, models
import django.db.models.deletion
import modelcluster.contrib.taggit
import modelcluster.fields
import wagtail.blocks
import wagtail.fields
import wagtail.images.blocks
import wagtail_headless_preview.models
from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
("wagtailimages", "0026_delete_uploadedimage"),
("wagtailcore", "0093_uploadedfile"),
(
"taggit",
"0006_rename_taggeditem_content_type_object_id_taggit_tagg_content_8fc721_idx",
),
("wagtailcore", "0094_alter_page_locale"),
]

operations = [
Expand Down Expand Up @@ -55,47 +48,10 @@ class Migration(migrations.Migration):
options={
"abstract": False,
},
bases=("wagtailcore.page",),
bases=(wagtail_headless_preview.models.HeadlessMixin, "wagtailcore.page"),
),
migrations.CreateModel(
name="BlogPage",
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",
),
),
("publication_date", models.DateField(verbose_name="Post date")),
("introduction", wagtail.fields.RichTextField()),
(
"body",
wagtail.fields.StreamField(
[
("image", wagtail.images.blocks.ImageChooserBlock()),
("paragraph", wagtail.blocks.RichTextBlock()),
]
),
),
(
"authors",
modelcluster.fields.ParentalManyToManyField(
blank=True, to="blog.author"
),
),
],
options={
"abstract": False,
},
bases=("wagtailcore.page",),
),
migrations.CreateModel(
name="BlogPageTag",
name="Category",
fields=[
(
"id",
Expand All @@ -106,75 +62,61 @@ class Migration(migrations.Migration):
verbose_name="ID",
),
),
(
"content_object",
modelcluster.fields.ParentalKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="tagged_items",
to="blog.blogpage",
),
),
(
"tag",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="%(app_label)s_%(class)s_items",
to="taggit.tag",
),
),
("name", models.CharField(max_length=255)),
],
options={
"abstract": False,
"verbose_name_plural": "categories",
},
),
migrations.CreateModel(
name="BlogPageGalleryImage",
name="BlogPage",
fields=[
(
"id",
models.BigAutoField(
"page_ptr",
models.OneToOneField(
auto_created=True,
on_delete=django.db.models.deletion.CASCADE,
parent_link=True,
primary_key=True,
serialize=False,
verbose_name="ID",
to="wagtailcore.page",
),
),
("publication_date", models.DateField(verbose_name="Post date")),
("introduction", wagtail.fields.RichTextField()),
(
"sort_order",
models.IntegerField(blank=True, editable=False, null=True),
"body",
wagtail.fields.StreamField(
[("image", 0), ("paragraph", 1)],
block_lookup={
0: ("wagtail.images.blocks.ImageChooserBlock", (), {}),
1: ("wagtail.blocks.RichTextBlock", (), {}),
},
),
),
("caption", models.CharField(blank=True, max_length=250)),
(
"image",
"author",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="+",
to="wagtailimages.image",
to="blog.author",
),
),
(
"page",
modelcluster.fields.ParentalKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="gallery_images",
to="blog.blogpage",
"category",
models.ForeignKey(
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="+",
to="blog.category",
),
),
],
options={
"ordering": ["sort_order"],
"abstract": False,
},
),
migrations.AddField(
model_name="blogpage",
name="tags",
field=modelcluster.contrib.taggit.ClusterTaggableManager(
blank=True,
help_text="A comma-separated list of tags.",
through="blog.BlogPageTag",
to="taggit.Tag",
verbose_name="Tags",
),
bases=(wagtail_headless_preview.models.HeadlessMixin, "wagtailcore.page"),
),
]
60 changes: 0 additions & 60 deletions blog/migrations/0002_category_remove_blogpage_tags_and_more.py

This file was deleted.

85 changes: 29 additions & 56 deletions blog/models.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
from django import forms
from django.db import models

from modelcluster.fields import ParentalKey, ParentalManyToManyField
from wagtail.models import Page, Orderable
from rest_framework import serializers
from wagtail import blocks
from wagtail.admin.panels import FieldPanel, MultiFieldPanel
from wagtail.api import APIField
from wagtail.fields import RichTextField, StreamField
from wagtail.admin.panels import FieldPanel, InlinePanel, MultiFieldPanel
from wagtail.images.blocks import ImageChooserBlock
from wagtail.models import Page
from wagtail.search import index
from wagtail import blocks
from modelcluster.contrib.taggit import ClusterTaggableManager
from taggit.models import TaggedItemBase
from wagtail.snippets.models import register_snippet
from wagtail.images.blocks import ImageChooserBlock

from wagtail.api import APIField
from rest_framework import serializers
from wagtail_headless_preview.models import HeadlessMixin


@register_snippet
Expand Down Expand Up @@ -43,60 +38,56 @@ class Meta:
verbose_name_plural = "categories"


class AuthorSerializer(serializers.ModelSerializer):
class Meta:
model = Author
fields = ["name"]


class CategorySerializer(serializers.ModelSerializer):
class Meta:
model = Category
fields = ["name"]


class BlogIndexPage(Page):
class BlogIndexPage(HeadlessMixin, Page):
parent_page_types = ["home.HomePage"]
subpage_types = ["blog.BlogPage"]
max_count = 1


class BlogPageTag(TaggedItemBase): # do it with snnipet
content_object = ParentalKey(
"BlogPage", related_name="tagged_items", on_delete=models.CASCADE
)

class BlogPage(HeadlessMixin, Page):
parent_page_types = ["blog.BlogIndexPage"]

class BlogPage(Page):
publication_date = models.DateField("Post date")
introduction = RichTextField()
authors = models.ForeignKey(
author = models.ForeignKey(
"blog.Author",
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name="+",
)

body = StreamField(
[
("image", ImageChooserBlock()),
("paragraph", blocks.RichTextBlock()), # add embedblock and blockquote
]
)

category = models.ForeignKey(
"blog.Category",
null=True,
on_delete=models.SET_NULL,
related_name="+",
)

def main_image(self):
gallery_item = self.gallery_images.first()
if gallery_item:
return gallery_item.image
else:
return None
body = StreamField(
[
("image", ImageChooserBlock()),
("paragraph", blocks.RichTextBlock()),
]
)

api_fields = [
APIField("publication_date"),
APIField("introduction"),
APIField("authors"),
APIField("body"),
APIField("author", serializer=AuthorSerializer()),
APIField("category", serializer=CategorySerializer()),
APIField("body"),
]

search_fields = Page.search_fields + [
Expand All @@ -108,29 +99,11 @@ def main_image(self):
MultiFieldPanel(
[
FieldPanel("publication_date"),
FieldPanel("authors", widget=forms.CheckboxSelectMultiple),
FieldPanel("author"),
FieldPanel("category"),
],
heading="Blog information",
),
FieldPanel("introduction"),
FieldPanel("body"),
FieldPanel("category"),
InlinePanel("gallery_images", label="Gallery images"),
]

parent_page_types = ["blog.BlogIndexPage"]


class BlogPageGalleryImage(Orderable):
page = ParentalKey(
BlogPage, on_delete=models.CASCADE, related_name="gallery_images"
)
image = models.ForeignKey(
"wagtailimages.Image", on_delete=models.CASCADE, related_name="+"
)
caption = models.CharField(blank=True, max_length=250)

panels = [
FieldPanel("image"),
FieldPanel("caption"),
]
Loading

0 comments on commit 2051409

Please sign in to comment.