From 17a86234b6d68140973edeeda084f54bdb05dc45 Mon Sep 17 00:00:00 2001 From: Francesco Filicetti Date: Thu, 4 Jul 2024 09:09:58 +0200 Subject: [PATCH] feat: CarouselItemLocalization images --- src/cms/carousels/admin.py | 1 + src/cms/carousels/forms.py | 10 ++++- ...carouselitemlocalization_image_and_more.py | 39 +++++++++++++++++++ src/cms/carousels/models.py | 8 ++++ src/cms/carousels/serializers.py | 10 +++++ 5 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 src/cms/carousels/migrations/0015_carouselitemlocalization_image_and_more.py diff --git a/src/cms/carousels/admin.py b/src/cms/carousels/admin.py index a3470143..e26dd212 100644 --- a/src/cms/carousels/admin.py +++ b/src/cms/carousels/admin.py @@ -26,6 +26,7 @@ class CarouselItemLocalizationInline(nested_admin.NestedStackedInline): extra = 0 sortable_field_name = "order" classes = ['collapse'] + raw_id_fields = ('image', 'mobile_image') class CarouselItemInline(nested_admin.NestedStackedInline): diff --git a/src/cms/carousels/forms.py b/src/cms/carousels/forms.py index 0d229605..2ad51faa 100644 --- a/src/cms/carousels/forms.py +++ b/src/cms/carousels/forms.py @@ -45,10 +45,18 @@ def __init__(self, *args, **kwargs): if carousel_id and carousel_item_id: self.fields['carousel_item'].queryset = CarouselItem.objects.filter(pk=carousel_item_id, carousel__pk=carousel_id) + setattr(self.fields['image'], + FORM_SOURCE_LABEL, + # only images + reverse('unicms_api:media-options') + '?file_type=image%2Fwebp') + setattr(self.fields['mobile_image'], + FORM_SOURCE_LABEL, + # only images + reverse('unicms_api:media-options') + '?file_type=image%2Fwebp') class Meta: model = CarouselItemLocalization - fields = ['carousel_item', 'language', 'pre_heading', + fields = ['carousel_item', 'image', 'mobile_image', 'language', 'pre_heading', 'heading', 'description', 'order', 'is_active'] diff --git a/src/cms/carousels/migrations/0015_carouselitemlocalization_image_and_more.py b/src/cms/carousels/migrations/0015_carouselitemlocalization_image_and_more.py new file mode 100644 index 00000000..4a0c3c85 --- /dev/null +++ b/src/cms/carousels/migrations/0015_carouselitemlocalization_image_and_more.py @@ -0,0 +1,39 @@ +# Generated by Django 4.2.9 on 2024-07-04 06:23 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + dependencies = [ + ("cmsmedias", "0013_alter_media_created_by_alter_media_modified_by_and_more"), + ( + "cmscarousels", + "0014_alter_carousel_created_by_alter_carousel_modified_by_and_more", + ), + ] + + operations = [ + migrations.AddField( + model_name="carouselitemlocalization", + name="image", + field=models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.PROTECT, + related_name="localization_image", + to="cmsmedias.media", + ), + ), + migrations.AddField( + model_name="carouselitemlocalization", + name="mobile_image", + field=models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.PROTECT, + related_name="localization_mobile_image", + to="cmsmedias.media", + ), + ), + ] diff --git a/src/cms/carousels/models.py b/src/cms/carousels/models.py index c6a7664e..a2e4151c 100644 --- a/src/cms/carousels/models.py +++ b/src/cms/carousels/models.py @@ -102,6 +102,14 @@ class CarouselItemLocalization(ActivableModel, carousel_item = models.ForeignKey(CarouselItem, on_delete=models.CASCADE) language = models.CharField(choices=_lang_choices, max_length=12, default='en') + image = models.ForeignKey(Media, + on_delete=models.PROTECT, + blank=True, null=True, + related_name="localization_image") + mobile_image = models.ForeignKey(Media, + on_delete=models.PROTECT, + blank=True, null=True, + related_name="localization_mobile_image") pre_heading = models.CharField( max_length=120, blank=True, default='', help_text=_('Pre Heading') ) diff --git a/src/cms/carousels/serializers.py b/src/cms/carousels/serializers.py index fe89d8a1..493c9bfd 100644 --- a/src/cms/carousels/serializers.py +++ b/src/cms/carousels/serializers.py @@ -80,6 +80,16 @@ class CarouselItemLocalizationSerializer(UniCMSCreateUpdateSerializer, UniCMSContentTypeClass): carousel_item = CarouselItemForeignKey() + def to_representation(self, instance): + data = super().to_representation(instance) + if instance.image: + image = MediaSerializer(instance.image) + data['image'] = image.data + if instance.mobile_image: + mobile_image = MediaSerializer(instance.mobile_image) + data['mobile_image'] = mobile_image.data + return data + class Meta: model = CarouselItemLocalization fields = '__all__'