o
    -hn(                     @   s  d dl mZmZmZ d dlmZmZ d dlmZ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 d dlmZ d d	lmZ d
dlmZmZmZ d
dlm Z m!Z!m"Z" d
dl#m$Z$ dd Z%G dd dee	eZ&G dd dee	eZ'G dd dee	eZ(G dd dee	eZ)G dd dee	eZ*G dd dee	eZ+G dd dee	eZ,G dd dee	eZ-G d d! d!ee	eZ.d"S )#    )renderredirectget_object_or_404)login_requireduser_passes_test)LoginRequiredMixinUserPassesTestMixin)messages)ListView
CreateView
UpdateView
DeleteViewView)reverse_lazy)QCount)timezone)	timedelta   )NotificationTemplateSMSLogEmailLog)NotificationTemplateFormTestSMSFormTestEmailForm)NotificationServicec                 C   s   | j o| jp| jjdd S )N
Management)name)is_authenticatedis_superusergroupsfilterexists)user r$   3/var/www/html/optinet_system/notifications/views.pyhas_admin_access   s   r&   c                   @   s   e Zd Zdd Zdd ZdS )NotificationDashboardViewc                 C      t | jjS Nr&   requestr#   selfr$   r$   r%   	test_func      z#NotificationDashboardView.test_funcc                 C   s  t j }t jjdd }tjjt  d }tjjt tdd d }d}tj	 rOtj }tjjdd	 }|dkrMt
|| d
 dnd}tjjt  d }	tjjt tdd d }
d}tj	 rtj }tjjdd	 }|dkrt
|| d
 dnd}t jdd d }tjdd d }tjdd d }||||||	|
||||d}t|d|S )NT	is_active)sent_at__date   )days)sent_at__gter   sentstatusd   r   z-updated_at   -sent_at)total_templatesactive_templates	sms_todaysms_this_weeksms_success_rateemail_todayemail_this_weekemail_success_raterecent_templates
recent_smsrecent_emailsznotifications/dashboard.html)r   objectscountr!   r   r   nowdater   r"   roundr   order_byr   )r-   r+   r<   r=   r>   r?   r@   	sms_totalsms_sentrA   rB   rC   email_total
email_sentrD   rE   rF   contextr$   r$   r%   get   s@   
 

 

zNotificationDashboardView.getN)__name__
__module____qualname__r.   rR   r$   r$   r$   r%   r'      s    r'   c                       s<   e Zd ZeZdZdZdZdd Zdd Z	 fdd	Z
  ZS )
NotificationTemplateListViewz notifications/template_list.html	templates   c                 C   r(   r)   r*   r,   r$   r$   r%   r.   I   r/   z&NotificationTemplateListView.test_funcc                 C   s   t j }| jjd}| jjd}| jjd}|r.|t|dt|dB t|dB }|r6|j|d}|dkrA|jd	d
}n
|dkrK|jdd
}|ddS )Nsearchtyper8   )name__icontains)content__icontainssubject__icontains)template_typeactiveTr0   inactiveFr_   r   )	r   rG   allr+   GETrR   r!   r   rL   )r-   querysetrY   r_   r8   r$   r$   r%   get_querysetL   s&   
z)NotificationTemplateListView.get_querysetc                    s\   t  jdi |}tj|d< | jjdd|d< | jjdd|d< | jjdd|d< |S )	Ntemplate_typesrY    rZ   selected_typer8   selected_statusr$   )superget_context_datar   TEMPLATE_TYPESr+   rc   rR   )r-   kwargsrQ   	__class__r$   r%   rk   c   s   
z-NotificationTemplateListView.get_context_data)rS   rT   rU   r   modeltemplate_namecontext_object_namepaginate_byr.   re   rk   __classcell__r$   r$   rn   r%   rV   C   s    rV   c                       8   e Zd ZeZeZdZedZ	dd Z
 fddZ  ZS )NotificationTemplateCreateView notifications/template_form.htmlnotifications:template_listc                 C   r(   r)   r*   r,   r$   r$   r%   r.   q   r/   z(NotificationTemplateCreateView.test_funcc                    &   t | jd|jj d t |S )N
