U
    [¨+d&  ã                   @   sF  d dl 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mZ d dlmZmZmZ G dd	„ d	e
ƒZG d
d„ deeƒZG dd„ deƒZG dd„ deeƒZG dd„ deeƒZG dd„ deeƒZG dd„ deeƒZG dd„ deeƒZG dd„ deeƒZG dd„ dƒZG dd„ deƒZG dd„ deeeƒZG d d!„ d!eeƒZdS )"é    N)ÚImproperlyConfigured)ÚForm)Úmodels)ÚHttpResponseRedirect)ÚContextMixinÚTemplateResponseMixinÚView)ÚBaseDetailViewÚSingleObjectMixinÚ!SingleObjectTemplateResponseMixinc                       sr   e Zd ZdZi ZdZdZdZdd„ Zdd„ Z	dd„ Z
dd	d
„Zdd„ Zdd„ Zdd„ Zdd„ Z‡ fdd„Z‡  ZS )Ú	FormMixinz5Provide a way to show and handle a form in a request.Nc                 C   s
   | j  ¡ S )z6Return the initial data to use for forms on this view.)ÚinitialÚcopy©Úself© r   ú=/tmp/pip-unpacked-wheel-n7e__lmp/django/views/generic/edit.pyÚget_initial   s    zFormMixin.get_initialc                 C   s   | j S )z#Return the prefix to use for forms.)Úprefixr   r   r   r   Ú
get_prefix   s    zFormMixin.get_prefixc                 C   s   | j S )zReturn the form class to use.)Ú
form_classr   r   r   r   Úget_form_class   s    zFormMixin.get_form_classc                 C   s   |dkr|   ¡ }|f |  ¡ ŽS )z7Return an instance of the form to be used in this view.N)r   Úget_form_kwargs)r   r   r   r   r   Úget_form#   s    zFormMixin.get_formc                 C   s:   |   ¡ |  ¡ dœ}| jjdkr6| | jj| jjdœ¡ |S )ú8Return the keyword arguments for instantiating the form.)r   r   )ÚPOSTÚPUT)ÚdataÚfiles)r   r   ÚrequestÚmethodÚupdater   ZFILES©r   Úkwargsr   r   r   r   )   s    þþÿzFormMixin.get_form_kwargsc                 C   s   | j stdƒ‚t| j ƒS )ú<Return the URL to redirect to after processing a valid form.ú-No URL to redirect to. Provide a success_url.)Úsuccess_urlr   Ústrr   r   r   r   Úget_success_url9   s    zFormMixin.get_success_urlc                 C   s   t |  ¡ ƒS )z3If the form is valid, redirect to the supplied URL.)r   r(   ©r   Úformr   r   r   Ú
form_valid?   s    zFormMixin.form_validc                 C   s   |   | j|d¡S )z0If the form is invalid, render the invalid form.)r*   ©Zrender_to_responseÚget_context_datar)   r   r   r   Úform_invalidC   s    zFormMixin.form_invalidc                    s"   d|kr|   ¡ |d< tƒ jf |ŽS )z&Insert the form into the context dict.r*   )r   Úsuperr-   r"   ©Ú	__class__r   r   r-   G   s    zFormMixin.get_context_data)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r&   r   r   r   r   r   r   r(   r+   r.   r-   Ú__classcell__r   r   r0   r   r      s   
r   c                       s@   e Zd ZdZdZdd„ Z‡ fdd„Zdd„ Z‡ fd	d
„Z‡  Z	S )ÚModelFormMixinz:Provide a way to show and handle a ModelForm in a request.Nc                 C   sˆ   | j dk	r| jrtdƒ‚| jr$| jS | jdk	r6| j}n$t| ddƒdk	rP| jj}n
|  ¡ j}| j dkrttd| jj ƒ‚t	j
|| j dS dS )z*Return the form class to use in this view.Nz;Specifying both 'fields' and 'form_class' is not permitted.ÚobjectzUUsing ModelFormMixin (base class of %s) without the 'fields' attribute is prohibited.)Úfields)r9   r   r   ÚmodelÚgetattrr8   r1   Zget_querysetr2   Úmodel_formsZmodelform_factory)r   r:   r   r   r   r   S   s$    ÿ



ÿÿzModelFormMixin.get_form_classc                    s(   t ƒ  ¡ }t| dƒr$| d| ji¡ |S )r   r8   Úinstance)r/   r   Úhasattrr!   r8   r"   r0   r   r   r   p   s    

zModelFormMixin.get_form_kwargsc                 C   sJ   | j r| j jf | jjŽ}n,z| j ¡ }W n tk
rD   tdƒ‚Y nX |S )r$   z^No URL to redirect to.  Either provide a url or define a get_absolute_url method on the Model.)r&   Úformatr8   Ú__dict__Zget_absolute_urlÚAttributeErrorr   )r   Úurlr   r   r   r(   w   s    ÿ
zModelFormMixin.get_success_urlc                    s   |  ¡ | _tƒ  |¡S )z0If the form is valid, save the associated model.)Úsaver8   r/   r+   r)   r0   r   r   r+   …   s    
zModelFormMixin.form_valid)
r2   r3   r4   r5   r9   r   r   r(   r+   r6   r   r   r0   r   r7   N   s   r7   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚProcessFormViewz.Render a form on GET and processes it on POST.c                 O   s   |   |  ¡ ¡S )z=Handle GET requests: instantiate a blank version of the form.r,   ©r   r   Úargsr#   r   r   r   ÚgetŽ   s    zProcessFormView.getc                 O   s(   |   ¡ }| ¡ r|  |¡S |  |¡S dS )zˆ
        Handle POST requests: instantiate a form instance with the passed
        POST variables and then check if it's valid.
        N)r   Úis_validr+   r.   ©r   r   rF   r#   r*   r   r   r   Úpost’   s    
