#coding=utf-8

from rest_framework import serializers
from utils.format import strftime,strfdate
from apps.base import Formater
class BooleanCharField(serializers.BooleanField):
    def get_attribute(self, obj):
        return obj

    def to_representation(self, obj):
        if '.' in self.source:
            k = self.source.split('.')
            has_r = hasattr(obj, k[0])
            if has_r:
                r = getattr(obj, k[0])
                if hasattr(r, k[1]):
                    val = getattr(r, k[1])
                else:
                    val = self.get_attribute(obj)
            else:
                return u'未知'
        else:
            val = getattr(obj, self.source)
        return u'是' if val else u'否'

class TimeCharField(serializers.CharField):
    def get_attribute(self, obj):
        return obj

    def to_representation(self, obj):
        if '.' in self.source:
            k = self.source.split('.')
            has_r = hasattr(obj, k[0])
            if has_r:
                r = getattr(obj, k[0])
                if hasattr(r, k[1]):
                    val = getattr(r, k[1])
                else:
                    val = self.get_attribute(obj)
            else:
                return ''
        else:
            val = getattr(obj, self.source)

        return strftime(val)

class DateCharField(serializers.CharField):
    def get_attribute(self, obj):
        return obj

    def to_representation(self, obj):
        if '.' in self.source:
            k = self.source.split('.')
            has_r = hasattr(obj, k[0])
            if has_r:
                r = getattr(obj, k[0])
                if hasattr(r, k[1]):
                    val = getattr(r, k[1])
                else:
                    val = self.get_attribute(obj)
            else:
                return ''
        else:
            val = getattr(obj, self.source)
        return strfdate(val)

class AmountShowCharField(serializers.CharField):
    def get_attribute(self, obj):
        return obj

    def to_representation(self, obj):
        if '.' in self.source:
            k = self.source.split('.')
            has_r = hasattr(obj, k[0])
            if has_r:
                r = getattr(obj, k[0])
                if hasattr(r, k[1]):
                    val = getattr(r, k[1])
                else:
                    val = self.get_attribute(obj)
            else:
                return ''
        else:
            val = getattr(obj, self.source)

        return Formater.formatAmountShow(val)

class PriceShowCharField(serializers.CharField):
    def get_attribute(self, obj):
        return obj

    def to_representation(self, obj):
        if '.' in self.source:
            k = self.source.split('.')
            has_r = hasattr(obj, k[0])
            if has_r:
                r = getattr(obj, k[0])
                if hasattr(r, k[1]):
                    val = getattr(r, k[1])
                else:
                    val = self.get_attribute(obj)
            else:
                return ''
        else:
            val = getattr(obj, self.source)

        return Formater.formatPriceShow(val)

class CountShowCharField(serializers.CharField):
    def get_attribute(self, obj):
        return obj

    def to_representation(self, obj):
        if '.' in self.source:
            k = self.source.split('.')
            has_r = hasattr(obj, k[0])
            if has_r:
                r = getattr(obj, k[0])
                if hasattr(r, k[1]):
                    val = getattr(r, k[1])
                else:
                    val = self.get_attribute(obj)
            else:
                return ''
        else:
            val = getattr(obj, self.source)

        return Formater.formatCountShow(val)
