U
    	h                     @   s   d Z ddlmZmZ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 dd	lmZmZmZ dd
lm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 G dd deeZ!dS )z
Web Views for Jingles App

This module provides web interface views for jingle detection management,
including template management, detection results, and ad break monitoring.
    )renderget_object_or_404redirect)ListView
DetailView
CreateView
UpdateView)Q)	Paginator)reverse_lazy)messages)LoginRequiredMixin)JingleTemplateJingleDetectionAdBreak)JingleTemplateFormc                       s8   e Zd ZdZeZdZdZdZdd Z	 fddZ
  ZS )	JingleTemplateListViewzList view for jingle templateszjingles/list.html	templates   c                 C   sX   t jddd}| jjd}|rT|t|dt|dB t|dB t|dB }|S )	Nchannel
created_byz-created_atsearch)name__icontains)description__icontains)Zcategory__icontains)channel__name__icontains)	r   objectsselect_relatedorder_byrequestGETgetfilterr	   )selfquerysetr    r$   6/var/www/html/JingleDetector/src/apps/jingles/views.pyget_queryset   s    z#JingleTemplateListView.get_querysetc                    s&   t  jf |}| jjdd|d< |S )Nr    )superget_context_datar   r   r    r"   kwargscontext	__class__r$   r%   r)   (   s    z'JingleTemplateListView.get_context_data)__name__
__module____qualname____doc__r   modeltemplate_namecontext_object_namepaginate_byr&   r)   __classcell__r$   r$   r-   r%   r      s   r   c                       s@   e Zd ZdZeZeZdZe	dZ
 fddZ fddZ  ZS )JingleTemplateCreateViewz Create view for jingle templatesjingles/form.htmljingles:template_listc                    s2   | j j|j_t| j d|jj d t |S )z&Set the created_by user before saving.Jingle template "z" created successfully!)	r   userinstancer   r   successnamer(   
form_validr"   formr-   r$   r%   r@   5   s    z#JingleTemplateCreateView.form_validc                    s   t | jd t |S zHandle form validation errors.z Please correct the errors below.r   errorr   r(   form_invalidrA   r-   r$   r%   rF   ;   s    z%JingleTemplateCreateView.form_invalidr/   r0   r1   r2   r   r3   r   
form_classr4   r   success_urlr@   rF   r7   r$   r$   r-   r%   r8   .   s   r8   c                       s@   e Zd ZdZeZeZdZe	dZ
 fddZ fddZ  ZS )JingleTemplateUpdateViewz Update view for jingle templatesr9   r:   c                    s&   t | jd|jj d t |S )z"Handle successful form submission.r;   z" updated successfully!)r   r>   r   r=   r?   r(   r@   rA   r-   r$   r%   r@   H   s    z#JingleTemplateUpdateView.form_validc                    s   t | jd t |S rC   rD   rA   r-   r$   r%   rF   M   s    z%JingleTemplateUpdateView.form_invalidrG   r$   r$   r-   r%   rJ   A   s   rJ   c                       s,   e Zd ZdZeZdZdZ fddZ  Z	S )JingleTemplateDetailViewzDetail view for jingle templatezjingles/detail.htmltemplatec                    s@   t  jf |}|  }tjj|dddd d |d< |S )N)rL   session-detection_time
   Zrecent_detections)r(   r)   
get_objectr   r   r!   r   r   )r"   r+   r,   rL   r-   r$   r%   r)   Y   s    
z)JingleTemplateDetailView.get_context_data)
r/   r0   r1   r2   r   r3   r4   r5   r)   r7   r$   r$   r-   r%   rK   S   s
   rK   c                       s8   e Zd ZdZeZdZdZdZdd Z	 fddZ
  ZS )	JingleDetectionListViewzList view for jingle detectionszjingles/detection_list.html
detectionsr   c                 C   s6   t jddd}| jjd}|r2|j|d}|S )NrL   rM   rN   )template_id)r   r   r   r   r   r   r    r!   )r"   r#   rS   r$   r$   r%   r&   l   s
    z$JingleDetectionListView.get_querysetc                    s8   t  jf |}tjjdd|d< | jjdd|d< |S )NT)	is_activer   rL   r'   Zselected_template)r(   r)   r   r   r!   r   r   r    r*   r-   r$   r%   r)   s   s    z(JingleDetectionListView.get_context_data)r/   r0   r1   r2   r   r3   r4   r5   r6   r&   r)   r7   r$   r$   r-   r%   rQ   e   s   rQ   c                   @   s   e Zd ZdZeZdZdZdS )JingleDetectionDetailViewz Detail view for jingle detectionzjingles/detection/detail.html	detectionN)r/   r0   r1   r2   r   r3   r4   r5   r$   r$   r$   r%   rU   z   s   rU   c                       s8   e Zd ZdZeZdZdZdZdd Z	 fddZ
  ZS )	AdBreakListViewzList view for ad breakszadbreaks/list.html	ad_breaksr   c                 C   s4   t jdd}| jjd}|r0|j|d}|S )NrM   z-start_time)
session_id)r   r   r   r   r   r   r    r!   )r"   r#   rY   r$   r$   r%   r&      s
    zAdBreakListView.get_querysetc                    s   t  jf |}|S )N)r(   r)   r*   r-   r$   r%   r)      s    z AdBreakListView.get_context_data)r/   r0   r1   r2   r   r3   r4   r5   r6   r&   r)   r7   r$   r$   r-   r%   rW      s   rW   c                       s,   e Zd ZdZeZdZdZ fddZ  Z	S )AdBreakDetailViewzDetail view for ad breakzadbreaks/detail.htmlad_breakc                    sD   t  jf |}|  }tjj|j|j|jgd	d
d|d< |S )N)rM   Zdetection_time__rangerL   detection_timerR   )r(   r)   rP   r   r   r!   rM   
start_timeend_timer   r   )r"   r+   r,   r[   r-   r$   r%   r)      s    
z"AdBreakDetailView.get_context_data)
r/   r0   r1   r2   r   r3   r4   r5   r)   r7   r$   r$   r-   r%   rZ      s
   rZ   N)"r2   django.shortcutsr   r   r   Zdjango.views.genericr   r   r   r   django.db.modelsr	   django.core.paginatorr
   django.urlsr   django.contribr   django.contrib.auth.mixinsr   apps.jingles.modelsr   r   r   Zapps.jingles.formsr   r   r8   rJ   rK   rQ   rU   rW   rZ   r$   r$   r$   r%   <module>   s"   
