def _handle_setting_address(self, request, attr_field): """ Set the address of the basket. If ID is sent, the existing MutableAddress will be used instead. """ self.process_request() try: # take the address by ID if request.data.get("id"): address = MutableAddress.objects.get(id=request.data["id"]) else: serializer = AddressSerializer(data=request.data) if serializer.is_valid(): address = serializer.save() else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) setattr(request.basket, attr_field, address) request.basket.save() except ValidationError as exc: return Response({exc.code: exc.message}, status=status.HTTP_400_BAD_REQUEST) except MutableAddress.DoesNotExist: return Response({"error": "Address does not exist"}, status=status.HTTP_404_NOT_FOUND) else: return Response(self.get_serializer(request.basket).data, status=status.HTTP_200_OK)
class OrderSerializer(AvailableOrderMethodsMixin, serializers.ModelSerializer): lines = OrderLineSerializer(many=True) billing_address = AddressSerializer(read_only=True) shipping_address = AddressSerializer(read_only=True) payments = PaymentSerializer(many=True, read_only=True) payment_data = JSONField(read_only=True) shipping_data = JSONField(read_only=True) extra_data = JSONField(read_only=True) codes = JSONField(source="_codes", read_only=True) class Meta: model = Order fields = "__all__" ref_name = "RestApiOrderSerializer" def get_fields(self): fields = super(OrderSerializer, self).get_fields() for name, field in fields.items(): if name in ("status", "key", "label", "currency", "prices_include_tax"): field.required = False if name == "order_date": field.default = lambda: now() if name == "status": field.default = OrderStatus.objects.get_default_initial() return fields
class ContactSerializer(serializers.ModelSerializer): groups = ContactGroupSerializer(many=True, read_only=True) default_shipping_address = AddressSerializer(required=False) default_billing_address = AddressSerializer(required=False) class Meta: model = Contact exclude = ["identifier"] extra_kwargs = {"created_on": {"read_only": True}}
class BaseOrderSerializer(serializers.Serializer): currency = serializers.SerializerMethodField() shipping_address = AddressSerializer() billing_address = AddressSerializer() payment_method = PaymentMethodSerializer() shipping_method = ShippingMethodSerializer() def get_currency(self, order): return CurrencySerializer(Currency.objects.get(code=order.currency), context=self.context).data
class ShopSerializer(TranslatableModelSerializer): translations = SchemaTranslatedField(shared_model=Shop) status = EnumField(ShopStatus) logo = serializers.SerializerMethodField() favicon = serializers.SerializerMethodField() contact_address = AddressSerializer(read_only=True) distance = serializers.SerializerMethodField() options = serializers.JSONField(binary=False, required=False) labels = LabelSerializer(many=True, required=False) class Meta: model = Shop kwargs = { "modified_on": {"read_only": True}, "created_on": {"read_only": True}, } exclude = ("identifier",) def to_representation(self, instance): data = super(ShopSerializer, self).to_representation(instance) data["currency"] = CurrencySerializer(Currency.objects.get(code=instance.currency), context=self.context).data return data def get_logo(self, shop): if shop.logo: return self.context["request"].build_absolute_uri(shop.logo.url) def get_favicon(self, shop): if shop.favicon: return self.context["request"].build_absolute_uri(shop.favicon.url) def get_distance(self, shop): return getattr(shop, "distance", 0)
class ContactSerializer(serializers.ModelSerializer): default_shipping_address = AddressSerializer(required=False) default_billing_address = AddressSerializer(required=False) gender = EnumField(Gender, required=False) name = serializers.CharField(required=False) timezone = TimezoneField(required=False) class Meta: model = PersonContact exclude = [ "identifier", "tax_group", "polymorphic_ctype", "account_manager", ] extra_kwargs = {"created_on": {"read_only": True}} ref_name = "FrontUserContactSerializer" def update_address(self, instance, field_name, validated_address_data): if not validated_address_data: return None contact_address = getattr(instance, field_name) if contact_address: MutableAddress.objects.filter(pk=contact_address.pk).update( **validated_address_data) contact_address.refresh_from_db() else: address = MutableAddress(**validated_address_data) address.save() setattr(instance, field_name, address) instance.save() def update(self, instance, validated_data): default_shipping_address = validated_data.pop( "default_shipping_address", None) default_billing_address = validated_data.pop("default_billing_address", None) instance = super(ContactSerializer, self).update(instance, validated_data) self.update_address(instance, "default_shipping_address", default_shipping_address) self.update_address(instance, "default_billing_address", default_billing_address) return instance
class OrderShopSerializer(serializers.ModelSerializer): logo = serializers.SerializerMethodField() contact_address = AddressSerializer() options = serializers.JSONField(binary=False, required=False) labels = LabelSerializer(many=True) class Meta: model = Shop fields = ("id", "name", "description", "short_description", "logo", "options", "contact_address", "labels") def get_logo(self, shop): if shop.logo: return self.context["request"].build_absolute_uri(shop.logo.url)
def get_shipping_address(self, basket): if basket._data.get("shipping_address_id"): address = MutableAddress.objects.filter( id=basket._data["shipping_address_id"]).first() return AddressSerializer(address, context=self.context).data