U
    {hG4                     @   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 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 G dd deeZG dd deZG dd deZG dd deZG dd deZdS )    N)ContentType)PermissionDenied)HttpResponseBadRequestJsonResponse)get_object_or_404)render_to_string)reverse)method_decorator)vary_on_headers)TemplateViewView)PermissionCheckedMixin)UploadedFilec                       st   e Zd ZdZdZeed 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 fddZ  ZS )AddViewadd3wagtailadmin/generic/multiple_upload/edit_form.htmlzX-Requested-Withc                    s   |   | _t |S N)	get_modelmodelsuperdispatch)selfrequest	__class__ s/var/www/dating/data/www/fatepal.com/env/lib/python3.8/site-packages/wagtail/admin/views/generic/multiple_upload.pyr   )   s    
zAddView.dispatchc                 C   s   |  S r   saver   formr   r   r   save_object/   s    zAddView.save_objectc                 C   s^   |   }| j| jdt| j| jjfddt| j| jjfdd|| jd| j| jjf | jj	diS )z
        Return the context data necessary for rendering the HTML form for editing
        an object that has been successfully uploaded
        edit_actionargsdelete_actionr    %s-%dinstanceprefixuser)
get_edit_form_classcontext_object_nameobjectr   edit_object_url_namepkdelete_object_url_nameedit_object_form_prefixr   r*   r   Zedit_form_classr   r   r   !get_edit_object_form_context_data2   s"         z)AddView.get_edit_object_form_context_datac              
   C   s(   dd| j | jjdt| j|  | jdiS )za
        Return the JSON response data for an object that has been successfully uploaded
        successTr    r   )context_object_id_namer-   r/   r   edit_form_template_namer3   r   r   r   r   r   get_edit_object_response_dataE   s       z%AddView.get_edit_object_response_datac                 C   s^   |   }| j| jdt| j| jjfddt| j| jjfdd|| jd| j| jjf | j	j
diS )z
        Return the context data necessary for rendering the HTML form for supplying the
        metadata to turn an upload object into a final object
        r"   r#   r%   r    r&   r'   )r+   context_upload_nameupload_objectr   edit_upload_url_nameiddelete_upload_url_namer-   edit_upload_form_prefixr   r*   r2   r   r   r   !get_edit_upload_form_context_dataS   s(          z)AddView.get_edit_upload_form_context_datac              
   C   s(   dd| j | jjdt| j|  | jdiS )z
        Return the JSON response data for an object that has been uploaded to an
        upload object and now needs extra metadata to become a final object
        r4   Tr    r5   )Zcontext_upload_id_namer;   r=   r   r7   r@   r   r8   r   r   r   get_edit_upload_response_datah   s       z%AddView.get_edit_upload_response_datac                 C   s   dd |jd dS )zN
        Return the JSON response data for an invalid form submission
        F
file)r4   error_message)joinerrorsr   r   r   r   get_invalid_response_dataw   s    z!AddView.get_invalid_response_datac                 C   s   |j stdS |  }||jd|j d j|jddd|j d i|jd}| rp| || _	t
|  S d|jkrt
| |S tjjtj|  | jj d | jjd| _| j| jj d j| jjdd	| _	t
|  S d S )
NzMust upload a filetitlezfiles[]
collection)rH   rI   rC   r*   )for_content_typerC   uploaded_by_user)rH   collection_id)FILESr   get_upload_form_classPOSTgetnamer*   is_validr!   r-   r   r9   rF   rG   r   objectscreater   get_for_modelr   r   r;   r   rA   )r   r   upload_form_classr    r   r   r   post   s6    
 

