# coding=utf-8 from django.conf import settings from rest_framework import serializers from apps.product.models import Product, ProductImg from apps.base import Formater from apps.customer.models import Customer from apps.foundation.models import Option, Config class ProductSerializer(serializers.ModelSerializer): cover_url = serializers.SerializerMethodField() price = serializers.SerializerMethodField() def get_cover_url(self, obj): if obj.cover: return {'width': obj.cover.width, 'height': obj.cover.height, 'url': obj.cover.get_path()} return '' def get_price(self, obj): return Formater.formatPriceShow(obj.price) class Meta: model = Product fields = ('id', 'cover_url', 'name', 'notes', 'price') class ProductDetailSerializer(serializers.ModelSerializer): price = serializers.SerializerMethodField() Product_images = serializers.SerializerMethodField() has_cover = serializers.SerializerMethodField() cover = serializers.SerializerMethodField() def get_cover(self, obj): if obj.cover: return {'width': obj.cover.width, 'height': obj.cover.height, 'url': obj.cover.get_path()} return '' def get_has_cover(self, obj): if obj.cover: return True return False def get_Product_images(self, obj): result = [] rows = ProductImg.objects.filter(product=obj).values('img__picture', 'img__width', 'img__height') for row in rows: url = {'width': row['img__width'], 'height': row['img__height'], 'url': '%s%s%s' % (settings.SERVER_DOMAIN, settings.MEDIA_URL, row['img__picture'])} result.append(url) return result def get_price(self, obj): return Formater.formatAmountShow(obj.discount_price) class Meta: model = Product fields = ( 'id', 'name', 'Product_images', 'describe', 'notes', 'has_cover', 'give_count', 'cover' ) class ProductTypeSerializer(serializers.ModelSerializer): class Meta: model = Option fields = ('id', 'name')