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>
60 lines
2.3 KiB
Python
60 lines
2.3 KiB
Python
from rest_framework import serializers
|
|
|
|
from .models import Address, City, Neighborhood
|
|
|
|
|
|
class CitySerializer(serializers.ModelSerializer):
|
|
class Meta:
|
|
model = City
|
|
fields = ('uuid', 'name', 'slug')
|
|
|
|
|
|
class NeighborhoodSerializer(serializers.ModelSerializer):
|
|
city = CitySerializer(read_only=True)
|
|
city_uuid = serializers.PrimaryKeyRelatedField(
|
|
source='city', queryset=City.objects.filter(is_active=True), write_only=True,
|
|
)
|
|
|
|
class Meta:
|
|
model = Neighborhood
|
|
fields = ('uuid', 'name', 'slug', 'city', 'city_uuid')
|
|
|
|
|
|
class AddressSerializer(serializers.ModelSerializer):
|
|
neighborhood = NeighborhoodSerializer(read_only=True)
|
|
neighborhood_uuid = serializers.PrimaryKeyRelatedField(
|
|
source='neighborhood', queryset=Neighborhood.objects.filter(is_active=True),
|
|
write_only=True, required=False, allow_null=True,
|
|
)
|
|
city_uuid = serializers.PrimaryKeyRelatedField(
|
|
source='city', queryset=City.objects.filter(is_active=True), write_only=True,
|
|
)
|
|
city = CitySerializer(read_only=True)
|
|
latitude = serializers.FloatField(write_only=True, required=False)
|
|
longitude = serializers.FloatField(write_only=True, required=False)
|
|
|
|
class Meta:
|
|
model = Address
|
|
fields = (
|
|
'uuid', 'label', 'title', 'full_address', 'plaque', 'floor', 'unit',
|
|
'recipient_name', 'recipient_phone', 'is_default',
|
|
'city', 'city_uuid', 'neighborhood', 'neighborhood_uuid',
|
|
'latitude', 'longitude', 'created_at',
|
|
)
|
|
|
|
def create(self, validated_data):
|
|
lat = validated_data.pop('latitude', None)
|
|
lng = validated_data.pop('longitude', None)
|
|
if lat is not None and lng is not None:
|
|
from django.contrib.gis.geos import Point
|
|
validated_data['location'] = Point(lng, lat, srid=4326)
|
|
validated_data['user'] = self.context['request'].user
|
|
return super().create(validated_data)
|
|
|
|
def update(self, instance, validated_data):
|
|
lat = validated_data.pop('latitude', None)
|
|
lng = validated_data.pop('longitude', None)
|
|
if lat is not None and lng is not None:
|
|
from django.contrib.gis.geos import Point
|
|
validated_data['location'] = Point(lng, lat, srid=4326)
|
|
return super().update(instance, validated_data)
|