a
    ;Xh"                     @   s   d dl mZmZ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 dlmZ d d	lmZ d d
lmZmZmZ d dlmZ d dlmZ d dlmZ dd Zedd Zedd Z dd Z!dd Z"edd Z#dd Z$dS )    )renderredirectget_object_or_404)JsonResponse)Notification)get_user_info)NotificationForm)	Paginator)reverse)User)transaction)send_notification_to_allsend_notification_to_userdelete_sent_notification)login_required)defaultdict)Qc                 C   s&   | j rtjj| dddS tj S )NF)useris_read-created_at)is_authenticatedr   objectsfilterorder_bynoner    r   </var/www/viveiro_mudafortebrasil/apps/notifications/views.pyget_notifications   s    r   c              
   C   s@  | j dkr0t| j}| r|jd }|jd }|jd }|jd }|jd }zt ` |rrt| j|| n<|r|rt	| j||| n"t
ddd	d
dW  d    W S W d    n1 s0    Y  t
dtddW S  ty } z"t
dt|d	ddW  Y d }~S d }~0 0 t
d|jd	d
dS t
ddiddS )NPOSTmessagenotification_typesend_to_allchoose_user	recipientFu   Selecione uma opção válida)successerrorsi  statusTnotification_list)r%   redirect_urli  errorzInvalid method  )methodr   r   is_validcleaned_datar   atomicr   r   r   r   r
   	Exceptionstrr&   )requestformr    r!   r"   r#   r$   er   r   r   create_notification   s(    







@,r6   c                 C   sH   | j dkr8t|| j}|r&tddiS tdddddS td	d
iddS )Nr   r%   TFu0   Notificação não encontrada ou não autorizada)r%   r+   i  r'   r+   u   Método inválidor,   )r-   r   r   r   )r3   notification_idr%   r   r   r   delete_sent_notification_view.   s    
r8   c                 C   sB   | j jr2tt|| j d}d|_|  tddiS tddiddS )N)idr   Tr%   r+   Unauthorizedi  r'   )r   r   r   r   r   saver   )r3   r7   notificationr   r   r   mark_as_read8   s    r=   c                 C   s   | j jstdS | jdd}| jdd}tjj| j dd}tjj| j dd}|rt|j|d}|j|d}|r|j|d	}|j|d	}t	|d
}t	|d
}| jd}| jd}|
|}	|
|}
t| j j|	|
||t d}t| d|S )Nindexq typer   r   senderZmessage__icontainsr!   
   Zpage_receivedZ	page_sent)	user_infopage_obj_receivedpage_obj_sentqueryfilter_typeZnotification_form$notifications/notification_list.html)r   r   r   GETgetr   r   r   r   r	   get_pager   r9   r   r   )r3   rJ   rK   Zreceived_notificationssent_notificationsZpaginator_receivedZpaginator_sentZpage_number_receivedZpage_number_sentrH   rI   contextr   r   r   r)   B   s4    




r)   c                 C   s   | j jstdS | jdd}| jdd}tjj| j dd}|rR|j|d}|rb|j|d}g }t	 }|D ]6}|j
r|j|vr|| ||j qp|| qpt|d	}| jd
}||}	t| j j|	||d}
t| d|
S )Nr>   r?   r@   rA   rB   r   rD   rE   rF   page)rG   page_objrJ   rK   z)notifications/sent_notification_list.html)r   r   r   rM   rN   r   r   r   r   setr"   r    appendaddr	   rO   r   r9   r   )r3   rJ   rK   r)   Zgrouped_notificationsZseen_messagesr<   	paginatorpage_numberrS   rQ   r   r   r   sent_notifications_listg   s4    




rY   c                 C   sT   | j jstdS tt|d}| jdkr6|  tdS t| j j|d}t	| d|S )Nr>   )r9   r   r)   )rG   r<   rL   )
r   r   r   r   r   r-   deleter   r9   r   )r3   r7   r<   rQ   r   r   r   delete_notification   s    

r[   N)%django.shortcutsr   r   r   django.httpr   Zapps.notifications.modelsr   apps.dashboard.utilsr   Zapps.notifications.formsr   django.core.paginatorr	   django.urlsr
   django.contrib.auth.modelsr   	django.dbr   Zapps.notifications.servicesr   r   r   django.contrib.auth.decoratorsr   collectionsr   django.db.modelsr   r   r6   r8   r=   r)   rY   r[   r   r   r   r   <module>   s,   

	
%
*