U
    ȂhW                     @   s|  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 ddlmZmZmZmZ ddlmZmZ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(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   )NotificationChannelNotificationRuleNotificationNotificationTemplate)NotificationChannelFormNotificationTemplateFormNotificationRuleFormdispatch)namec                   @   s(   e Zd ZdZeZdZdZdZdd Z	dS )NotificationChannelListViewz#List view for notification channelsz notifications/channels/list.htmlchannels   c                 C   s@   t j d}| jjd}|r<|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   )selfquerysetr    r(   =/var/www/html/StreamProcessor/src/apps/notifications/views.pyget_queryset   s    z(NotificationChannelListView.get_querysetN)
__name__
__module____qualname____doc__r   modeltemplate_namecontext_object_namepaginate_byr*   r(   r(   r(   r)   r      s   r   c                   @   s(   e Zd ZdZeZdZdZdZdd Z	dS )NotificationListViewzList view for notificationsz$notifications/notification/list.htmlnotificationsr   c                 C   s4   t jdd}| jjd}|r0|j|d}|S )Nchannelz-scheduled_atstatusr6   )r   r   select_relatedr!   r"   r#   r$   r%   )r&   r'   r6   r(   r(   r)   r*   0   s
    z!NotificationListView.get_querysetN)
r+   r,   r-   r.   r   r/   r0   r1   r2   r*   r(   r(   r(   r)   r3   (   s   r3   c                   @   s(   e Zd ZdZeZdZdZdZdd Z	dS )NotificationRuleListViewz List view for notification rulesznotifications/rules/list.htmlrulesr   c                 C   s   t jdd}|S )Nr5   r   )r   r   r8   r!   )r&   r'   r(   r(   r)   r*   @   s    z%NotificationRuleListView.get_querysetN)
r+   r,   r-   r.   r   r/   r0   r1   r2   r*   r(   r(   r(   r)   r9   8   s   r9   c                       s4   e Zd ZdZeZeZdZe	dZ
 fddZ  ZS )NotificationChannelCreateViewz4
    Create view for new notification channels.
     notifications/channels/form.htmlnotifications:channel_listc                    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)   rE   O   s    z(NotificationChannelCreateView.form_validr+   r,   r-   r.   r   r/   r   
form_classr0   r   success_urlrE   __classcell__r(   r(   rH   r)   r;   E   s   r;   c                       s4   e Zd ZdZeZeZdZe	dZ
 fddZ  ZS )NotificationChannelUpdateViewz9
    Update view for existing notification channels.
    r<   r=   c                    s&   t | jd|jj d t |S )!Add success message after update.r>   " updated successfully!r   rC   r"   rA   r   rD   rE   rF   rH   r(   r)   rE   `   s    z(NotificationChannelUpdateView.form_validrJ   r(   r(   rH   r)   rN   V   s   rN   c                       s4   e Zd ZdZeZeZdZe	dZ
 fddZ  ZS )NotificationTemplateCreateViewz5
    Create view for new notification templates.
    -notifications/notification/template_form.htmlnotifications:template_listc                    s&   t | jd|jj d t |S )#Add success message after creation.Notification template "r?   rQ   rF   rH   r(   r)   rE   p   s    z)NotificationTemplateCreateView.form_validr+   r,   r-   r.   r   r/   r   rK   r0   r   rL   rE   rM   r(   r(   rH   r)   rR   f   s   rR   c                       s4   e Zd ZdZeZeZdZe	dZ
 fddZ  ZS )NotificationTemplateUpdateViewz:
    Update view for existing notification templates.
    rS   rT   c                    s&   t | jd|jj d t |S )rO   rV   rP   rQ   rF   rH   r(   r)   rE      s    z)NotificationTemplateUpdateView.form_validrW   r(   r(   rH   r)   rX   v   s   rX   c                       s4   e Zd ZdZeZeZdZe	dZ
 fddZ  ZS )NotificationRuleCreateViewz1
    Create view for new notification rules.
    notifications/rules/form.htmlnotifications:rule_listc                    s&   t | jd|jj d t |S )rU   Notification rule "r?   rQ   rF   rH   r(   r)   rE      s    z%NotificationRuleCreateView.form_validr+   r,   r-   r.   r   r/   r   rK   r0   r   rL   rE   rM   r(   r(   rH   r)   rY      s   rY   c                       s4   e Zd ZdZeZeZdZe	dZ
 fddZ  ZS )NotificationRuleUpdateViewz6
    Update view for existing notification rules.
    rZ   r[   c                    s&   t | jd|jj d t |S )rO   r\   rP   rQ   rF   rH   r(   r)   rE      s    z%NotificationRuleUpdateView.form_validr]   r(   r(   rH   r)   r^      s   r^   c                 C   s2   t jjdddddd }t| dd|iS )	z+HTMX endpoint for live notification updatessentr7   r5   z-sent_atN   z.notifications/partials/live_notifications.htmlr4   )r   r   r%   r8   r!   r   )r"   recent_notificationsr(   r(   r)   live_notifications   s     rb   N)*r.   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   modelsr   r   r   r   formsr   r   r   r   r3   r9   r;   rN   rR   rX   rY   r^   rb   r(   r(   r(   r)   <module>   s2   