zAddView.postc                    s|   t  jf |}|  }|| jjd| _| jjd}| j	| jjd}t
|dk rVd }|| jjd j|| jj|d |S )NrJ   rM   r      rC   )	help_textcollectionsZ
form_mediaselected_collection_id)r   get_context_datarO   r   r*   r    GETrQ   permission_policyZ#collections_user_has_permission_forlenupdatefieldsrZ   Zmedia)r   kwargscontextrW   r\   r[   r   r   r   r]      s$     	zAddView.get_context_data)__name__
__module____qualname__Zpermission_requiredr7   r	   r
   r   r!   r3   r9   r@   rA   rG   rX   r]   __classcell__r   r   r   r   r      s   
	*r   c                   @   s&   e Zd ZdgZdZdd Zdd ZdS )EditViewrX   r   c                 C   s   |   d S r   r   r   r   r   r   r!      s    zEditView.save_objectc                 O   s   || j  }|  | _|  | _t| j|d| _| j|j	d| jsFt
| j|j|j| jd| j|f |j	d}| r| | tdd| j| jjiS tdd| j| jjdt| j| j| jd	t| j|fd
dt| j|fd
d|i|diS d S )Nr/   Zchanger&   r'   r4   TFr    r"   r#   r%   r5   )pk_url_kwargr   r   r+   
form_classr   r-   r_    user_has_permission_for_instancer*   r   rP   rN   r1   rS   r!   r   r6   r/   r   r7   r,   r   r.   r0   )r   r   r$   rc   	object_idr    r   r   r   rX      sj    


  
           
zEditView.postNre   rf   rg   http_method_namesr7   r!   rX   r   r   r   r   ri      s   ri   c                   @   s   e Zd ZdgZdd ZdS )
DeleteViewrX   c                 O   s`   || j  }|  | _t| j|d| _| jj}| j|jd| jsDt	| j
  tdd| j|iS )Nrj   deleter4   T)rk   r   r   r   r-   r/   r_   rm   r*   r   rr   r   r6   )r   r   r$   rc   rn   r   r   r   rX     s&    

  
  zDeleteView.postNre   rf   rg   rp   rX   r   r   r   r   rq     s   rq   c                   @   s&   e Zd ZdgZdZdd Zdd ZdS )CreateFromUploadViewrX   r   c                 C   s@   | j jjtj| jjj| jjjdd | jj	| j _
|  d S )NFr   )r-   rC   r   ospathbasenameuploadrR   r   r*   rL   r   r   r   r   r!   9  s      z CreateFromUploadView.save_objectc                 O   s
  || j  }|  | _|  | _tt|tj	| jd| _
| j
j|jkrJt|  | _| j|j|j| jd| j|f |jd}| r| | | j
j  | j
  tdd| j| jjiS tdt| j| j| j
dt| j| j
jfdd	t| j| j
jfdd
|i|ddS d S )Nr=   rK   r&   r'   r4   TFr"   r#   r%   r    r5   )r4   r    )upload_pk_url_kwargr   r   r+   rl   r   r   r   rT   rV   rx   rL   r*   r   r-   rP   rN   r?   rS   r!   rC   rr   r   r6   r=   r   r7   r:   r   r<   r>   )r   r   r$   rc   	upload_idr    r   r   r   rX   @  sh    





        
zCreateFromUploadView.postNro   r   r   r   r   rt   +  s   rt   c                   @   s   e Zd ZdgZdd ZdS )DeleteUploadViewrX   c                 O   sR   || j  }tt|tj|  d}|j|jkr4t	|j
  |  tddiS )Nry   r4   T)rz   r   r   r   rT   rV   r   rL   r*   r   rC   rr   r   )r   r   r$   rc   r{   rx   r   r   r   rX   ~  s    

 zDeleteUploadView.postNrs   r   r   r   r   r|   x  s   r|   )os.pathru   Z"django.contrib.contenttypes.modelsr   Zdjango.core.exceptionsr   Zdjango.httpr   r   Zdjango.shortcutsr   Zdjango.template.loaderr   Zdjango.urlsr   Zdjango.utils.decoratorsr	   Zdjango.views.decorators.varyr
   Zdjango.views.generic.baser   r   Zwagtail.admin.views.genericr   Zwagtail.modelsr   r   ri   rq   rt   r|   r   r   r   r   <module>   s"    6FM