U
    ah                     @   s:   d Z ddlmZ ddlmZ ddlZG dd dejZdS )zForms for Notifications App

This module contains Django forms for notification management,
including forms for creating and editing notification channels.
    )forms   )NotificationChannelNc                       s`   e Zd ZdZejejdddddddZG d	d
 d
Z fddZ	dd Z
 fddZ  ZS )NotificationChannelFormz3Form for creating and editing notification channels   z<Enter JSON configuration (e.g., {"bot_token": "your_token"}))rowsplaceholderattrsz-Channel-specific configuration in JSON formatF)widget	help_textrequiredc                   @   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dej
ddddddej
dddddddZdS )zNotificationChannelForm.Metanamechannel_type	is_activeconfiguration
rate_limitretry_attemptstimeout_secondszform-controlzEnter channel name)classr   r	   r   zform-selectzform-check-inputr   i     )r   minmaxvaluer   
         i,  )r   r   r   r   r   r   N)__name__
__module____qualname__r   modelfieldsr   	TextInputSelectCheckboxInputNumberInputwidgets r'   r'   </var/www/html/JingleDetector/src/apps/notifications/forms.pyMeta   sB     r)   c                    s>   t  j|| | jd jjddi | jjs:d| jd _d S )Nr   r   zform-control font-monospacez{})	super__init__r!   r   r
   updateinstancepkinitial)selfargskwargs	__class__r'   r(   r+   :   s     z NotificationChannelForm.__init__c              
   C   s|   | j dd}| si S z$t|}t|ts:td|W S  tj	k
rv } ztdt
| W 5 d}~X Y nX dS )z)Validate that configuration is valid JSONr    z0Configuration must be a JSON object (dictionary)zInvalid JSON format: N)cleaned_datagetstripjsonloads
isinstancedictr   ValidationErrorJSONDecodeErrorstr)r0   r   Zparsed_configer'   r'   r(   clean_configurationE   s    


z+NotificationChannelForm.clean_configurationc                    sD  t   }|d}|di }|dkrV|ds>| dd |dsT| dd n|dkr|d	st| dd
 |ds| dd |ds| dd n|dkr|ds| dd n~|dkr|ds| dd n^|dkr|ds@| dd n:|dkr@|ds(| dd |ds@| dd |S )zAdditional form validationr   r   telegram	bot_tokenz8Telegram channels require a "bot_token" in configurationchat_idz6Telegram channels require a "chat_id" in configurationemail	smtp_hostz3Email channels require "smtp_host" in configurationusernamez2Email channels require "username" in configurationto_emailz2Email channels require "to_email" in configurationwebhookurlz1Webhook channels require a "url" in configurationslackZwebhook_urlz7Slack channels require a "webhook_url" in configurationdiscordz9Discord channels require a "webhook_url" in configurationsmsZapi_keyz2SMS channels require an "api_key" in configurationZphone_numberz6SMS channels require a "phone_number" in configuration)r*   cleanr7   	add_error)r0   r6   r   r   r3   r'   r(   rN   S   s<    










zNotificationChannelForm.clean)r   r   r   __doc__r   	CharFieldTextarear   r)   r+   rA   rN   __classcell__r'   r'   r3   r(   r      s   	"r   )rP   djangor   modelsr   r9   	ModelFormr   r'   r'   r'   r(   <module>   s   