U
    ch#                     @   sb  d 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 ddlmZ ddlmZ dd	lmZ dd
lmZ e	eddG dd deZe	eddG dd deZe	eddG dd deZG dd deeZG dd deeZG dd deeZG dd deeZG dd deeZG dd deeZG dd  d eeZ ed!d" Z!d#S )$zWeb Views for Notifications App

This module provides web interface views for notification management,
including channels, rules, and notification history.
    )renderget_object_or_404)login_required)LoginRequiredMixin)method_decorator)ListView
DetailView
CreateView
UpdateView)JsonResponse)Q)reverse_lazy)messagesdispatch)namec                   @   s8   e Zd ZdZdZdZdZdd Zedd Z	d	d
 Z
dS )NotificationChannelListViewz#List view for notification channelsz notifications/channels/list.htmlchannels   c                 C   s   ddl m} |S N   )NotificationChannelmodelsr   selfr    r   </var/www/html/JingleDetector/src/apps/notifications/views.py	get_model   s    z%NotificationChannelListView.get_modelc                 C   s   |   S Nr   r   r   r   r   model!   s    z!NotificationChannelListView.modelc                 C   sH   |   }|j d}| jjd}|rD|t|dt|dB }|S )N-created_atsearch)name__icontains)Zchannel_type__icontains)	r   objectsallorder_byrequestGETgetfilterr   )r   r   querysetr#   r   r   r   get_queryset%   s    z(NotificationChannelListView.get_querysetN__name__
__module____qualname____doc__template_namecontext_object_namepaginate_byr   propertyr!   r-   r   r   r   r   r      s   
r   c                   @   s8   e Zd ZdZdZdZdZdd Zedd Z	d	d
 Z
dS )NotificationListViewzList view for notificationsz$notifications/notification/list.htmlnotificationsr   c                 C   s   ddl m} |S Nr   Notificationr   r;   r   r;   r   r   r   r   8   s    zNotificationListView.get_modelc                 C   s   |   S r   r   r    r   r   r   r!   <   s    zNotificationListView.modelc                 C   s<   |   }|jdd}| jjd}|r8|j|d}|S )Nchannelz-scheduled_atstatusr?   )r   r%   select_relatedr'   r(   r)   r*   r+   )r   r;   r,   r?   r   r   r   r-   @   s    z!NotificationListView.get_querysetNr.   r   r   r   r   r7   1   s   
r7   c                   @   s8   e Zd ZdZdZdZdZdd Zedd Z	d	d
 Z
dS )NotificationRuleListViewz List view for notification rulesznotifications/rules/list.htmlrulesr   c                 C   s   ddl m} |S Nr   )NotificationRuler   rE   r   rE   r   r   r   r   P   s    z"NotificationRuleListView.get_modelc                 C   s   |   S r   r   r    r   r   r   r!   T   s    zNotificationRuleListView.modelc                 C   s   |   }|jdd}|S )Nr>   r"   )r   r%   rA   r'   )r   rE   r,   r   r   r   r-   X   s    z%NotificationRuleListView.get_querysetNr.   r   r   r   r   rB   I   s   
rB   c                       sH   e Zd ZdZdZedZdd Zedd Z	dd	 Z
 fd
dZ  ZS )NotificationChannelCreateViewz4
    Create view for new notification channels.
     notifications/channels/form.htmlnotifications:channel_listc                 C   s   ddl m} |S r   r   r   r   r   r   r   f   s    z'NotificationChannelCreateView.get_modelc                 C   s   |   S r   r   r    r   r   r   r!   j   s    z#NotificationChannelCreateView.modelc                 C   s   ddl m} |S Nr   )NotificationChannelFormformsrL   r   rL   r   r   r   get_form_classn   s    z,NotificationChannelCreateView.get_form_classc                    s2   | j j|j_t| j d|jj d t |S )z,Set the current user as the channel creator.Notification channel "" created successfully!)	r(   userinstance
created_byr   successr   super
form_validr   form	__class__r   r   rX   r   s    z(NotificationChannelCreateView.form_validr/   r0   r1   r2   r3   r   success_urlr   r6   r!   rP   rX   __classcell__r   r   r[   r   rH   ^   s   
rH   c                       sH   e Zd ZdZdZedZdd Zedd Z	dd	 Z
 fd
dZ  ZS )NotificationChannelUpdateViewz9
    Update view for existing notification channels.
    rI   rJ   c                 C   s   ddl m} |S r   r   r   r   r   r   r      s    z'NotificationChannelUpdateView.get_modelc                 C   s   |   S r   r   r    r   r   r   r!      s    z#NotificationChannelUpdateView.modelc                 C   s   ddl m} |S rK   rM   rO   r   r   r   rP      s    z,NotificationChannelUpdateView.get_form_classc                    s&   t | jd|jj d t |S )!Add success message after update.rQ   " updated successfully!r   rV   r(   rT   r   rW   rX   rY   r[   r   r   rX      s    z(NotificationChannelUpdateView.form_validr]   r   r   r[   r   r`   y   s   
