o
    1iy                     @   sF   d dl mZ d dlmZ d dlmZ d dlmZ G dd dejZ	dS )    )Any)	force_str)serializers)	countriesc                       sJ   e Zd Z fddZdd ZdefddZdd	 Zd
d Zdd Z	  Z
S )CountryFieldc                    sZ   | dd | _| dd | _| dd }|pt| _t j| jg|R i | |   d S )Ncountry_dict	name_onlyr   )popr   r   r   super__init___setup_spectacular_annotation)selfargskwargsfield_countries	__class__ c/var/www/html/humari/django-venv/lib/python3.10/site-packages/django_countries/serializer_fields.pyr   
   s   
zCountryField.__init__c                 C   sT   | j |}|s| jrd S dS | jrt| j |S | js|S |t| j |dS )N codename)r   alpha2
allow_nullr   r   r   r   )r   objr   r   r   r   to_representation   s   zCountryField.to_representationdatac                 C   sj   | j s|dkr| jd|d t|tr|d}| j|}|r3|s3| jt|}|s3| jd|d |S )Nr   invalid_choice)inputr   )	allow_blankfail
isinstancedictgetr   r   by_namer   )r   r   countryr   r   r   to_internal_value$   s   

zCountryField.to_internal_valuec                 C   s<   dddddddidddgd	}| j rd
|ddigi}|S )z.Return schema for country_dict representation.objectstring   )type	minLength	maxLengthr+   r   r   r   )r+   
propertiesrequiredoneOfnullr   r   schemar   r   r   _get_country_dict_schema1   s   
z%CountryField._get_country_dict_schemac                 C   s"   ddi}| j rd|ddigi}|S )z+Return schema for name_only representation.r+   r)   r0   r1   r2   r3   r   r   r   _get_name_only_schema?   s   z"CountryField._get_name_only_schemac                 C   s<   | j r|  }d|i| _dS | jr|  }d|i| _dS dS )ab  
        Set up schema annotation for drf-spectacular.

        This sets the _spectacular_annotation attribute that drf-spectacular
        checks when generating OpenAPI schemas. When country_dict=True or
        name_only=True, we override the default ChoiceField enum schema
        with the appropriate schema for the actual representation.
        fieldN)r   r5   _spectacular_annotationr   r6   r3   r   r   r   r   F   s   	z*CountryField._setup_spectacular_annotation)__name__
__module____qualname__r   r   r   r'   r5   r6   r   __classcell__r   r   r   r   r   	   s    r   N)
typingr   django.utils.encodingr   rest_frameworkr   django_countriesr   ChoiceFieldr   r   r   r   r   <module>   s
    