Skip to content

Commit

Permalink
feat: remove article gallery field (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
niccofyren authored Nov 19, 2024
1 parent eccb8ac commit 02b1e61
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 35 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ __pycache__
# Dev assets and state
db.sqlite3
media

# Frontend specific files
static
15 changes: 15 additions & 0 deletions aktuelt/migrations/0010_delete_newspagegalleryimage.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Generated by Django 4.2.16 on 2024-11-13 17:59

from django.db import migrations


class Migration(migrations.Migration):
dependencies = [
("aktuelt", "0009_remove_newspage_date_newspage_custom_published_at_and_more"),
]

operations = [
migrations.DeleteModel(
name="NewsPageGalleryImage",
),
]
28 changes: 3 additions & 25 deletions aktuelt/models.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from django.db import models
from modelcluster.contrib.taggit import ClusterTaggableManager
from modelcluster.fields import ParentalKey, ParentalManyToManyField
from modelcluster.fields import ParentalKey
from taggit.models import TaggedItemBase
from wagtail.admin.panels import FieldPanel, InlinePanel, MultiFieldPanel
from wagtail.api import APIField
from wagtail.fields import RichTextField
from wagtail.models import Orderable, Page, forms
from wagtail.models import Orderable, Page
from wagtail.search import index
from wagtail.snippets.models import register_snippet

Expand All @@ -14,7 +14,6 @@
ContributorsSerializer,
NewsBodySerializer,
NewsImageSerializer,
NewsPageGallerySerializer,
NewsPageTagsSerializer,
)

Expand Down Expand Up @@ -68,14 +67,6 @@ class NewsPage(Page):
related_name="+",
)

def get_main_image(self):
if self.main_image:
return self.main_image

gallery_item = self.gallery_images.first()

return gallery_item.image if gallery_item else None

search_fields = Page.search_fields + [
index.SearchField("intro"),
index.SearchField("body"),
Expand Down Expand Up @@ -104,8 +95,7 @@ def schedule(self):
# TODO: Replace with prettier (main model based?) serializer pattern?
APIField("contributors", serializer=ContributorsSerializer(source="news_page_contributors")),
APIField("tags", serializer=NewsPageTagsSerializer()),
APIField("gallery_images", serializer=NewsPageGallerySerializer()),
APIField("main_image", serializer=NewsImageSerializer(source="get_main_image")),
APIField("main_image", serializer=NewsImageSerializer()),
]

content_panels = Page.content_panels + [
Expand All @@ -121,7 +111,6 @@ def schedule(self):
],
heading="News information",
),
InlinePanel("gallery_images", label="Gallery images"),
]


Expand All @@ -136,17 +125,6 @@ class NewsPageContributor(Orderable):
]


class NewsPageGalleryImage(Orderable):
page = ParentalKey(NewsPage, 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"),
]


@register_snippet
class Contributor(models.Model):
name = models.CharField(max_length=255)
Expand Down
10 changes: 0 additions & 10 deletions aktuelt/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,16 +48,6 @@ def to_representation(self, news_page_contributors):
]


class NewsPageGallerySerializer(Field):
def to_representation(self, value):
return [
# 100% guarantee there are better ways to do this
# TODO: Replace with generic and reusable serializer
NewsImageSerializer().to_representation(gallery_image.image)
for gallery_image in value.all()
]


class NewsPageTagsSerializer(Field):
def to_representation(self, value):
return [
Expand Down

0 comments on commit 02b1e61

Please sign in to comment.