U
    G"h"/                     @   s~   d Z ddlmZ ddlmZ ddlmZ ddlZddlm	Z	m
Z
mZ G dd	 d	ejZG d
d dejZG dd dejZdS )zForms for Notifications App

This module provides Django forms for notification management,
including channels, templates, and rules.
    )forms)ValidationError)UserN   )NotificationChannelNotificationTemplateNotificationRulec                       sf   e Zd ZdZG dd dZ fddZdd Zdd	 Zd
d Zdd Z	dd Z
dd Zdd Z  ZS )NotificationChannelFormz
    Form for creating and editing notification channels.
    
    Provides validation for channel configuration including
    type-specific validation and JSON configuration handling.
    c                   @   s   e Zd ZeZdddddddgZejdd	d
dejddidej	ddidej
dddddejdddddejdddddejddddddZddddddddZd S )!zNotificationChannelForm.Metanamechannel_type	is_activeconfiguration
rate_limitretry_attemptstimeout_secondsfmt-1 block w-full border-gray-300 rounded-md shadow-sm focus:ring-primary-500 focus:border-primary-500zEnter channel nameclassplaceholderattrsr   Gh-4 w-4 text-primary-600 focus:ring-primary-500 border-gray-300 rounded   zEnter configuration as JSONr   rowsr   r   i  )r   minmaxr   
      i,  )r
   r   r   r   r   r   r   z(Unique name for the notification channelz'Select the type of notification channelz(Whether this channel is currently activez-Channel-specific configuration in JSON formatz$Maximum messages per minute (1-1000)z5Number of retry attempts for failed deliveries (0-10)z0Timeout for delivery attempts in seconds (5-300)N)__name__
__module____qualname__r   modelfieldsr   	TextInputSelectCheckboxInputTextareaNumberInputwidgets
help_texts r+   r+   =/var/www/html/StreamProcessor/src/apps/notifications/forms.pyMeta   sd          "r-   c                    sB   t  j|| | jr>| jjr>| jjr>tj| jjdd| jd _d S )N   )indentr   )	super__init__instancepkr   jsondumpsr#   initialselfargskwargs	__class__r+   r,   r1   I   s     z NotificationChannelForm.__init__c              
   C   s   | j d}|stdzt|}W n2 tjk
rX } ztd| W 5 d}~X Y nX | j d}|dkrz| | nb|dkr| | nN|dkr| | n:|d	kr| 	| n&|d
kr| 
| n|dkr| | |S )z&Validate and parse JSON configuration.r   zConfiguration is requiredInvalid JSON format: Nr   telegramemailwebhookslackdiscordsms)cleaned_datagetr   r4   loadsJSONDecodeError_validate_telegram_config_validate_email_config_validate_webhook_config_validate_slack_config_validate_discord_config_validate_sms_config)r8   r   Zconfig_dicter   r+   r+   r,   clean_configurationR   s*     
z+NotificationChannelForm.clean_configurationc                 C   s0   ddg}|D ]}| |std| dqdS )z(Validate Telegram channel configuration.	bot_tokenchat_idzTelegram channels require "" in configurationNrE   r   r8   configrequired_fieldsfieldr+   r+   r,   rH   p   s    
z1NotificationChannelForm._validate_telegram_configc                 C   sD   dg}|D ]}| |s
td| dq
t|d ts@tddS )z%Validate email channel configuration.
recipientszEmail channels require "rR   z2Email recipients must be a list of email addressesNrE   r   
isinstancelistrT   r+   r+   r,   rI   w   s    
z.NotificationChannelForm._validate_email_configc                 C   s.   dg}|D ]}| |s
td| dq
dS )z'Validate webhook channel configuration.urlzWebhook channels require "rR   NrS   rT   r+   r+   r,   rJ      s    
z0NotificationChannelForm._validate_webhook_configc                 C   s.   dg}|D ]}| |s
td| dq
dS )z%Validate Slack channel configuration.webhook_urlzSlack channels require "rR   NrS   rT   r+   r+   r,   rK      s    
z.NotificationChannelForm._validate_slack_configc                 C   s.   dg}|D ]}| |s
td| dq
dS )z'Validate Discord channel configuration.r]   zDiscord channels require "rR   NrS   rT   r+   r+   r,   rL      s    
z0NotificationChannelForm._validate_discord_configc                 C   sF   ddg}|D ]}| |std| dqt|d tsBtddS )z#Validate SMS channel configuration.Zapi_keyZphone_numberszSMS channels require "rR   z1SMS phone_numbers must be a list of phone numbersNrY   rT   r+   r+   r,   rM      s    
z,NotificationChannelForm._validate_sms_config)r   r    r!   __doc__r-   r1   rO   rH   rI   rJ   rK   rL   rM   __classcell__r+   r+   r;   r,   r	      s   2	r	   c                   @   s&   e Zd ZdZG dd dZdd ZdS )NotificationTemplateFormz?
    Form for creating and editing notification templates.
    c                	   @   s   e Zd ZeZdddddddgZejdd	d
