winofy-backend/apps/reviews/serializers.py
Ali Asadi 52adc732fa Initial Winofy backend: marketplace core (no payment integration)
Django 6 + DRF resource server against the Gooyal accounts OAuth2 service,
matching the Winsoo ecosystem's conventions. Covers locations, stores,
catalog, cart, checkout/orders (with the multi-store-cart split and the
status stepper), reviews, and notifications, plus a demo-data seed command.

Payment integration (wallet debits, online gateway, seller payouts) is
intentionally left out here — see feature/payment.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-10 14:03:15 +03:30

55 lines
2.2 KiB
Python

from rest_framework import serializers
from apps.orders.models import Order
from .models import Review
class ReviewSerializer(serializers.ModelSerializer):
order_uuid = serializers.PrimaryKeyRelatedField(source='order', queryset=Order.objects.all(), write_only=True)
customer_name = serializers.CharField(source='customer.username', read_only=True)
class Meta:
model = Review
fields = (
'uuid', 'order_uuid', 'store', 'product', 'rating', 'comment',
'seller_reply', 'customer_name', 'created_at',
)
read_only_fields = ('store', 'product', 'seller_reply')
def validate(self, attrs):
order = attrs['order']
request = self.context['request']
if order.customer_id != request.user.pk:
raise serializers.ValidationError({'order_uuid': 'این سفارش متعلق به شما نیست.'})
if order.status != Order.Status.DELIVERED:
raise serializers.ValidationError({'order_uuid': 'فقط سفارش‌های تحویل داده شده را می‌توان نظر داد.'})
if hasattr(order, 'review'):
raise serializers.ValidationError({'order_uuid': 'برای این سفارش قبلاً نظر ثبت شده است.'})
return attrs
def create(self, validated_data):
order = validated_data['order']
validated_data['customer'] = self.context['request'].user
validated_data['store'] = order.store
validated_data['product'] = order.items.first().product if order.items.exists() else None
review = super().create(validated_data)
from .services import refresh_store_rating
refresh_store_rating(review.store)
from apps.orders.services import notify
from apps.orders.models import Notification
notify(
recipient=review.store.owner,
type=Notification.Type.NEW_REVIEW,
title='ثبت نظر جدید',
body=f'مشتری برای سفارش {order.uuid} یک نظر {review.rating} ستاره ثبت کرد.',
related_order=order,
)
return review
class SellerReplySerializer(serializers.Serializer):
seller_reply = serializers.CharField()