
    y<h/                     J   d dl mZmZmZmZmZmZ d dlmZ d dl	m
Z
mZmZ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Zd dlmZ  e       Zd Z G d de      Z G d de      Z  G d de      Z! G d d      Z" G d d      Z# G d d      Z$y
)    )LoginManager	UserMixin
login_userlogin_requiredlogout_usercurrent_user)geodesic)DataRequiredEmailLengthEqualTo)Error)datedatetime)r   )Bcrypt)	FlaskForm)StringFieldPasswordFieldSelectFieldBooleanField
validatorsNc                  H    t         j                  j                  dddd      S )N	localhostroot_z7b6CJcimmo_new)hostuserpassworddatabase)mysql	connectorconnect     /var/www/html/classes.pyget_db_connectionr'      s*    ??""	 #  r%   c                   "    e Zd Zd Zed        Zy)Userc                     || _         || _        || _        || _        g d}|j	                         D ]  \  }}||vst        | ||        d|v r|d   | _        y y )N)	is_activeis_authenticatedis_anonymousget_idr+   )idemailpassword_hashaccount_typeitemssetattr
_is_active)	selfuser_idr0   r1   r2   kwargsprotected_propertieskeyvalues	            r&   __init__zUser.__init__   st    
*(  [ !,,. 	*JC..c5)	*
 & $[1DO !r%   c                     t        | dd      S )Nr5   T)getattr)r6   s    r&   r+   zUser.is_active,   s     t\400r%   N)__name__
__module____qualname__r<   propertyr+   r$   r%   r&   r)   r)      s    2& 1 1r%   r)   c                   L   e Zd Z ed ed       ed      gddi      Z ed ed	       ed
d      gddi      Z	 ed ed       e
dd      gddi      Z edg d ed      g      Z ed ed      gddi      Z ed ed      gddi      Z ed      Zy)RegistrationFormr   L'email est obligatoire.messageFormat d'email invalide.placeholderVotre emailr   	render_kwMot de passe Le mot de passe est obligatoire.   u2   Le mot de passe doit faire au moins 8 caractères.)minrG   Votre mot de passezConfirmer le mot de passez&Veuillez confirmer votre mot de passe.r   z'Les mots de passe doivent correspondre.zConfirmez votre mot de passezType de compte))particulierParticulier)agent_immobilierzAgent immobilier)investisseurInvestisseur)autreAutreu)   Veuillez sélectionner un type de compte.)choicesr   u   Prénomu   Le prénom est obligatoire.u   Votre prénomNomzLe nom est obligatoire.z	Votre nomu   S'abonner à la newsletterN)r?   r@   rA   r   r
   r   r0   r   r   r   r   confirm_passwordr   r2   
first_name	last_namer   newsletter_subscriptionr$   r%   r&   rD   rD   1   s   !;<45
 !-0E !CDq"VW
 !"67H %#!IJJ(QR
 !"@A 
 !)TUV	L  )FGH /2J
  )BCD +.I
 ++HIr%   rD   c                       e Zd Z ed ed       ed      gddi      Z ed ed	      gdd
i      Z e	d      Z
y)	LoginFormr   rE   rF   rH   rI   rJ   rK   rM   rN   rQ   zSe souvenir de moiN)r?   r@   rA   r   r
   r   r0   r   r   r   remember_mer$   r%   r&   r`   r`   `   sa    !;<45
 !-0E  )KLM "67H
 34Kr%   r`   c                       e Zd Zed        Zy)Cityc                 4   	 |j                  d      }|j                  d| f       |j                         }|st        d|         yt        d|d    d|d	    d
       |S # t        j
                  j                  $ r}t        d|        Y d}~yd}~ww xY w)u:   
        Récupère une ville par son code INSEE.
        T
