U
    h                     @   sF   d Z ddlZddlmZ ddlmZ ddlmZ G dd dejZ	dS )z
Django Forms for Jingles Application

This module contains form definitions for creating and editing
jingle templates, detections, and related models.
    N)forms)slugify)JingleTemplatec                       sn   e Zd ZdZejddejdddddZG d	d
 d
Z fddZ	dd Z
 fddZd fdd	Z  ZS )JingleTemplateFormz
    Form for creating and editing jingle templates.
    
    Provides validation for jingle template configuration including
    image upload, slug generation, and threshold validation.
    Tz-Upload a reference image for jingle detectionzblock w-full text-sm text-gray-500 file:mr-4 file:py-2 file:px-4 file:rounded-full file:border-0 file:text-sm file:font-semibold file:bg-blue-50 file:text-blue-700 hover:file:bg-blue-100zimage/*)classacceptattrs)required	help_textwidgetc                	   @   s   e Zd ZeZdddddgZejdddd	ejdd
ddd	ej	ddid	ej
dddddd	ejddid	dZddddddZdS )zJingleTemplateForm.Metanamedescriptioncategorysimilarity_threshold	is_activez`mt-1 block w-full border-gray-300 rounded-md shadow-sm focus:ring-blue-500 focus:border-blue-500zEnter jingle name)r   placeholderr      z#Describe the jingle and its context)r   rowsr   r   z0.01z0.0z1.0)r   stepminmaxzAh-4 w-4 text-blue-600 focus:ring-blue-500 border-gray-300 rounded)r   r   r   r   r   z"Human-readable name for the jinglez-Description of the jingle content and contextz'Category classification for this jinglez5Detection threshold (0.0-1.0, lower = more sensitive)z"Enable this template for detectionN)__name__
__module____qualname__r   modelfieldsr   	TextInputTextareaSelectNumberInputCheckboxInputwidgets
help_texts r$   r$   7/var/www/html/StreamProcessor/src/apps/jingles/forms.pyMeta   sJ        r&   c                    s0   t  j|| | jjs,| jds,d| jd< dS )z$Initialize form with default values.r   g?N)super__init__instancepkinitialget)selfargskwargs	__class__r$   r%   r(   F   s    zJingleTemplateForm.__init__c                 C   s2   | j d}|dk	r.|dk s$|dkr.td|S )z
        Validate similarity threshold range.
        
        Returns:
            float: Validated threshold
            
        Raises:
            ValidationError: If threshold is out of range
        r   Ng        g      ?z0Similarity threshold must be between 0.0 and 1.0)cleaned_datar,   r   ValidationError)r-   	thresholdr$   r$   r%   clean_similarity_thresholdO   s
    

z-JingleTemplateForm.clean_similarity_thresholdc                    s   t   }|d}|rt| }tdd|}tdd|}|d}|rd|d  rdd| }t	|dk rd|pxd	 d
d }|}d}t
jj|d}| jjr|j| jjd}| sq| d| }|d7 }q||d< |S )zA
        Generate slug from name during form validation.
        r   z
[^a-z0-9-] z-+-r   zjingle-r   newN2      )slug)r*   r;   )r'   cleanr,   r   lowerresubstripisdigitlenr   objectsfilterr)   r*   excludeexists)r-   r2   r   	base_slugr;   counterexistingr0   r$   r%   r<   a   s,    




zJingleTemplateForm.cleanc           
         s   t  jdd}t| dr.d| jkr.| jd |_| jd}|rddl}ddlm} dd	l	m
} |jd
|j}|j||j}|||}	|	|_|r|  |S )a   
        Save the jingle template with auto-generated slug and image file handling.
        
        Args:
            commit (bool): Whether to save to database
            
        Returns:
            JingleTemplate: The saved template instance
        F)commitr2   r;   
image_filer   N)settings)default_storagejingles)r'   savehasattrr2   r;   r,   osdjango.confrL   django.core.files.storagerM   pathjoinr   
image_path)
r-   rJ   templaterK   rQ   rL   rM   Z
upload_dir	file_pathZ
saved_pathr0   r$   r%   rO      s    
zJingleTemplateForm.save)T)r   r   r   __doc__r   
ImageField	FileInputrK   r&   r(   r5   r<   rO   __classcell__r$   r$   r0   r%   r      s   	'	*r   )
rY   r>   djangor   django.utils.textr   apps.jingles.modelsr   	ModelFormr   r$   r$   r$   r%   <module>   s
   