Template "z" created successfully!r	   successr+   instancer   rj   
form_validr-   formrn   r$   r%   r~   t      z)NotificationTemplateCreateView.form_validrS   rT   rU   r   rp   r   
form_classrq   r   success_urlr.   r~   rt   r$   r$   rn   r%   rv   k       rv   c                       ru   )NotificationTemplateUpdateViewrw   rx   c                 C   r(   r)   r*   r,   r$   r$   r%   r.   ~   r/   z(NotificationTemplateUpdateView.test_funcc                    ry   )Nrz   z" updated successfully!r{   r   rn   r$   r%   r~      r   z)NotificationTemplateUpdateView.form_validr   r$   r$   rn   r%   r   x   r   r   c                       s4   e Zd ZeZdZedZdd Z fddZ	  Z
S )NotificationTemplateDeleteViewz*notifications/template_confirm_delete.htmlrx   c                 C   r(   r)   r*   r,   r$   r$   r%   r.      r/   z(NotificationTemplateDeleteView.test_funcc                    s8   |   }t|d|j d t j|g|R i |S )Nrz   z" deleted successfully!)
get_objectr	   r|   r   rj   delete)r-   r+   argsrm   templatern   r$   r%   r      s   z%NotificationTemplateDeleteView.delete)rS   rT   rU   r   rp   rq   r   r   r.   r   rt   r$   r$   rn   r%   r      s    r   c                   @   ,   e Zd ZeZdZdZdZdd Zdd Z	dS )	SMSLogListViewznotifications/sms_logs.htmllogs2   c                 C   r(   r)   r*   r,   r$   r$   r%   r.      r/   zSMSLogListView.test_funcc                 C   sh   t j }| jjd}| jjd}|r'|t|dt|dB t|dB }|r/|j|d}|dS )NrY   r8   )recipient_phone__icontainsrecipient_name__icontainsmessage__icontainsr7   r;   )	r   rG   rb   r+   rc   rR   r!   r   rL   )r-   rd   rY   r8   r$   r$   r%   re      s   

zSMSLogListView.get_querysetN)
rS   rT   rU   r   rp   rq   rr   rs   r.   re   r$   r$   r$   r%   r          r   c                   @   r   )	EmailLogListViewznotifications/email_logs.htmlr   r   c                 C   r(   r)   r*   r,   r$   r$   r%   r.      r/   zEmailLogListView.test_funcc                 C   s   t j }| jjd}| jjd}| jjd}|r3|t|dt|dB t|dB t|dB }|r;|j|d}|rC|j|d	}|d
S )NrY   r8   
email_type)recipient_email__icontainsr   r]   r   r7   )r   r;   )	r   rG   rb   r+   rc   rR   r!   r   rL   )r-   rd   rY   r8   r   r$   r$   r%   re      s&   

zEmailLogListView.get_querysetN)
rS   rT   rU   r   rp   rq   rr   rs   r.   re   r$   r$   r$   r%   r      r   r   c                   @   $   e Zd Zdd Zdd Zdd ZdS )TestSMSViewc                 C   r(   r)   r*   r,   r$   r$   r%   r.      r/   zTestSMSView.test_funcc                 C      t  }t|dd|iS )Nnotifications/test_sms.htmlr   )r   r   r-   r+   r   r$   r$   r%   rR         zTestSMSView.getc              
   C   s   t |j}| rP|jd }|jd }zt }|j||dd}|r+t|d|  nt|d W n t	yO } zt|dt
|  W Y d }~nd }~ww t|dd	|iS )
Nphonemessage	Test Userrecipient_namezTest SMS sent successfully to z8Failed to send test SMS. Please check your SMS settings.zError sending test SMS: r   r   )r   POSTis_validcleaned_datar   send_smsr	   r|   error	Exceptionstrr   )r-   r+   r   r   r   servicer|   er$   r$   r%   post   s    


"zTestSMSView.postNrS   rT   rU   r.   rR   r   r$   r$   r$   r%   r          r   c                   @   r   )TestEmailViewc                 C   r(   r)   r*   r,   r$   r$   r%   r.      r/   zTestEmailView.test_funcc                 C   r   )Nnotifications/test_email.htmlr   )r   r   r   r$   r$   r%   rR      r   zTestEmailView.getc           	   
   C   s   t |j}| rV|jd }|jd }|jd }z t }|j|||dd}|r1t|d|  nt|d W n t	yU } zt|dt
|  W Y d }~nd }~ww t|d	d
|iS )Nemailsubjectr   r   r   z Test email sent successfully to z<Failed to send test email. Please check your email settings.zError sending test email: r   r   )r   r   r   r   r   
send_emailr	   r|   r   r   r   r   )	r-   r+   r   r   r   r   r   r|   r   r$   r$   r%   r      s"   



"zTestEmailView.postNr   r$   r$   r$   r%   r      r   r   N)/django.shortcutsr   r   r   django.contrib.auth.decoratorsr   r   django.contrib.auth.mixinsr   r   django.contribr	   django.views.genericr
   r   r   r   r   django.urlsr   django.db.modelsr   r   django.utilsr   datetimer   modelsr   r   r   formsr   r   r   servicesr   r&   r'   rV   rv   r   r   r   r   r   r   r$   r$   r$   r%   <module>   s,   1(