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
|
||||||
|
|
||||||
|
|
@ -76,13 +77,13 @@ class ProductQuerySet(models.QuerySet):
|
||||||
|
|
||||||
def with_base_related(self):
|
def with_base_related(self):
|
||||||
return self.select_related('product_type', 'establishment') \
|
return self.select_related('product_type', 'establishment') \
|
||||||
.prefetch_related('product_type__subtypes')
|
.prefetch_related('product_type__subtypes')
|
||||||
|
|
||||||
def with_es_related(self):
|
def with_es_related(self):
|
||||||
"""Returns qs with related for ES indexing."""
|
"""Returns qs with related for ES indexing."""
|
||||||
return self.with_base_related()\
|
return self.with_base_related() \
|
||||||
.prefetch_related('tags', 'standards','classifications', 'classifications__standard',
|
.prefetch_related('tags', 'standards', 'classifications', 'classifications__standard',
|
||||||
'classifications__classification_type','classifications__tags')\
|
'classifications__classification_type', 'classifications__tags') \
|
||||||
.select_related('wine_region', 'wine_sub_region')
|
.select_related('wine_region', 'wine_sub_region')
|
||||||
|
|
||||||
def common(self):
|
def common(self):
|
||||||
|
|
@ -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."""
|
||||||
|
|
@ -154,7 +168,7 @@ class Product(TranslatedFieldsMixin, BaseAttributes):
|
||||||
related_name='products',
|
related_name='products',
|
||||||
verbose_name=_('establishment'))
|
verbose_name=_('establishment'))
|
||||||
public_mark = models.PositiveIntegerField(blank=True, null=True, default=None,
|
public_mark = models.PositiveIntegerField(blank=True, null=True, default=None,
|
||||||
verbose_name=_('public mark'),)
|
verbose_name=_('public mark'), )
|
||||||
wine_region = models.ForeignKey('location.WineRegion', on_delete=models.PROTECT,
|
wine_region = models.ForeignKey('location.WineRegion', on_delete=models.PROTECT,
|
||||||
related_name='wines',
|
related_name='wines',
|
||||||
blank=True, null=True, default=None,
|
blank=True, null=True, default=None,
|
||||||
|
|
|
||||||
|
|
@ -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