o
    1ikV                     @   s"  U d dl Z d dlZd dlmZmZmZmZmZmZm	Z	m
Z
 d dlmZ er1d dlmZ d dlmZ d dlZd dlmZ d dlmZ d dlmZ d d	lmZ d d
lmZmZ d dlmZ d dlmZ d dl m!Z" d dl#m$Z$m%Z%m&Z&m'Z'm(Z( d dl)m*Z* ee e+d< zd dl,Z-ej.dkre-j/j0ddZ1n	e-j/0 2dg Z1W n e3y   d dl4Z4e45dZ1Y nw dd e1D Z6G dd dZ7G dd dZ8G dd dZ9G dd dZ:G dd de(j;Z;eee<f Z=ee<ee= f Z>ee	e=e>f  Z?G d d! d!e;ej@ZAG d"d# d#e;ejBZCG d$d% d%eZDeDjEG d&d' d'ejFZGeDjEG d(d) d)eGZHG d*d+ d+ejIZJeDjEG d,d- d-eJZKeDjEG d.d/ d/eKZLeDjEG d0d1 d1eJZMeDjEG d2d3 d3eMZNeDjEG d4d5 d5eJZOeDjEG d6d7 d7eOZPeDjEG d8d9 d9eJZQeDjEG d:d; d;eQZReSd<d= e&jT dS )>    N)TYPE_CHECKINGAnyIterableOptionalTupleTypeUnioncast)parse)overload)Self)forms)FieldListFilter)
exceptions)lookups)BLANK_CHOICE_DASH	CharField)	force_str)lazy)escape)	Countries	countriesfiltersioc_datawidgets)settings_entry_points)   
   zdjango_countries.Country)groupc                 C   s   i | ]}|j | qS  )nameload).0epr    r    X/var/www/html/humari/django-venv/lib/python3.10/site-packages/django_countries/fields.py
<dictcomp>)       r&   c                   @   sP   e Zd ZddgZdddZdefdd	Zdd
dZdedededdfddZ	dS )TemporaryEscapecountryoriginal_escapeCountryreturnNc                 C   
   || _ d S Nr)   )selfr)   r    r    r%   __init__/      