dictionarya  
            SELECT
                insee_code, city_name AS name, latitude, longitude,
                avg_price_per_sqm_apartment, avg_price_per_sqm_house,
                number_of_transactions
            FROM cities
            WHERE insee_code = %s
            u2   [DEBUG] Aucune ville trouvée avec le code INSEE: Nu   [DEBUG] Ville trouvée: namez	 (INSEE: 
insee_code)z2[DEBUG] Erreur MySQL dans City.get_by_insee_code: )cursorexecutefetchoneprintr!   r"   r   )rh   
connectionrj   	city_dataes        r&   get_by_insee_codezCity.get_by_insee_codeq   s    
	&&$&7FNN    )IJ:,WX,Yv->,?yS_I`Haabcd$$ 	FqcJK	s   AA" A" "B?BBN)r?   r@   rA   staticmethodrq   r$   r%   r&   rc   rc   p   s     r%   rc   c                       e Zd Zed        Zy)Districtc                    	 |j                  d      }|j                  d| f       |j                         }|st        d|         yt        d|d    d|d	    d
       |j                  d| f       |j                         |d<   |j                  d| f       |j	                         |d<   |S # t
        j                  j                  $ r}t        d|        Y d}~yd}~ww xY w)u4   
        Récupère un quartier par son ID.
        Tre   a  
            SELECT
                district_id, district_name AS name, latitude, longitude,
                avg_price_per_sqm_apartment, avg_price_per_sqm_house,
                number_of_transactions
            FROM districts
            WHERE district_id = %s
            u*   [DEBUG] Aucun quartier trouvé avec l'ID: Nu   [DEBUG] Quartier trouvé: rg   z (ID: district_idri   a  
            SELECT
                total_population, population_by_age, number_of_households,
                median_income_per_consumption_unit, unemployment_rate,
                number_of_active_establishments, establishment_type,
                number_of_housings, housing_type, census_year
            FROM insee_data
            WHERE insee_code = (SELECT insee_code FROM districts WHERE district_id = %s)
            
insee_dataa*  
            SELECT
                year, avg_price_per_sqm_apartment, avg_price_per_sqm_house,
                yearly_price_evolution, number_of_transactions
            FROM territory_indicators
            WHERE insee_code = (SELECT insee_code FROM districts WHERE district_id = %s)
            territory_indicatorsz.[DEBUG] Erreur MySQL dans District.get_by_id: )rj   rk   rl   rm   fetchallr!   r"   r   )rv   rn   rj   district_datarp   s        r&   	get_by_idzDistrict.get_by_id   s   
(	&&$&7FNN  ! #OO-M B;-PQ.}V/D.EVMZgLhKiijkl NN  ! +1//*;M,'NN  ! 5;OO4EM01  $$ 	B1#FG	s   AB. A%B. .C#CC#N)r?   r@   rA   rr   r{   r$   r%   r&   rt   rt      s    , ,r%   rt   c                       e Zd Zed        Zy)CityDataManagerc                 d   d }	 t               }|s<t        d       	 |r-|j                         r|j                          t        d       y y y 	 t	        j
                         j                  }|dz
  }|j                  dd      }|j                  d|| f       |j                         }t        d|        |dt               v r|j                          |r|j                          |r-|j                         r|j                          t        d       S S S # t        $ ru}t        d	|        Y d }~dt               v rj                          |r|j                          |r-|j                         r|j                          t        d       y y y d }~ww xY w# dt               v rj                          |r|j                          w w xY w# t         j"                  j$                  $ rG}t        d
|        Y d }~|r-|j                         r|j                          t        d       y y y d }~wt        $ rG}t        d|        Y d }~|r-|j                         r|j                          t        d       y y y d }~ww xY w# |r-|j                         r|j                          t        d       w w w xY w)Nu6   [DEBUG] Échec de la connexion à la base de données.u1   [DEBUG] Connexion à la base de données fermée.   T)rf   buffereda  
                    SELECT
                        c.insee_code,
                        c.city_name AS name,
                        c.latitude,
                        c.longitude,
                        c.department_code,
                        c.region_code,
                        p.avg_price_per_sqm_apartment,
                        p.avg_price_per_sqm_house,
                        p.number_of_transactions,
                        p.data_year
                    FROM cities c
                    LEFT JOIN prices p ON c.insee_code = p.insee_code AND p.data_year = %s
                    WHERE c.insee_code = %s
                u!   [DEBUG] Données récupérées : cursor_cityu8   [DEBUG] Erreur lors de la récupération des données : z9[DEBUG] Erreur MySQL dans CityDataManager.get_city_data: z>[DEBUG] Erreur inattendue dans CityDataManager.get_city_data: )r'   rm   is_connectedcloser   nowyearrj   rk   rl   locals	Exceptionry   lenfloatgetr!   r"   r   )rh   conncurrent_yearprevious_yearr   ro   rp   cursor_districts	districtsdistrictdistrict_cursorrw   rx   results                 r&   get_city_datazCityDataManager.get_city_data   s|   n	K$&DNOP ))+

IJ ,tM$!'||~22 ,q 0 #kkTDkI## % $Z02" (002	9)EF  !FH,%%'JJLD ))+

IJ ,tU  PQRPSTU !FH,%%'JJLD ))+

IJ ,tU
 !FH,%%'JJL x $$ 	MaSQR
 ))+

IJ ,t	  	RSTRUVW))+

IJ ,t		 ))+

IJ ,tsx   G	 A)D 6.G	 	FF+F /.G	 FF 1GG	 	I;&H(4I> (I;4I6I> 6I;;I> >1J/N)r?   r@   rA   rr   r   r$   r%   r&   r}   r}      s    pK pKr%   r}   )%flask_loginr   r   r   r   r   r   geopy.distancer	   wtforms.validatorsr
   r   r   r   mysql.connectorr   r   r   flask_bcryptr   	flask_wtfr   wtformsr   r   r   r   r   r!   bcryptr'   r)   rD   r`   rc   rt   r}   r$   r%   r&   <module>r      s    f f # C C ! # !   U U  #	19 12-Jy -J^5	 5  8. .`rK rKr%   