dejddd
dejddd
dejdddddejddddde	 ej
ddiddZdS )zNotificationTemplateForm.Metar
   template_typesubject_templatemessage_template	variableschannel_typesr   r   zEnter template namer   r   #e.g., jingle_detected, stream_errorz6Subject template with variables like {{variable_name}}   z6Message template with variables like {{variable_name}}r      z8JSON array of variable names: ["variable1", "variable2"]r   r   )r
   ra   rb   rc   rd   re   r   N)r   r    r!   r   r"   r#   r   r$   r'   CheckboxSelectMultipler&   r)   r+   r+   r+   r,   r-      sJ         r-   c              
   C   sn   | j d}|sg S z"t|}t|ts2td|W S  tjk
rh } ztd| W 5 d}~X Y nX dS )z"Validate and parse variables JSON.rd   zVariables must be a JSON arrayr=   N)rD   rE   r4   rF   rZ   r[   r   rG   )r8   rd   Zvariables_listrN   r+   r+   r,   clean_variables   s    

z(NotificationTemplateForm.clean_variablesN)r   r    r!   r^   r-   rj   r+   r+   r+   r,   r`      s   $r`   c                       s6   e Zd ZdZG dd dZ fddZdd Z  ZS )NotificationRuleFormz;
    Form for creating and editing notification rules.
    c                	   @   s   e Zd ZeZdddddddgZejdd	d
dejddd
dejddidejddidej	dddddejddidej
ddiddZdS )zNotificationRuleForm.Metar
   
event_typechanneltemplate
conditionsr   priorityr   zEnter rule namer   r   rf   r   rh   z#JSON conditions for rule triggeringr   r   )r
   rl   rm   rn   ro   rp   r   N)r   r    r!   r   r"   r#   r   r$   r%   r'   r&   r)   r+   r+   r+   r,   r-      sN            r-   c                    s>   t  j|| tjjdd| jd _tjjdd| jd _d S )NT)r   rm   rn   )r0   r1   r   objectsfilterr#   querysetr   r7   r;   r+   r,   r1     s    zNotificationRuleForm.__init__c              
   C   s\   | j d}|si S zt|}|W S  tjk
rV } ztd| W 5 d}~X Y nX dS )z#Validate and parse conditions JSON.ro   r=   N)rD   rE   r4   rF   rG   r   )r8   ro   Zconditions_dictrN   r+   r+   r,   clean_conditions  s    
z%NotificationRuleForm.clean_conditions)r   r    r!   r^   r-   r1   rt   r_   r+   r+   r;   r,   rk      s   #rk   )r^   djangor   django.core.exceptionsr   django.contrib.auth.modelsr   r4   modelsr   r   r   	ModelFormr	   r`   rk   r+   r+   r+   r,   <module>   s    9