must_of_the_week behaviour for update. public_mark has been returned

This commit is contained in:
a.gorbunov 2020-02-08 10:37:28 +00:00
parent 3da4dfd54b
commit c6e12f3320
2 changed files with 33 additions and 10 deletions

View File

@ -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):

View File

@ -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"""