zTemporaryEscape.__init__c                 C   s   | j jS r.   )r)   _escaper0   r    r    r%   __bool__2   s   zTemporaryEscape.__bool__c                 C   s   | j j| _d| j _d S )NT)r)   r3   r*   r4   r    r    r%   	__enter__5   s   
zTemporaryEscape.__enter__typevalue	tracebackc                 C   s   | j | j_d S r.   )r*   r)   r3   )r0   r7   r8   r9   r    r    r%   __exit__9      zTemporaryEscape.__exit__)r)   r+   r,   N)r,   N)
__name__
__module____qualname__	__slots__r1   boolr5   r6   r   r:   r    r    r    r%   r(   ,   s    

r(   c                
   @   sT  e Zd Z			d2dedee dedee fddZdd	 Zd
d Zdd Z	dd Z
dd Zdd Zdd Zedd ZedefddZdefddZedefddZedefdd Zedee fd!d"Zedee fd#d$Zedefd%d&Zedefd'd(Zed)d* Ze	d3d+edee ded  fd,d-Zed.d/ Zd0d1 ZdS )4r+   Ncodeflag_urlstr_attrcustom_countriesc                 C   s:   || _ d| _|| _|tu rd }|| _| j|p|| _d S )NF)rB   r3   	_str_attrr   rD   alpha2rA   )r0   rA   rB   rC   rD   r    r    r%   r1   >   s   zCountry.__init__c                 C   s   t t| | jpdS N )r   getattrrE   r4   r    r    r%   __str__P   s   zCountry.__str__c                 C   s   t | jpdt |p
dkS rG   )r   rA   r0   otherr    r    r%   __eq__S   s   zCountry.__eq__c                 C   s   |  | S r.   )rM   rK   r    r    r%   __ne__V      zCountry.__ne__c                 C      t t| S r.   )hashr   r4   r    r    r%   __hash__Y   rO   zCountry.__hash__c                 C   s`   d| j g}| jd ur|d| j | jdkr#|d| j | jj dd| dS )Nzcode=z	flag_url=rA   z	str_attr=(, ))rA   rB   appendrE   	__class__r<   join)r0   argsr    r    r%   __repr__\   s   

zCountry.__repr__c                 C   
   t | jS r.   )r@   rA   r4   r    r    r%   r5   d   r2   zCountry.__bool__c                 C   rP   r.   )lenr   r4   r    r    r%   __len__g   rO   zCountry.__len__c                 C   s
   | j ptS r.   )rD   r   r4   r    r    r%   r   j      
zCountry.countriesr,   c                 C   s   t | S r.   )r(   r4   r    r    r%   r   n   s   zCountry.escapec                 C   s   | j rt|S |S r.   )r   escape_html)r0   textr    r    r%   maybe_escaper   s   zCountry.maybe_escapec                 C   s   |  | j| jS r.   )ra   r   r!   rA   r4   r    r    r%   r!   u   s   zCountry.namec                 C      | j | jS r.   )r   alpha3rA   r4   r    r    r%   rc   y      zCountry.alpha3c                 C   rb   r.   r   numericrA   r4   r    r    r%   rf   }   rd   zCountry.numericc                 C   s   | j j| jddS )NT)paddedre   r4   r    r    r%   numeric_padded   s   zCountry.numeric_paddedc                 C   sT   | j sdS | j}|d u rtj}|j| j | j  d}|sdS ttj|}| 	|S )NrH   )
code_upperrA   )
rA   rB   r   COUNTRIES_FLAG_URLformatlowerurlparseurljoin
STATIC_URLra   )r0   rB   urlr    r    r%   flag   s   
zCountry.flagc                 C   s,   | j sdS t| j  \}}d| d| S )a  
        Output the css classes needed to display an HTML element as a flag
        sprite.

        Requires the use of 'flags/sprite.css' or 'flags/sprite-hq.css'.
        Usage example::

            <i class="{{ ctry.flag_css }}" aria-label="{{ ctry.code }}></i>
        rH   zflag-sprite flag-z flag-_)rA   listrl   )r0   xyr    r    r%   flag_css   s   zCountry.flag_cssc                    s>   | j sdS d  fdd| j  D }t|d t|d  S )a  
        Generate a unicode flag for the given country.

        The logic for how these are determined can be found at:

        https://en.wikipedia.org/wiki/Regional_Indicator_Symbol

        Currently, these glyphs appear to only be supported on OS X and iOS.
        rH   i c                    s   g | ]}t |  qS r    )ord)r#   rs   OFFSETr    r%   
<listcomp>   r'   z(Country.unicode_flag.<locals>.<listcomp>r      )rA   upperchr)r0   pointsr    rw   r%   unicode_flag   s
   zCountry.unicode_flagioc_codec                 C   s&   t j| d}|dkrd S t||dS )NrH   )rB   )r   
IOC_TO_ISOgetr+   )r   rB   rA   r    r    r%   country_from_ioc   s   zCountry.country_from_iocc                 C   rb   r.   )r   r   rA   r4   r    r    r%   r      rd   zCountry.ioc_codec                 C   s   |t v r
t | | S tr.   )
EXTENSIONSAttributeError)r0   attrr    r    r%   __getattr__   s   zCountry.__getattr__)NrA   Nr.   )r<   r=   r>   strr   r   r1   rJ   rM   rN   rR   rZ   r5   r]   propertyr   r(   r   ra   r!   rc   intrf   rh   rq   ru   r~   staticmethodr   r   r   r    r    r    r%   r+   =   sf    



r+   c                   @   sX   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd ZdS )MultipleCountriesDescriptora  
    A list-like wrapper that provides proper string representation for Django admin.

    This makes CountryField(multiple=True) work correctly in admin list_display
    and readonly_fields by providing a comma-separated string of country names.

    Note: This does NOT inherit from list to avoid Django admin's special handling
    of list/tuple types in display_for_value, which would show codes instead of names.
    c                 C   s   t || _d S r.   )rr   
_countries)r0   countries_iterr    r    r%   r1      r;   z$MultipleCountriesDescriptor.__init__c                 C   s    | j sdS ddd | j D S )z7Return comma-separated country names for admin display.rH   rT   c                 s   s    | ]}t |jV  qd S r.   )r   r!   r#   r)   r    r    r%   	<genexpr>       z6MultipleCountriesDescriptor.__str__.<locals>.<genexpr>)r   rX   r4   r    r    r%   rJ      s   z#MultipleCountriesDescriptor.__str__c                 C   s   dd dd | jD  dS )z+Maintain list representation for debugging.[rT   c                 s       | ]}t |V  qd S r.   )reprr   r    r    r%   r          z7MultipleCountriesDescriptor.__repr__.<locals>.<genexpr>])rX   r   r4   r    r    r%   rZ      s   z$MultipleCountriesDescriptor.__repr__c                 C   r[   )zAllow iteration over countries.)iterr   r4   r    r    r%   __iter__   r^   z$MultipleCountriesDescriptor.__iter__c                 C   s
   | j | S )zAllow indexing.)r   )r0   indexr    r    r%   __getitem__   r^   z'MultipleCountriesDescriptor.__getitem__c                 C   r[   )zReturn number of countries.)r\   r   r4   r    r    r%   r]      r^   z#MultipleCountriesDescriptor.__len__c                 C   r[   )z#Return True if there are countries.)r@   r   r4   r    r    r%   r5      r^   z$MultipleCountriesDescriptor.__bool__c                 C   s    t |tr| j|jkS | j|kS )zCheck equality.)
isinstancer   r   rK   r    r    r%   rM      s   

z"MultipleCountriesDescriptor.__eq__c                 C   s   t | j| S )z
        Implement the + operator.

        Accepts country codes (strings) or Country objects. Validation and
        normalization is handled later by the field's get_clean_value() method
        when the result is assigned via __set__.
        )r   r   rK   r    r    r%   __add__   s   z#MultipleCountriesDescriptor.__add__N)r<   r=   r>   __doc__r1   rJ   rZ   r   r   r]   r5   rM   r   r    r    r    r%   r      s    
r   c                	   @   s~   e Zd ZU dZded< dddZer0eddd	edd
fddZ	eded	ede
eef fddZ	dddZ	dd Zdd ZdS )CountryDescriptoraQ  
    A descriptor for country fields on a model instance. Returns a Country when
    accessed so you can do things like::

        >>> from people import Person
        >>> person = Person.object.get(name='Chris')

        >>> person.country.name
        'New Zealand'

        >>> person.country.flag
        '/static/flags/nz.gif'
    CountryFieldfieldr,   Nc                 C   r-   r.   )r   )r0   r   r    r    r%   r1     r2   zCountryDescriptor.__init__instanceownerr   c                 C      d S r.   r    r0   r   r   r    r    r%   __get__     zCountryDescriptor.__get__c                 C   r   r.   r    r   r    r    r%   r     s   c                    sn   |d u r S  j j|jvr|j j jgd |j j j } j jr2|d u r'd S t fdd|D S  |S )N)fieldsc                 3   s    | ]}  |V  qd S r.   r/   r#   rA   r4   r    r%   r   .  r   z,CountryDescriptor.__get__.<locals>.<genexpr>)r   r!   __dict__refresh_from_dbmultipler   r)   )r0   r   r   r8   r    r4   r%   r   #  s   
c                 C   s   t || jj| jj| jjdS )N)rA   rB   rC   rD   )r+   r   countries_flag_urlcountries_str_attrr   )r0   rA   r    r    r%   r)   1  s   zCountryDescriptor.countryc                 C   s   | j |}||j| j j< d S r.   )r   get_clean_valuer   r!   )r0   r   r8   r    r    r%   __set__9  s   zCountryDescriptor.__set__)r   r   r,   N)NN)r<   r=   r>   r   __annotations__r1   r   r   r   r   r   r+   r   r)   r   r    r    r    r%   r     s$   
 


r   c                       s@   e Zd ZU eejj ed< ej	dk r fddZ
  ZS   ZS )LazyChoicesMixinwidget   r   c                    s   t  | || j_dS )z;
            Also update the widget's choices.
            N)super_set_choicesr   choicesr0   r8   rW   r    r%   r   C  s   zLazyChoicesMixin._set_choices)r<   r=   r>   r   r   r   ChoiceWidgetr   djangoVERSIONr   __classcell__r    r    r   r%   r   >  s
   
 
r   c                   @       e Zd ZU dZeed< ejZdS )LazyTypedChoiceFieldzL
    A form TypedChoiceField that respects choices being a lazy object.
    r   N)	r<   r=   r>   r   r   r   r   
LazySelectr   r    r    r    r%   r   P     
 
r   c                   @   r   )LazyTypedMultipleChoiceFieldzT
    A form TypedMultipleChoiceField that respects choices being a lazy object.
    r   N)	r<   r=   r>   r   r   r   r   LazySelectMultipler   r    r    r    r%   r   Y  r   r   c                       s  e Zd ZU dZeZeed< dedef fddZ	dd Z
 fd	d
Z fddZ fddZe fddZdd Zdd Z fddZejdkrUdeddf fdd	Zn	d) fdd	ZeeeZ fdd Z fd!d"Z fd#d$Zd%d& Z fd'd(Z  ZS )*r   zb
    A country field for Django models that provides all ISO 3166-1 countries as
    choices.
    r   rY   kwargsc                    s   | dd }|r| nt _| dd  _| dd _| dd  _| dd  _| dd _| d	d _tj	d
krF fdd|d< n j|d< d|vru jrht
 jjd tdd  jjD  |d< ntdd  jjD |d< t j|i | d S )Nr   r   r   rA   blank_labelr   multiple_uniqueTmultiple_sortr   c                      s    j S r.   )r   r    r4   r    r%   <lambda>z  s    z'CountryField.__init__.<locals>.<lambda>r   
max_lengthrz   c                 s   r   r.   r\   r   r    r    r%   r     r   z(CountryField.__init__.<locals>.<genexpr>c                 s   r   r.   r   r   r    r    r%   r     s    
)popr   r   r   r   r   r   r   r   r   r\   summaxr   r1   )r0   rY   r   countries_classr   r4   r%   r1   k  s.   


zCountryField.__init__c                 C   s   dS )Nr   r    r4   r    r    r%   get_internal_type  r   zCountryField.get_internal_typec                    s&   t  || t|| j| |  d S r.   )r   contribute_to_classsetattrr!   descriptor_class)r0   clsr!   r   r    r%   r     s   z CountryField.contribute_to_classc                    s    t t| j|i |}| |S )z)Returns field's value just before saving.)r   r   pre_saveget_prep_value)r0   rY   r   r8   r   r    r%   r     s   
zCountryField.pre_savec                    s2   |  |}| jr|rd|nd}tt| |S )z:Returns field's value prepared for saving into a database.,rH   )r   r   rX   r   r   r   r   r   r    r%   r     s   
zCountryField.get_prep_valuec                    s   | j rdS t jS )a]  
        Override flatchoices to prevent admin choice lookups for multiple fields.

        For multiple=True fields, Django admin's display_for_field tries to
        look up the value in flatchoices. Since the value is a
        MultipleCountriesDescriptor, we return None so Django skips the
        choice lookup and uses str() instead.
        N)r   r   flatchoicesr4   r   r    r%   r     s   
zCountryField.flatchoicesc                 C   s$   t |dr|j}|d u rd S t|S )NrA   )hasattrrA   r   r   r    r    r%   country_to_text  s
   
zCountryField.country_to_textc                 C   s   |d u rd S | j s| |S t|ttfr(t|tr$d|v r$|d}n|g}nzt| W n ty:   |g}Y nw g }t }|D ]}| |}|sLqB| j	rY||v rTqB|
| || qB| jrft|}|S )Nr   )r   r   r   r   r+   splitr   	TypeErrorsetr   addrV   r   sorted)r0   r8   cleaned_valueseencr    r    r%   r     s6   



zCountryField.get_clean_valuec                    sp   t t|  \}}}}|dd | jr| j|d< | js d|d< | js'd|d< | jtur2| jj|d< ||||fS )z
        Remove choices from deconstructed field, as this is the country list
        and not user editable.

        Not including the ``blank_label`` property, as this isn't database
        related.
        r   Nr   Fr   r   r   )	r   r   deconstructr   r   r   r   r   rW   )r0   r!   pathrY   r   r   r    r%   r     s   

zCountryField.deconstructr   TNr    c                    s:   | j rd}| jd u rt}nd| jfg}t j||||dS )NFrH   )include_blankblank_choicelimit_choices_toordering)r   r   r   r   get_choices)r0   r   r   r   r   r   r    r%   r     s   
zCountryField.get_choicesc                    sF   |d u r| j d u rt}nd| j fg}| jrd}t j|||d|S )NrH   F)r   r   )r   r   r   r   r   )r0   r   r   rY   r   r   r    r%   _get_choices_legacy  s   
z CountryField._get_choices_legacyc                    s<   | d| jrtnt d|vrt j|d< t jdi |S )Nchoices_form_classcoercer    )
setdefaultr   r   r   r   	to_python	formfield)r0   r   r   r    r%   r     s   zCountryField.formfieldc                    sH   | j s	t |S |s|S t|tr|d}t j  fdd|D S )Nr   c                    s   g | ]}|r |qS r    r    )r#   vparent_to_pythonr    r%   ry   %  r'   z*CountryField.to_python.<locals>.<listcomp>)r   r   r   r   r   r   r   r   r   r%   r     s   

zCountryField.to_pythonc                    s   | j s
t ||S | jsdS |r4| jdur4dd | jD }|D ]}||vr3tj| jd dd|idq | jsF|| j	v rFtj| jd dddS )	zR
        Use custom validation for when using a multiple countries field.
        Nc                 S   s   g | ]\}}|qS r    r    )r#   
option_keyoption_valuer    r    r%   ry   3  s    z)CountryField.validate.<locals>.<listcomp>invalid_choicer8   )rA   paramsblank)rA   )
r   r   validateeditabler   r   ValidationErrorerror_messagesr   empty_values)r0   r8   model_instancer   single_valuer   r    r%   r   '  s"   zCountryField.validatec                 C   s   |  |}| |S )z6
        Ensure data is serialized correctly.
        )value_from_objectr   )r0   objr8   r    r    r%   value_to_string@  s   

zCountryField.value_to_stringc                    s$   | j s|dv rd| }t |S )N)
contains	icontains
startswithistartswithendswith	iendswithregexiregexr!   inamecountry_)r   r   
get_lookup)r0   lookup_namer   r    r%   r
  G  s   
zCountryField.get_lookup)TN) r<   r=   r>   r   r   r   r   r   r   r1   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rr   r   r   r   r   r
  r   r    r    r   r%   r   b  s8   
 #

	r   c                   @   s.   e Zd ZU dZdZeed< dd Zdd ZdS )	ExactNameLookupcountry_nameFinsensitivec                 C   s"   t d| jjjjt| j| jdS )Nr   )r  )r	   lhsoutput_fieldr   by_namer   rhsr  r4   r    r    r%   get_prep_lookup]  s   zExactNameLookup.get_prep_lookupc                 C   s   |j d | S )Nexact)	operators)r0   
connectionr  r    r    r%   
get_rhs_opb  r;   zExactNameLookup.get_rhs_opN)	r<   r=   r>   r  r  r@   r   r  r  r    r    r    r%   r  X  s
   
 r  c                   @   s   e Zd ZU dZdZeed< dS )IExactNameLookupcountry_inameTr  N)r<   r=   r>   r  r  r@   r   r    r    r    r%   r  f  s   
 r  c                       s>   e Zd ZU eed< dZeed< dZeed<  fddZ  Z	S )FullNameLookupexprFr  Tescape_regexc                    s   t | jtrB| jj| jrt| jn| jd}td| j	j
jj|d| jd}t| jdkr@| j| j ks8| jr@|| j  |S t  S )N)r`   r   T)r  r     )r   r  r   r  rk   r  rer   r	   r  r  r   r  r  r\   r{   r   r   r  )r0   r8   optionsr   r    r%   r  q  s   
zFullNameLookup.get_prep_lookup)
r<   r=   r>   r   r   r  r@   r  r  r   r    r    r   r%   r  l  s
   
 r  c                   @      e Zd ZdZdZdS )CountryContainscountry_contains{text}Nr<   r=   r>   r  r  r    r    r    r%   r!        r!  c                   @   r   )CountryIContainscountry_icontainsTNr<   r=   r>   r  r  r    r    r    r%   r&    r%  r&  c                   @   r   )CountryStartswithcountry_startswithz^{text}Nr$  r    r    r    r%   r)    r%  r)  c                   @   r   )CountryIStartswithcountry_istartswithTNr(  r    r    r    r%   r+    r%  r+  c                   @   r   )CountryEndswithcountry_endswithz{text}$Nr$  r    r    r    r%   r-    r%  r-  c                   @   r   )CountryIEndswithcountry_iendswithTNr(  r    r    r    r%   r/    r%  r/  c                   @   s   e Zd ZdZdZdZdS )CountryRegexcountry_regexr#  FN)r<   r=   r>   r  r  r  r    r    r    r%   r1    s    r1  c                   @   r   )CountryIRegexcountry_iregexTNr(  r    r    r    r%   r3    r%  r3  c                 C   s
   t | tS r.   )r   r   )fr    r    r%   r     s   
 r   )Ur  systypingr   r   r   r   r   r   r   r	   urllibr
   rm   r   typing_extensionsr   r   r   django.contrib.admin.filtersr   django.corer   django.db.modelsr   django.db.models.fieldsr   r   django.utils.encodingr   django.utils.functionalr   django.utils.htmlr   r_   django_countriesr   r   r   r   r   django_countries.confr   r   importlib.metadata	importlibversion_infometadataentry_pointsr   r   ImportErrorpkg_resourcesiter_entry_pointsr   r(   r+   r   r   r   r   _Choice_ChoiceNamedGroup_FieldChoicesTypedChoiceFieldr   TypedMultipleChoiceFieldr   r   register_lookupExactr  r  Inr  r!  r&  r)  r+  r-  r/  r1  r3  registerCountryFilterr    r    r    r%   <module>   s   
 (

 9:		 w