zProcessFormView.postc                 O   s   | j ||ŽS ©N)rJ   ©r   rF   r#   r   r   r   ÚputŸ   s    zProcessFormView.putN)r2   r3   r4   r5   rG   rJ   rM   r   r   r   r   rD   ‹   s   rD   c                   @   s   e Zd ZdZdS )ÚBaseFormViewz"A base view for displaying a form.N©r2   r3   r4   r5   r   r   r   r   rN   £   s   rN   c                   @   s   e Zd ZdZdS )ÚFormViewz?A view for displaying a form and rendering a template response.NrO   r   r   r   r   rP   §   s   rP   c                       s,   e Zd ZdZ‡ fdd„Z‡ fdd„Z‡  ZS )ÚBaseCreateViewz„
    Base view for creating a new object instance.

    Using this base class requires subclassing to provide a response mixin.
    c                    s   d | _ tƒ j|f|ž|ŽS rK   )r8   r/   rG   rE   r0   r   r   rG   ²   s    zBaseCreateView.getc                    s   d | _ tƒ j|f|ž|ŽS rK   )r8   r/   rJ   rE   r0   r   r   rJ   ¶   s    zBaseCreateView.post©r2   r3   r4   r5   rG   rJ   r6   r   r   r0   r   rQ   «   s   rQ   c                   @   s   e Zd ZdZdZdS )Ú
CreateViewzQ
    View for creating a new object, with a response rendered by a template.
    Ú_formN©r2   r3   r4   r5   Ztemplate_name_suffixr   r   r   r   rS   »   s   rS   c                       s,   e Zd ZdZ‡ fdd„Z‡ fdd„Z‡  ZS )ÚBaseUpdateViewz
    Base view for updating an existing object.

    Using this base class requires subclassing to provide a response mixin.
    c                    s   |   ¡ | _tƒ j|f|ž|ŽS rK   )Ú
get_objectr8   r/   rG   rE   r0   r   r   rG   Ê   s    
zBaseUpdateView.getc                    s   |   ¡ | _tƒ j|f|ž|ŽS rK   )rW   r8   r/   rJ   rE   r0   r   r   rJ   Î   s    
zBaseUpdateView.postrR   r   r   r0   r   rV   Ã   s   rV   c                   @   s   e Zd ZdZdZdS )Ú
UpdateViewzDView for updating an object, with a response rendered by a template.rT   NrU   r   r   r   r   rX   Ó   s   rX   c                   @   s,   e Zd ZdZdZdd„ Zdd„ Zdd„ ZdS )	ÚDeletionMixinz&Provide the ability to delete objects.Nc                 O   s$   |   ¡ | _|  ¡ }| j ¡  t|ƒS )zn
        Call the delete() method on the fetched object and then redirect to the
        success URL.
        )rW   r8   r(   Údeleter   )r   r   rF   r#   r&   r   r   r   rZ   Þ   s    

zDeletionMixin.deletec                 O   s   | j |f|ž|ŽS rK   )rZ   rE   r   r   r   rJ   é   s    zDeletionMixin.postc                 C   s$   | j r| j jf | jjŽS tdƒ‚d S )Nr%   )r&   r?   r8   r@   r   r   r   r   r   r(   ì   s    zDeletionMixin.get_success_url)r2   r3   r4   r5   r&   rZ   rJ   r(   r   r   r   r   rY   Ù   s
   rY   c                   @   s   e Zd ZdS )ÚDeleteViewCustomDeleteWarningN)r2   r3   r4   r   r   r   r   r[   ô   s   r[   c                       s4   e Zd ZdZeZ‡ fdd„Zdd„ Zdd„ Z‡  Z	S )ÚBaseDeleteViewzx
    Base view for deleting an object.

    Using this base class requires subclassing to provide a response mixin.
    c                    s<   | j jtjk	r*tjd| j j› dtdd tƒ j||Ž d S )NzbDeleteView uses FormMixin to handle POST requests. As a consequence, any custom deletion logic in z2.delete() handler should be moved to form_valid().é   )Ú
stacklevel)	r1   rZ   rY   ÚwarningsÚwarnr2   r[   r/   Ú__init__rL   r0   r   r   ra     s    úzBaseDeleteView.__init__c                 O   s2   |   ¡ | _|  ¡ }| ¡ r$|  |¡S |  |¡S d S rK   )rW   r8   r   rH   r+   r.   rI   r   r   r   rJ     s
    

zBaseDeleteView.postc                 C   s   |   ¡ }| j ¡  t|ƒS rK   )r(   r8   rZ   r   )r   r*   r&   r   r   r   r+     s    
zBaseDeleteView.form_valid)
r2   r3   r4   r5   r   r   ra   rJ   r+   r6   r   r   r0   r   r\   ø   s
   r\   c                   @   s   e Zd ZdZdZdS )Ú
DeleteViewzs
    View for deleting an object retrieved with self.get_object(), with a
    response rendered by a template.
    Z_confirm_deleteNrU   r   r   r   r   rb      s   rb   )r_   Zdjango.core.exceptionsr   Zdjango.formsr   r   r<   Zdjango.httpr   Zdjango.views.generic.baser   r   r   Zdjango.views.generic.detailr	   r
   r   r   r7   rD   rN   rP   rQ   rS   rV   rX   rY   ÚWarningr[   r\   rb   r   r   r   r   Ú<module>   s&   ?=(