o
     e                     @   s   d dl Z d dlmZ d dlmZ d dlmZmZ d dlm	Z	 d dl
m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 e eZG dd deZG dd deeZdS )    N)UserPassesTestMixinstatic)HttpResponseJsonResponse)render_to_string)gettext)TemplateViewView)method_decorator)xframe_options_sameorigin)
UploadForm)get_attachment_model
get_confighas_codemirror_configc                       s@   e Zd ZdZ fddZee fddZ fddZ  Z	S )SummernoteEditorz+django_summernote/widget_iframe_editor.htmlc                    s   t    t }tdd |d D }tdd |d D }|d t r(|d nd | |d	  | _|d
 t r<|d nd | |d  | _|| _d S )Nc                 s       | ]}t |V  qd S Nr   .0x r   J/var/www/html/venv/lib/python3.10/site-packages/django_summernote/views.py	<genexpr>       z,SummernoteEditor.__init__.<locals>.<genexpr>default_cssc                 s   r   r   r   r   r   r   r   r      r   
default_jsbase_csscodemirror_cssr   cssbase_jscodemirror_jsjs)super__init__r   tupler   r   r"   config)selfr&   static_default_cssstatic_default_js	__class__r   r   r$      s*   

zSummernoteEditor.__init__c                       t t| j|i |S r   )r#   r   dispatchr'   argskwargsr*   r   r   r-   )      zSummernoteEditor.dispatchc                    sX   t  jdi |}| jd |d< | jd dd|d< | j|d< | j|d< t |d< |S )	Nid-_id_safer   r"   r&   r   )r#   get_context_datar0   replacer   r"   r   )r'   r0   contextr*   r   r   r6   -   s   


z!SummernoteEditor.get_context_data)
__name__
__module____qualname__template_namer$   r   r   r-   r6   __classcell__r   r   r*   r   r      s    r   c                       sH   e Zd Zdd Z fddZee fddZdd Zd	d
 Z	  Z
S )SummernoteUploadAttachmentc                 C   s   t  d | jS )Ntest_func_upload_view)r   requestr'   r   r   r   	test_func:   s   z$SummernoteUploadAttachment.test_funcc                    s   t    t | _d S r   )r#   r$   r   r&   rA   r*   r   r   r$   =   s   
z#SummernoteUploadAttachment.__init__c                    r,   r   )r#   r>   r-   r.   r*   r   r   r-   A   r1   z#SummernoteUploadAttachment.dispatchc                 O   s   t dtddddS )NfalsezOnly POST method is allowedstatusmessage  rE   )r   r4   )r'   r@   r/   r0   r   r   r   getE   s   zSummernoteUploadAttachment.getc           
      O   s  |j j}| jd r!tdt|j dd |j  tdtddddS | jd	 r3|s3tdtd
dddS |j	dsDtdtddddS |j
 }|dd  |j	dD ]+}td|id}| stdt|j dd |j  tdd|jd ddd  S qUzTg }|j	dD ]?}t }| }	||	_|j| jd krtdtdddd  W S |	jdi | |	jj|	_| jd r||	j|	_||	 qttdd|iddW S  ty   tdtdddd Y S w )Ndisable_attachmentz4User<%s:%s> tried to use disabled attachment module.pkrC   zAttachment module is disabledrD   i  rH   !attachment_require_authenticationz$Only authenticated users are allowedfileszNo files were requestedrG   csrfmiddlewaretokenfile)rM   z+User<%s:%s> tried to upload non-image file. attachment_filesize_limitz7File size exceeds the limit allowed and cannot be savedattachment_absolute_uriz(django_summernote/upload_attachment.jsonattachmentszapplication/json)content_typezFailed to save attachmenti  r   )useris_authenticatedr&   loggererrorgetattrr   r4   FILESgetlistPOSTcopypopr   is_validjoinerrorsr   rO   sizesaveurlbuild_absolute_uriappendr   r   IOError)
r'   r@   r/   r0   authenticatedrO   formrS   klass
attachmentr   r   r   postK   s   






zSummernoteUploadAttachment.post)r9   r:   r;   rB   r$   r   r   r-   rI   rl   r=   r   r   r*   r   r>   9   s    r>   )loggingdjango.contrib.auth.mixinsr   django.templatetags.staticr   django.httpr   r   django.template.loaderr   django.utils.translationr   r4   django.views.genericr	   r
   django.utils.decoratorsr   $django.views.decorators.clickjackingr   django_summernote.formsr   django_summernote.utilsr   r   r   	getLoggerr9   rW   r   r>   r   r   r   r   <module>   s    
'