Skip to content

Commit

Permalink
feat: CarouselItemLocalization images
Browse files Browse the repository at this point in the history
  • Loading branch information
francesco-filicetti committed Jul 4, 2024
1 parent 293ba97 commit 17a8623
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/cms/carousels/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
10 changes: 9 additions & 1 deletion src/cms/carousels/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -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']


Expand Down
Original file line number Diff line number Diff line change
@@ -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",
),
),
]
8 changes: 8 additions & 0 deletions src/cms/carousels/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
)
Expand Down
10 changes: 10 additions & 0 deletions src/cms/carousels/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__'
Expand Down

0 comments on commit 17a8623

Please sign in to comment.