poroduct in_fav
This commit is contained in:
parent
a06a7cef7f
commit
a1d5feb8dc
|
|
@ -278,6 +278,14 @@ class User(AbstractUser):
|
||||||
model='news',
|
model='news',
|
||||||
).values_list('object_id', flat=True)
|
).values_list('object_id', flat=True)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def favorite_product_ids(self):
|
||||||
|
"""Return news IDs that in favorites for current user."""
|
||||||
|
return self.favorites.by_content_type(
|
||||||
|
app_label='product',
|
||||||
|
model='product',
|
||||||
|
).values_list('object_id', flat=True)
|
||||||
|
|
||||||
|
|
||||||
class UserRole(ProjectBaseMixin):
|
class UserRole(ProjectBaseMixin):
|
||||||
"""UserRole model."""
|
"""UserRole model."""
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ from django.contrib.contenttypes import fields as generic
|
||||||
from django.contrib.gis.db import models as gis_models
|
from django.contrib.gis.db import models as gis_models
|
||||||
from django.core.exceptions import ValidationError
|
from django.core.exceptions import ValidationError
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
from django.db.models import Case, When
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
from django.core.validators import MaxValueValidator, MinValueValidator
|
from django.core.validators import MaxValueValidator, MinValueValidator
|
||||||
|
|
||||||
|
|
@ -110,6 +111,19 @@ class ProductQuerySet(models.QuerySet):
|
||||||
"""Filter products by published state."""
|
"""Filter products by published state."""
|
||||||
return self.filter(state=self.model.PUBLISHED)
|
return self.filter(state=self.model.PUBLISHED)
|
||||||
|
|
||||||
|
def annotate_in_favorites(self, user):
|
||||||
|
"""Annotate flag in_favorites"""
|
||||||
|
favorite_product_ids = []
|
||||||
|
if user.is_authenticated:
|
||||||
|
favorite_product_ids = user.favorite_product_ids
|
||||||
|
return self.annotate(
|
||||||
|
in_favorites=Case(
|
||||||
|
When(id__in=favorite_product_ids, then=True),
|
||||||
|
default=False,
|
||||||
|
output_field=models.BooleanField(default=False)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class Product(TranslatedFieldsMixin, BaseAttributes):
|
class Product(TranslatedFieldsMixin, BaseAttributes):
|
||||||
"""Product models."""
|
"""Product models."""
|
||||||
|
|
|
||||||
|
|
@ -95,6 +95,7 @@ class ProductBaseSerializer(serializers.ModelSerializer):
|
||||||
preview_image_url = serializers.URLField(source='preview_main_image_url',
|
preview_image_url = serializers.URLField(source='preview_main_image_url',
|
||||||
allow_null=True,
|
allow_null=True,
|
||||||
read_only=True)
|
read_only=True)
|
||||||
|
in_favorites = serializers.BooleanField(allow_null=True)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
"""Meta class."""
|
"""Meta class."""
|
||||||
|
|
@ -112,6 +113,7 @@ class ProductBaseSerializer(serializers.ModelSerializer):
|
||||||
'preview_image_url',
|
'preview_image_url',
|
||||||
'wine_region',
|
'wine_region',
|
||||||
'wine_colors',
|
'wine_colors',
|
||||||
|
'in_favorites',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,11 @@ class ProductBackOfficeMixinView(ProductBaseView):
|
||||||
|
|
||||||
permission_classes = (permissions.IsAuthenticated,)
|
permission_classes = (permissions.IsAuthenticated,)
|
||||||
|
|
||||||
|
def get_queryset(self):
|
||||||
|
"""Override get_queryset method."""
|
||||||
|
qs = models.Product.objects.annotate_in_favorites(self.request.user)
|
||||||
|
return qs
|
||||||
|
|
||||||
|
|
||||||
class ProductTypeBackOfficeMixinView:
|
class ProductTypeBackOfficeMixinView:
|
||||||
"""Product type back-office mixin view."""
|
"""Product type back-office mixin view."""
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user