# site settings rest api serializers

from rest_framework import serializers
from saleor.wing.models import Wing as Table


class TableListSerializer(serializers.ModelSerializer):
    update_url = serializers.HyperlinkedIdentityField(view_name='wing:api-update')
    delete_url = serializers.HyperlinkedIdentityField(view_name='wing:api-delete')
    text = serializers.SerializerMethodField()

    class Meta:
        model = Table
        fields = ('id',
                  'name',
                  'text',
                  'description',
                  'update_url',
                  'delete_url'
                 )

    def get_text(self, obj):
        try:
            return obj.name
        except:
            return ''


class CreateListSerializer(serializers.ModelSerializer):
    class Meta:
        model = Table
        fields = ('id',
                  'name',
                  'description',
                 )

    def create(self, validated_data):
        instance = Table()
        instance.name = validated_data.get('name')
        if validated_data.get('description'):
            instance.description = validated_data.get('description')
        instance.save()

        return instance


class UpdateSerializer(serializers.ModelSerializer):
    class Meta:
        model = Table
        fields = ('id',
                  'name',
                  'description',
                 )

    def update(self, instance, validated_data):
        instance.name = validated_data.get('name', instance.name)
        instance.description = validated_data.get('description', instance.description)

        instance.save()
        return instance

[((8, 17, 8, 82), 'rest_framework.serializers.HyperlinkedIdentityField', 'serializers.HyperlinkedIdentityField', (), '', False, 'from rest_framework import serializers\n'), ((9, 17, 9, 82), 'rest_framework.serializers.HyperlinkedIdentityField', 'serializers.HyperlinkedIdentityField', (), '', False, 'from rest_framework import serializers\n'), ((10, 11, 10, 46), 'rest_framework.serializers.SerializerMethodField', 'serializers.SerializerMethodField', ({}, {}), '()', False, 'from rest_framework import serializers\n'), ((38, 19, 38, 26), 'saleor.wing.models.Wing', 'Table', ({}, {}), '()', True, 'from saleor.wing.models import Wing as Table\n')]