From 53387886cd9d166056d385a807591616a621e9f7 Mon Sep 17 00:00:00 2001 From: Kuroshini Date: Tue, 17 Dec 2019 23:13:57 +0300 Subject: [PATCH] must of the week --- apps/main/models.py | 19 +++++++++++++++++++ .../migrations/0045_news_must_of_the_week.py | 18 ++++++++++++++++++ apps/news/models.py | 1 + apps/news/serializers.py | 12 +++++++++--- 4 files changed, 47 insertions(+), 3 deletions(-) create mode 100644 apps/news/migrations/0045_news_must_of_the_week.py diff --git a/apps/main/models.py b/apps/main/models.py index 2302dec3..06e2355b 100644 --- a/apps/main/models.py +++ b/apps/main/models.py @@ -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.""" diff --git a/apps/news/migrations/0045_news_must_of_the_week.py b/apps/news/migrations/0045_news_must_of_the_week.py new file mode 100644 index 00000000..57f5f351 --- /dev/null +++ b/apps/news/migrations/0045_news_must_of_the_week.py @@ -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), + ), + ] diff --git a/apps/news/models.py b/apps/news/models.py index ce785207..d39962c5 100644 --- a/apps/news/models.py +++ b/apps/news/models.py @@ -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: diff --git a/apps/news/serializers.py b/apps/news/serializers.py index ce91c9c5..78d048c5 100644 --- a/apps/news/serializers.py +++ b/apps/news/serializers.py @@ -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):