from django.utils import timezone
from rest_framework import serializers
from .models import BaiheForm, BaiheFormData


class BaiheFormSerializers(serializers.HyperlinkedModelSerializer):

    class Meta:
        model = BaiheForm
        fields = '__all__'

    def validate_start_time(self, value):
        if value < timezone.now():
            raise serializers.ValidationError(
                'start_time must be after current time'
            )
        return value

    def validate_end_time(self, value):
        if value < timezone.now():
            raise serializers.ValidationError(
                'end_time must be after current time'
            )
        return value

    def validate_structure(self, value):
        '''
        @ field_type
        @ label_name
        @ placeholder
        @ choices
        @ default
        @ required
        @ validation
        '''

        if not isinstance(value, dict):
            raise serializers.ValidationError(
                'structure must be a dict not a `%s`' % type(value)
            )
        i = 0
        for field_order, field in value:
            if field_order != i:
                raise serializers.ValidationError(
                    'field order not consistent at `%d`' % i
                )
            i += 1

            key_has_field_type = False
            key_has_label_name = False

            for key, v in field:
                if key == 'field_type' and v:
                    key_has_field_type = True
                elif key == 'label_name' and v:
                    key_has_label_name = True

            if not (key_has_field_type and key_has_label_name):
                raise serializers.ValidationError(
                    'field_type and label_name required'
                )

        return value

    def validate(self, data):
        if data['start_time'] >= data['end_time']:
            raise serializers.ValidationError(
                'start_time must be before end_time'
            )
        return data


class BaiheFormDataSerializers(serializers.HyperlinkedModelSerializer):

    class Meta:
        model = BaiheFormData
        fields = '__all__'
        read_only_fields = ('user',)

@@ -1,12 +1,30 @@
+import traceback%0A%0A
 from django.
@@ -84,16 +84,17 @@
 alizers%0A
+%0A
 from .mo
@@ -127,24 +127,1262 @@
 heFormData%0A%0A
+FIELD_TYPE_CHOICES = %5B'text', 'phone', 'email', 'textarea',%0A                      'radio', 'select', 'checkbox', 'file', 'image'%5D%0AVALIDATION_CHOICES = %5B'max_length', 'min_length'%5D%0A%0A%0Aclass FormStructureSerializers(serializers.Serializer):%0A    field_type = serializers.ChoiceField(FIELD_TYPE_CHOICES)%0A    label_name = serializers.CharField(max_length=100)%0A    placeholder = serializers.CharField(%0A        max_length=100,%0A        required=False,%0A        allow_blank=True%0A    )%0A    choices = serializers.CharField(%0A        max_length=100,%0A        required=False,%0A        allow_blank=True%0A    )%0A    default = serializers.CharField(%0A        max_length=100,%0A        required=False,%0A        allow_blank=True%0A    )%0A    required = serializers.BooleanField(initial=True, required=False)%0A    validation = serializers.ChoiceField(%0A        VALIDATION_CHOICES,%0A        required=False,%0A        allow_blank=True%0A    )%0A    addable = serializers.BooleanField(required=False)%0A%0A    def validate(self, data):%0A        if data%5B'field_type'%5D in %5B'radio', 'select', 'checkbox'%5D and not data.get('choices'):%0A            raise serializers.ValidationError(%0A                'choices not provided for %60%25s%60 field' %25 data%5B'field_type'%5D%0A            )%0A        return data%0A%0A
 %0Aclass Baihe
@@ -1429,32 +1429,84 @@
 odelSerializer):
+%0A    structure = FormStructureSerializers(many=True)
 %0A%0A    class Meta
@@ -1561,16 +1561,226 @@
 all__'%0A%0A
+    def create(self, validated_data):%0A        structure = validated_data.pop('structure')%0A        form = BaiheForm(**validated_data)%0A        form.structure = structure%0A        form.save()%0A%0A        return form%0A%0A
     def 
@@ -2155,32 +2155,32 @@
 r current time'%0A
+
             )%0A  
@@ -2203,1133 +2203,8 @@
 ue%0A%0A
-    def validate_structure(self, value):%0A        '''%0A        @ field_type%0A        @ label_name%0A        @ placeholder%0A        @ choices%0A        @ default%0A        @ required%0A        @ validation%0A        '''%0A%0A        if not isinstance(value, dict):%0A            raise serializers.ValidationError(%0A                'structure must be a dict not a %60%25s%60' %25 type(value)%0A            )%0A        i = 0%0A        for field_order, field in value:%0A            if field_order != i:%0A                raise serializers.ValidationError(%0A                    'field order not consistent at %60%25d%60' %25 i%0A                )%0A            i += 1%0A%0A            key_has_field_type = False%0A            key_has_label_name = False%0A%0A            for key, v in field:%0A                if key == 'field_type' and v:%0A                    key_has_field_type = True%0A                elif key == 'label_name' and v:%0A                    key_has_label_name = True%0A%0A            if not (key_has_field_type and key_has_label_name):%0A                raise serializers.ValidationError(%0A                    'field_type and label_name required'%0A                )%0A%0A        return value%0A%0A
     

update form structure validation