
    =
j                    r   d dl mZ d dl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 d dlmZ d d	lmZ d
Z ed       G d d             Zd!dZd!dZd"dZdddd#dZddddd$dZddd	 	 	 	 	 	 	 d%dZddddd&dZd'dZdddddd	 	 	 	 	 	 	 	 	 	 	 	 	 d(dZ	 	 d)ddd	 	 	 	 	 	 	 	 	 	 	 d*d Zy)+    )annotationsN)	dataclass)Any)Q)ValidationError)IstatMunicipality)PROVINCE_MAPPINGS)normalize_countryITT)frozenc                  N    e Zd ZU ded<   ded<   ded<   ded<   ded<   d	Zded
<   y)ResolvedResidenceboolis_italian_residentIstatMunicipality | Nonemunicipalityz
str | Nonemunicipality_codemunicipality_nameprovince_codeTprovince_matchesN)__name__
__module____qualname____annotations__r         /backend/istat/municipalities.pyr   r      s+    **!!!!!d!r   r   c                R   t        | xs d      j                         j                         }|syt        j                  d|      }dj                  d |D              }t        j                  dd|t        j                        }t        j                  dd|      j                         S )N NFKDc              3  L   K   | ]  }t        j                  |      r|  y w)N)unicodedata	combining).0chars     r   	<genexpr>z.normalize_municipality_name.<locals>.<genexpr>"   s     LDD0E0Ed0K4Ds   $$z[\W_]+ )flagsz\s+)	strstriplowerr"   	normalizejoinresubUNICODE)valuetexts     r   normalize_municipality_namer3      s    u{!!#))+D  .D77LDLLD66)S$bjj9D66&#t$**,,r   c                X    t        | xs d      j                         j                         S Nr   )r)   r*   upper)r1   s    r   normalize_province_coder7   '   s#    u{!!#))++r   c                    t        |       }t        |xs | xs d      j                         j                         t        k(  S r5   )r
   r)   r*   r6   
ITALY_ISO2)country
normalizeds     r   is_italian_residence_countryr<   +   s8    "7+Jz*W*+11399;zIIr   city
extra_datac                    t        |t              r|ni }| xs7 |j                  d      xs$ |j                  d      xs |j                  d      S )Nr>   residence_cityresidence_municipality
isinstancedictget)r>   r?   extras      r   !get_residence_municipality_sourcerH   0   sQ    $Z6JBE 	/99V	/99%&	/ 99-.	r   provinceregionr?   c                    t        |t              r|ni }| xs; |j                  d      xs( |j                  d      xs |j                  d      xs |S )NrJ   residence_provincestaterC   )rJ   rK   r?   rG   s       r   get_residence_province_sourcerO   :   sZ    $Z6JBE 	99Z 	99)*	 99W	 r   )rJ   active_onlyc                  t        | xs d      j                         }|sy t        j                  j	                         }|r|j                  d      }t        |      }|r|j                  |      }|j                         r5|j                  d      }|j                  |      j                         }||S t        |      }|sy |j                  |      j                  dd	      j                         S )
Nr   T	is_activerJ   	   )code)normalized_namenamerV   )r)   r*   r   objectsallfilterr7   isdigitzfillfirstr3   order_by)	r1   rJ   rP   	raw_valuequerysetr   rV   r   rW   s	            r   resolve_municipalityrb   E   s     EKR &&(I ((,,.H??T?2+H5M??M?:q!D1779#1)<O????;DDVVTZZ\\r      )rJ   searchlimitc                   t        |       }t        j                  j                  d      }|r|j                  |      }t	        |xs d      j                         }|rst        |      }t        |      }|r|t        |      z  }|j                         r*|t        |j                  d      d t        |             z  }|j                  |      }|j                  d	d
d      d t        t        |d      d       S )NTrR   rT   r   )name__icontains)normalized_name__icontainsrU   )code__startswithrX   rJ   rV   d      )r7   r   rY   r[   r)   r*   r3   r   r\   r]   lenr_   maxmin)rJ   rd   re   r   ra   
raw_searchnormalized_searchquerys           r   search_municipalitiesrr   c   s    +H5M ((//$/?H??M?:V\r"((*J7
C*-Q2CDDEQ
(8(8(;<Mc*o(NOOE??5)VZ89R3s5#PQ;RSSr   c                    | j                   rd| j                    dnd}| j                  | j                  | j                   xs d| j                  | j                   | dS )Nz ()r   )rV   rX   rJ   r1   label)rJ   rV   rX   )r   label_provinces     r   serialize_municipalityrw   v   si    6B6K6Kr,//02QSN!!!! ))/R"" %%&~&67 r   F)r>   rJ   rK   r?   require_for_italyc                   t        |       }t        t        |||            }t        ||      }|s/t	        dd d |rt        |      j                         nd |xs d       S t        ||      }	d }
|	s|rt        |      }
|s|
}	|
d uxr |xr |
j                  |k7   }|sEt	        d|	|	r|	j                  nd |	r|	j                  nd |xs |	r|	j                  |      S d |      S i }|sd|d	<   n|t        vrd
|d	<   |sd|d<   n|	d|d<   |s|j                  d	d       |rt        |      t	        d|	|	j                  |	j                  |d      S )NrI   r=   F)r   r   r   r   r   rT   T)r   r   r   r   r   r   z+Province is required for Italian residents.rJ   z/Province must be a valid Italian province code.z/Municipality is required for Italian residents.r>   z;Municipality must be selected from the official ISTAT list.z.Province must match the selected municipality.)r<   r7   rO   rH   r   r)   r*   rb   rJ   rV   rX   r	   
setdefaultr   )r:   r>   rJ   rK   r?   rx   
is_italianr   municipality_valuer   municipality_without_provincer   errorss                r   resolve_residencer      s    .g6J+%!	
M ;
   %"ASc"45;;=Y]'/4
 	
 ((:]SL$(!.(<=O(P%8L%T1 	D	D)22mC   $%3?l//T3?l//T'\\L,A,A-
 	

 X\-
 	
  FJz	/	/NzJv		Vv*&VWf%% !&++&++# r   )rK   r?   c               $    t        | ||||d      S )NT)r:   r>   rJ   rK   r?   rx   )r   )r:   rJ   r   rK   r?   s        r   validate_italian_residencer      s#      r   )r1   r   returnr)   )r:   r   r   r   )r>   r   r?   dict | Noner   r   )rJ   r   rK   r   r?   r   r   r   )r1   r   rJ   r   rP   r   r   r   )rJ   r   rd   r   re   int)r   r   r   zdict[str, str])r:   r   r>   r   rJ   r   rK   r   r?   r   rx   r   r   r   )NN)r:   r   rJ   r   r   r   rK   r   r?   r   r   r   )
__future__r   r.   r"   dataclassesr   typingr   django.db.modelsr   rest_framework.exceptionsr   istat.modelsr   #istat.xml_export.mappings.provincesr	   services.country_utilsr
   r9   r   r3   r7   r<   rH   rO   rb   rr   rw   r   r   r   r   r   <module>r      sv   " 	  !   5 * A 4 
 $" " "-,J
 6:UY  6:im  	]] ] 	]
 ]< .2TV T& "#MM M 	M
 M M M Md 
 " 
   r   