'''
Created on Apr 10, 2013

@author: egiraldo
'''
from django.forms import widgets
from rest_framework import serializers
from conexiones.models import Conexion

#---Metodos Conexiones
class ConexionSerializer(serializers.Serializer):
    id_conexion= serializers.Field()  # Note: `Field` is an untyped read-only field.
    descripcion = serializers.CharField(widget=widgets.Textarea,max_length=200)
    sid = serializers.IntegerField(widget=widgets.Textarea)
    username  = serializers.CharField(widget=widgets.Textarea,max_length=50)
    password  = serializers.CharField(widget=widgets.Textarea,max_length=50)
    puerto  = serializers.IntegerField(widget=widgets.Textarea)
    host  = serializers.CharField(widget=widgets.Textarea,max_length=200)
    driver = serializers.CharField(widget=widgets.Textarea,max_length=100)
    esquema = serializers.CharField(widget=widgets.Textarea,max_length=200)
    service_name = serializers.IntegerField(widget=widgets.Textarea)
    def restore_object(self, attrs, instance=None):
        if instance:
            # Update existing instance
            instance.descripcion = attrs.get('descripcion', instance.descripcion)
            instance.sid = attrs.get('sid', instance.sid)
            instance.username = attrs.get('username', instance.username)
            instance.password = attrs.get('password', instance.password)
            instance.puerto = attrs.get('puerto', instance.puerto)
            instance.host = attrs.get('host', instance.host)
            instance.driver = attrs.get('driver', instance.host)
            instance.esquema = attrs.get('esquema', instance.host)
            instance.service_name = attrs.get('service_name', instance.host)
            return instance
        # Create new instance
        return Conexion(**attrs)