r`   c                       sH   e Zd ZdZdZedZdd Zedd Z	dd	 Z
 fd
dZ  ZS )NotificationTemplateCreateViewz5
    Create view for new notification templates.
    -notifications/notification/template_form.htmlnotifications:template_listc                 C   s   ddl m} |S Nr   )NotificationTemplater   rh   r   rh   r   r   r   r      s    z(NotificationTemplateCreateView.get_modelc                 C   s   |   S r   r   r    r   r   r   r!      s    z$NotificationTemplateCreateView.modelc                 C   s   ddl m} |S Nr   )NotificationTemplateFormrN   rl   r   rl   r   r   r   rP      s    z-NotificationTemplateCreateView.get_form_classc                    s&   t | jd|jj d t |S )#Add success message after creation.Notification template "rR   rc   rY   r[   r   r   rX      s    z)NotificationTemplateCreateView.form_validr]   r   r   r[   r   rd      s   
rd   c                       sH   e Zd ZdZdZedZdd Zedd Z	dd	 Z
 fd
dZ  ZS )NotificationTemplateUpdateViewz:
    Update view for existing notification templates.
    re   rf   c                 C   s   ddl m} |S rg   ri   rj   r   r   r   r      s    z(NotificationTemplateUpdateView.get_modelc                 C   s   |   S r   r   r    r   r   r   r!      s    z$NotificationTemplateUpdateView.modelc                 C   s   ddl m} |S rk   rm   rn   r   r   r   rP      s    z-NotificationTemplateUpdateView.get_form_classc                    s&   t | jd|jj d t |S )ra   rp   rb   rc   rY   r[   r   r   rX      s    z)NotificationTemplateUpdateView.form_validr]   r   r   r[   r   rq      s   
rq   c                       sH   e Zd ZdZdZedZdd Zedd Z	dd	 Z
 fd
dZ  ZS )NotificationRuleCreateViewz1
    Create view for new notification rules.
    notifications/rules/form.htmlnotifications:rule_listc                 C   s   ddl m} |S rD   rF   rG   r   r   r   r      s    z$NotificationRuleCreateView.get_modelc                 C   s   |   S r   r   r    r   r   r   r!      s    z NotificationRuleCreateView.modelc                 C   s   ddl m} |S Nr   )NotificationRuleFormrN   rv   r   rv   r   r   r   rP      s    z)NotificationRuleCreateView.get_form_classc                    s&   t | jd|jj d t |S )ro   Notification rule "rR   rc   rY   r[   r   r   rX      s    z%NotificationRuleCreateView.form_validr]   r   r   r[   r   rr      s   
rr   c                       sH   e Zd ZdZdZedZdd Zedd Z	dd	 Z
 fd
dZ  ZS )NotificationRuleUpdateViewz6
    Update view for existing notification rules.
    rs   rt   c                 C   s   ddl m} |S rD   rF   rG   r   r   r   r      s    z$NotificationRuleUpdateView.get_modelc                 C   s   |   S r   r   r    r   r   r   r!      s    z NotificationRuleUpdateView.modelc                 C   s   ddl m} |S ru   rw   rx   r   r   r   rP      s    z)NotificationRuleUpdateView.get_form_classc                    s&   t | jd|jj d t |S )ra   ry   rb   rc   rY   r[   r   r   rX      s    z%NotificationRuleUpdateView.form_validr]   r   r   r[   r   rz      s   
rz   c                       sH   e Zd ZdZdZedZdd Zedd Z	dd	 Z
 fd
dZ  ZS )NotificationCreateViewz;
    Create view for sending individual notifications.
    z$notifications/notification/form.htmlznotifications:notification_listc                 C   s   ddl m} |S r9   r<   r=   r   r   r   r     s    z NotificationCreateView.get_modelc                 C   s   |   S r   r   r    r   r   r   r!     s    zNotificationCreateView.modelc                 C   s   ddl m} |S )Nr   )NotificationForm)rN   r|   )r   r|   r   r   r   rP     s    z%NotificationCreateView.get_form_classc                    s   t | jd t |S )ro   zNotification sent successfully!)r   rV   r(   rW   rX   rY   r[   r   r   rX     s    z!NotificationCreateView.form_validr]   r   r   r[   r   r{      s   
r{   c                 C   s>   ddl m} |jjdddddd }t| d	d
|iS )z+HTMX endpoint for live notification updatesr   r:   sentr@   r>   z-sent_atN   z.notifications/partials/live_notifications.htmlr8   )r   r;   r%   r+   rA   r'   r   )r(   r;   recent_notificationsr   r   r   live_notifications  s     r   N)"r2   django.shortcutsr   r   django.contrib.auth.decoratorsr   django.contrib.auth.mixinsr   django.utils.decoratorsr   Zdjango.views.genericr   r   r	   r
   django.httpr   django.db.modelsr   django.urlsr   django.contribr   r   r7   rB   rH   r`   rd   rq   rr   rz   r{   r   r   r   r   r   <module>   s0   


