must of the week
This commit is contained in:
parent
23299ad4a1
commit
53387886cd
|
|
@ -210,6 +210,25 @@ class CarouselQuerySet(models.QuerySet):
|
|||
"""Filter collection by country code."""
|
||||
return self.filter(country__code=code)
|
||||
|
||||
def create_or_destroy(self, instance_to_bind, country):
|
||||
"""Creates or destroys Carousel instance depending on instance fields"""
|
||||
toggle = True
|
||||
kwargs = {
|
||||
'content_type': ContentType.objects.get_for_model(instance_to_bind),
|
||||
'object_id': instance_to_bind.pk,
|
||||
'country': country,
|
||||
}
|
||||
if toggle is None:
|
||||
return
|
||||
elif toggle:
|
||||
kwargs.update({
|
||||
'is_parse': True,
|
||||
'active': True,
|
||||
})
|
||||
self.create(**kwargs)
|
||||
else:
|
||||
self.filter(**kwargs).delete()
|
||||
|
||||
|
||||
class Carousel(models.Model):
|
||||
"""Carousel model."""
|
||||
|
|
|
|||
18
apps/news/migrations/0045_news_must_of_the_week.py
Normal file
18
apps/news/migrations/0045_news_must_of_the_week.py
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
# Generated by Django 2.2.7 on 2019-12-17 17:59
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('news', '0044_auto_20191216_2044'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='news',
|
||||
name='must_of_the_week',
|
||||
field=models.BooleanField(default=False),
|
||||
),
|
||||
]
|
||||
|
|
@ -223,6 +223,7 @@ class News(GalleryModelMixin, BaseAttributes, TranslatedFieldsMixin, HasTagsMixi
|
|||
verbose_name=_('Duplication datetime'))
|
||||
duplication_uuid = models.UUIDField(default=uuid.uuid4, editable=True, unique=False,
|
||||
verbose_name=_('Field to detect doubles'))
|
||||
must_of_the_week = models.BooleanField(default=False, verbose_name=_('Show in the carousel'))
|
||||
objects = NewsQuerySet.as_manager()
|
||||
|
||||
class Meta:
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ from rest_framework.fields import SerializerMethodField
|
|||
|
||||
from account.serializers.common import UserBaseSerializer
|
||||
from gallery.models import Image
|
||||
from main.models import SiteSettings
|
||||
from main.models import SiteSettings, Carousel
|
||||
from location import models as location_models
|
||||
from location.serializers import CountrySimpleSerializer, AddressBaseSerializer
|
||||
from news import models
|
||||
|
|
@ -185,6 +185,7 @@ class NewsBackOfficeBaseSerializer(NewsBaseSerializer):
|
|||
'locale_to_description_is_active',
|
||||
'is_published',
|
||||
'duplication_date',
|
||||
'must_of_the_week',
|
||||
)
|
||||
extra_kwargs = {
|
||||
'backoffice_title': {'allow_null': False},
|
||||
|
|
@ -199,7 +200,9 @@ class NewsBackOfficeBaseSerializer(NewsBaseSerializer):
|
|||
slugs__values__contains=list(slugs.values())
|
||||
).exists():
|
||||
raise serializers.ValidationError({'slugs': _('News with this slug already exists.')})
|
||||
return super().create(validated_data)
|
||||
instance = super().create(validated_data)
|
||||
Carousel.objects.create_or_destroy(instance, instance.address.city.country)
|
||||
return instance
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
slugs = validated_data.get('slugs')
|
||||
|
|
@ -208,7 +211,10 @@ class NewsBackOfficeBaseSerializer(NewsBaseSerializer):
|
|||
slugs__values__contains=list(slugs.values())
|
||||
).exclude(pk=instance.pk).exists():
|
||||
raise serializers.ValidationError({'slugs': _('News with this slug already exists.')})
|
||||
return super().update(instance, validated_data)
|
||||
ret = super().update(instance, validated_data)
|
||||
if ret.must_of_the_week != instance.must_of_the_week:
|
||||
Carousel.objects.create_or_destroy(instance, instance.address.city.country)
|
||||
return ret
|
||||
|
||||
|
||||
class NewsBackOfficeDuplicationInfoSerializer(serializers.ModelSerializer):
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user