diff --git a/apps/establishment/serializers/back.py b/apps/establishment/serializers/back.py index 2cca450c..bf858c0d 100644 --- a/apps/establishment/serializers/back.py +++ b/apps/establishment/serializers/back.py @@ -232,6 +232,8 @@ class EstablishmentRUDSerializer(model_serializers.EstablishmentBaseSerializer): last_review = ReviewBaseSerializer(read_only=True) + must_of_the_week = serializers.BooleanField(required=False, ) + class Meta(model_serializers.EstablishmentBaseSerializer.Meta): fields = [ 'id', @@ -264,6 +266,7 @@ class EstablishmentRUDSerializer(model_serializers.EstablishmentBaseSerializer): 'must_of_the_week', 'last_update_by_gm', 'last_update_by_manager', + 'public_mark', ] def to_representation(self, instance): diff --git a/apps/utils/models.py b/apps/utils/models.py index adbfbbeb..de583308 100644 --- a/apps/utils/models.py +++ b/apps/utils/models.py @@ -542,22 +542,42 @@ class AwardsModelMixin: class CarouselMixin: + def __must_of_the_week_info_from_self(self) -> dict: + from main.models import Country + + return { + 'content_type': ContentType.objects.get_for_model(self), + 'object_id': getattr(self, 'pk'), + 'country': getattr(self, 'country', get_object_or_404(Country, id=getattr(self, 'country_id', 0))), + } + + def __must_of_the_week_has_fields(self) -> bool: + return hasattr(self, 'pk') and (hasattr(self, 'country') or hasattr(self, 'country_id')) + @property def must_of_the_week(self) -> bool: + from main import models as main_models + """Detects whether current item in carousel""" - from main.models import Carousel - - if hasattr(self, 'pk') and (hasattr(self, 'country') or hasattr(self, 'country_id')): - kwargs = { - 'content_type': ContentType.objects.get_for_model(self), - 'object_id': self.pk, - 'country': getattr(self, 'country', getattr(self, 'country_id', None)), - } - - return Carousel.objects.filter(**kwargs).exists() + if self.__must_of_the_week_has_fields(): + kwargs = self.__must_of_the_week_info_from_self() + return main_models.Carousel.objects.filter(**kwargs).exists() return False + @must_of_the_week.setter + def must_of_the_week(self, status: bool): + from main import models as main_models + + """Update field by status""" + if status != self.must_of_the_week and self.__must_of_the_week_has_fields(): + carousel, _ = main_models.Carousel.objects.get_or_create( + **self.__must_of_the_week_info_from_self() + ) + + if not status and isinstance(carousel, main_models.Carousel): + carousel.delete() + class UpdateByMixin(models.Model): """Modify by mixin"""