# coding=utf-8
import datetime
from rest_framework import serializers
from .models import *
from django.conf import settings
from utils.booleancharfield import PriceShowCharField
from apps.base import Formater

class OrderSerializer(serializers.ModelSerializer):
    payment_name = serializers.CharField(source='get_payment_display', read_only=True)
    create_user_text = serializers.CharField(source='create_user.username', read_only=True)
    status_text = serializers.CharField(source='get_status_display', read_only=True)

    total_amount = PriceShowCharField(read_only=True)
    actual_amount = PriceShowCharField(read_only=True)
    class Meta:
        model = Order
        fields = '__all__'

    def create(self, validated_data):
        validated_data['create_user'] = self.context['request'].user
        instance = super(OrderSerializer, self).create(validated_data)
        instance.no = instance.get_no()
        instance.save()
        return instance

class CouponSerializer(serializers.ModelSerializer):
    create_user_text = serializers.CharField(source='create_user.username', read_only=True)
    amount = PriceShowCharField(read_only=True)
    class Meta:
        model = Coupon
        fields = '__all__'

    def create(self, validated_data):
        validated_data['create_user'] = self.context['request'].user
        instance = super(CouponSerializer, self).create(validated_data)
        instance.save()
        return instance
