add user rating logic to dynamic product serializer for its instace view type
This commit is contained in:
@@ -159,7 +159,7 @@ class DynamicProductSerializer(serializers.ModelSerializer):
|
|||||||
customer_pickup_title = serializers.SerializerMethodField()
|
customer_pickup_title = serializers.SerializerMethodField()
|
||||||
customer_pickup_description = serializers.SerializerMethodField()
|
customer_pickup_description = serializers.SerializerMethodField()
|
||||||
average_rating = serializers.SerializerMethodField()
|
average_rating = serializers.SerializerMethodField()
|
||||||
|
user_rating = serializers.SerializerMethodField()
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
super().__init__(*args, **kwargs)
|
||||||
view_type = self.context.get('view_type', 'all')
|
view_type = self.context.get('view_type', 'all')
|
||||||
@@ -177,10 +177,21 @@ class DynamicProductSerializer(serializers.ModelSerializer):
|
|||||||
view_type = {
|
view_type = {
|
||||||
'list': ['id', 'name', 'rating', 'slug', 'category', 'colors', 'image', 'best_deal_price_before_discount', 'best_deal_price_after_discount', 'best_deal_discount', 'main_image', 'average_rating'],
|
'list': ['id', 'name', 'rating', 'slug', 'category', 'colors', 'image', 'best_deal_price_before_discount', 'best_deal_price_after_discount', 'best_deal_discount', 'main_image', 'average_rating'],
|
||||||
'slider': ['id', 'name', 'rating', 'slug', 'category', 'variants', 'colors', 'image', 'best_deal_price_before_discount', 'best_deal_price_after_discount', 'best_deal_discount', 'average_rating'],
|
'slider': ['id', 'name', 'rating', 'slug', 'category', 'variants', 'colors', 'image', 'best_deal_price_before_discount', 'best_deal_price_after_discount', 'best_deal_discount', 'average_rating'],
|
||||||
'instance': ['id', 'name', 'description', 'rating', 'slug', 'meta_description', 'meta_keywords', 'meta_rating', 'category', 'related_products', 'in_pack_items', 'variants', 'colors', 'added_to_favorites', 'image', 'customer_pickup_title', 'customer_pickup_description', 'average_rating'],
|
'instance': ['id', 'name', 'description', 'rating', 'slug', 'meta_description', 'meta_keywords', 'meta_rating', 'category', 'related_products', 'in_pack_items', 'variants', 'colors', 'added_to_favorites', 'image', 'customer_pickup_title', 'customer_pickup_description', 'average_rating', 'user_rating'],
|
||||||
'chat': ['id', 'name', 'description', 'variants', 'image']
|
'chat': ['id', 'name', 'description', 'variants', 'image']
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def get_user_rating(self, obj):
|
||||||
|
request = self.context.get('request')
|
||||||
|
if not request.user.is_authenticated:
|
||||||
|
return None
|
||||||
|
product_ratings = obj.ratings.all()
|
||||||
|
if product_ratings.filter(user=request.user).exists():
|
||||||
|
return product_ratings.filter(user=request.user).first().rating
|
||||||
|
else:
|
||||||
|
return None
|
||||||
|
|
||||||
def _get_best_deal_variant(self, obj):
|
def _get_best_deal_variant(self, obj):
|
||||||
"""Get best deal variant from prefetched variants (pre-ordered by discount/price)"""
|
"""Get best deal variant from prefetched variants (pre-ordered by discount/price)"""
|
||||||
if not hasattr(self, '_best_deal_cache'):
|
if not hasattr(self, '_best_deal_cache'):
|
||||||
|
|||||||
Reference in New Issue
Block a user