o
    FhV                     @   s6  d Z ddlZddlmZm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 ddlmZ ddlmZmZmZmZmZ dd	lmZ dd
lmZmZ edeZejdddgddejdddgdddd Zejddgddd Z ejddgddd Z!ededd Z"ejdddgded d! Z#dS )"aB  
views.py

Purpose:
  User-facing HTML pages that are not pure APIs: login/logout, selection page,
  add camera page. This blueprint wires routes and uses shared services.

Routes:
  - GET/POST /, /login: login
  - GET /logout: logout
  - GET /select: main selection page shell
  - GET/POST /dodaj-kameru: add camera form
    N)datetime	timedelta)	Blueprintrender_templaterequestredirecturl_forsessionflashmake_response   )get_db)login_required)is_login_allowedis_ip_allowedrecord_login_failurerecord_login_successrecord_ip_failure)secure_password_check)log_auth_eventget_request_ipviews/GETPOST
login_root)methodsendpointz/loginlogin_loginc                  C   s  dd l } tjdkrtjdd  }tjdd}ttp d}t||\}}t	|\}}|r2|snt
dd zt| W n	 tyF   Y nw z	td||d	 W n	 tyY   Y nw ttd
}tt|||jd< |dfS t }	|	d|f}
|
 }t|||rt  |d td< t|| ztd||d |d W n	 ty   Y nw ttdS zt|| W n	 ty   Y nw z	td||d	 W n	 ty   Y nw t
dd ttdS td
S )Nr   r   username passwordu/   Previše pokušaja. Pokušajte ponovno kasnije.errorlogin_rate_limited)r   ipz
login.htmlzRetry-Afteri  z-SELECT * FROM users WHERE LOWER(username) = ?iduser_idlogin_success)r   r&   r$   views.select_pagelogin_failureu&   Pogrešno korisničko ime ili lozinka.zviews.login_root)bcryptr   methodformgetstriplowerr   r   r   r
   r   	Exceptionr   r   r   strmaxheadersr   executefetchoner   r	   clearr   r   r   r   )r*   r   r!   	client_ipallowed_userretry_after_user
allowed_ipretry_after_iprespdbcuruser r@   "/var/www/html/app_modules/views.pylogin   s`   



rB   z/auth/login)r   c                   C   s   t  S )N)rB   r@   r@   r@   rA   login_post_aliasS   s   rC   z/logoutc                   C   s   t dd  tdS )Nr&   r   )r	   popr   r@   r@   r@   rA   logoutX   s   rE   z/selectc                   C   s   t dS )Nzselect.html)r   r@   r@   r@   rA   select_page^   s   rF   z/dodaj-kameruc                  C   s   t jdkrnt jdpd } t jdpd }|  r#t| dkr.tdd tt	dS |s;td	d tt	dS t
 }d
d l}z|dtd | |f |  tdd W n |jyg   tdd Y nw tt	dS tdS )Nr   	camera_idr    camera_name   u*   Broj kamere mora imati točno 12 znamenki.r"   zviews.add_camera_redesignzIme kamere je obavezno.r   zPINSERT OR IGNORE INTO cameras (user_id, camera_id, camera_name) VALUES (?, ?, ?)r&   zKamera je dodana.successu(   Dogodila se greška. Pokušajte ponovno.r(   zadd_camera.html)r   r+   r,   r-   r.   isdigitlenr
   r   r   r   sqlite3r4   r	   commitErrorr   )rG   rH   r=   rM   r@   r@   rA   add_camera_redesigne   s.   


rP   )$__doc__rer   r   flaskr   r   r   r   r   r	   r
   r   r=   r   securityr   
rate_limitr   r   r   r   r   security_enhancementsr   auditr   r   __name__bprouterB   rC   rE   rF   rP   r@   r@   r@   rA   <module>   s.    (
5

