# coding=utf-8

from rest_framework import serializers
from apps.store.models import Shop, StoreBrand


class ShopSerializer(serializers.ModelSerializer):
    img = serializers.SerializerMethodField()
    brand_text = serializers.SerializerMethodField()

    def get_img(self, obj):
        if obj.img:
            return {'width': obj.img.width, 'height': obj.img.height, 'url': obj.img.get_path()}
        return ''

    def get_brand_text(self, obj):
        brand_rows = StoreBrand.objects.filter(store=obj)
        data = []
        for brand_row in brand_rows:
            data.append(brand_row.brand.name)
        return ','.join(data)

    class Meta:
        model = Shop
        fields = '__all__'


class ShopDetailSerializer(serializers.ModelSerializer):
    img = serializers.SerializerMethodField()

    def get_img(self, obj):
        if obj.img:
            return {'width': obj.img.width, 'height': obj.img.height, 'url': obj.img.get_path()}
        return ''

    class Meta:
        model = Shop
        fields = '__all__'
