
v^                  @   s  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 Gd d   d e  Z Gd d	   d	 e e  Z Gd
 d   d e
  Z Gd d   d e e  Z Gd d   d e	 e  Z Gd d   d e e  Z Gd d   d e e  Z Gd d   d e e  Z Gd d   d e e  Z Gd d   d  Z Gd d   d e e  Z Gd d   d e e  Z d S)    )ImproperlyConfigured)models)HttpResponseRedirect)ContextMixinTemplateResponseMixinView)BaseDetailViewSingleObjectMixin!SingleObjectTemplateResponseMixinc                   s   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   S)	FormMixinz5Provide a way to show and handle a form in a request.Nc             C   s   |  j  j   S)z6Return the initial data to use for forms on this view.)initialcopy)self r   ;/tmp/pip-build-8lau8j11/django/django/views/generic/edit.pyget_initial   s    zFormMixin.get_initialc             C   s   |  j  S)z#Return the prefix to use for forms.)prefix)r   r   r   r   
get_prefix   s    zFormMixin.get_prefixc             C   s   |  j  S)zReturn the form class to use.)
form_class)r   r   r   r   get_form_class   s    zFormMixin.get_form_classc             C   s(   | d k r |  j    } | |  j     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   sY   d |  j    d |  j   i } |  j j d k rU | j d |  j j d |  j j i  | S)z8Return the keyword arguments for instantiating the form.r   r   POSTPUTdatafiles)zPOSTzPUT)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)z<Return the URL to redirect to after processing a valid form.z-No URL to redirect to. Provide a success_url.)success_urlr   str)r   r   r   r   get_success_url1   s    	zFormMixin.get_success_urlc             C   s   t  |  j    S)z3If the form is valid, redirect to the supplied URL.)r   r"   )r   formr   r   r   
form_valid7   s    zFormMixin.form_validc             C   s   |  j  |  j d |   S)z0If the form is invalid, render the invalid form.r#   )render_to_responseget_context_data)r   r#   r   r   r   form_invalid;   s    zFormMixin.form_invalidc                s,   d | k r |  j    | d <t   j |   S)z&Insert the form into the context dict.r#   )r   superr&   )r   r   )	__class__r   r   r&   ?   s    zFormMixin.get_context_data)__name__
__module____qualname____doc__r   r   r    r   r   r   r   r   r   r"   r$   r'   r&   r   r   )r)   r   r   
   s   r   c                   sX   e  Z d  Z d Z d Z d d   Z   f d d   Z d d   Z   f d	 d
   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 r4 |  j S|  j d k	 rO |  j } n6 t |  d d  d k	 rv |  j j } n |  j   j } |  j  d k r t d |  j j   t	 j
 | d |  j  Sd 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)r0   r   r   modelgetattrr/   r)   Zget_querysetr*   model_formsZmodelform_factory)r   r1   r   r   r   r   J   s    		zModelFormMixin.get_form_classc                s8   t    j   } t |  d  r4 | j d |  j i  | S)z8Return the keyword arguments for instantiating the form.r/   instance)r(   r   hasattrr   r/   )r   r   )r)   r   r   r   g   s    zModelFormMixin.get_form_kwargsc             C   s\   |  j  r$ |  j  j |  j j   } n4 y |  j j   } Wn t k
 rW t d   Yn X| S)z<Return the URL to redirect to after processing a valid form.z^No URL to redirect to.  Either provide a url or define a get_absolute_url method on the Model.)r    formatr/   __dict__Zget_absolute_urlAttributeErrorr   )r   urlr   r   r   r"   n   s    	zModelFormMixin.get_success_urlc                s   | j    |  _ t   j |  S)z0If the form is valid, save the associated model.)saver/   r(   r$   )r   r#   )r)   r   r   r$   {   s    zModelFormMixin.form_valid)	r*   r+   r,   r-   r0   r   r   r"   r$   r   r   )r)   r   r.   F   s   r.   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   |  j  |  j    S)z=Handle GET requests: instantiate a blank version of the form.)r%   r&   )r   r   argsr   r   r   r   get   s    zProcessFormView.getc             O   s6   |  j    } | j   r% |  j |  S|  j |  Sd S)z
        Handle POST requests: instantiate a form instance with the passed
        POST variables and then check if it's valid.
        N)r   Zis_validr$   r'   )r   r   r<   r   r#   r   r   r   post   s    zProcessFormView.postc             O   s   |  j  | |   S)N)r>   )r   r<   r   r   r   r   put   s    zProcessFormView.putN)r*   r+   r,   r-   r=   r>   r?   r   r   r   r   r;      s   r;   c               @   s   e  Z d  Z d Z d S)BaseFormViewz"A base view for displaying a form.N)r*   r+   r,   r-   r   r   r   r   r@      s   r@   c               @   s   e  Z d  Z d Z d S)FormViewz?A view for displaying a form and rendering a template response.N)r*   r+   r,   r-   r   r   r   r   rA      s   rA   c                   s:   e  Z d  Z d Z   f d d   Z   f d d   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 | | |  S)N)r/   r(   r=   )r   r   r<   r   )r)   r   r   r=      s    	zBaseCreateView.getc                s   d  |  _  t   j | | |  S)N)r/   r(   r>   )r   r   r<   r   )r)   r   r   r>      s    	zBaseCreateView.post)r*   r+   r,   r-   r=   r>   r   r   )r)   r   rB      s   rB   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)r*   r+   r,   r-   template_name_suffixr   r   r   r   rC      s   rC   c                   s:   e  Z d  Z d Z   f d d   Z   f d d   Z   S)BaseUpdateViewz
    Base view for updating an existing object.

    Using this base class requires subclassing to provide a response mixin.
    c                s%   |  j    |  _ t   j | | |  S)N)
get_objectr/   r(   r=   )r   r   r<   r   )r)   r   r   r=      s    zBaseUpdateView.getc                s%   |  j    |  _ t   j | | |  S)N)rG   r/   r(   r>   )r   r   r<   r   )r)   r   r   r>      s    zBaseUpdateView.post)r*   r+   r,   r-   r=   r>   r   r   )r)   r   rF      s   rF   c               @   s   e  Z d  Z d Z d Z d S)
UpdateViewzDView for updating an object, with a response rendered by a template.rD   N)r*   r+   r,   r-   rE   r   r   r   r   rH      s   rH   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   s2   |  j    |  _ |  j   } |  j j   t |  S)zn
        Call the delete() method on the fetched object and then redirect to the
        success URL.
        )rG   r/   r"   deleter   )r   r   r<   r   r    r   r   r   rJ      s    zDeletionMixin.deletec             O   s   |  j  | | |  S)N)rJ   )r   r   r<   r   r   r   r   r>      s    zDeletionMixin.postc             C   s/   |  j  r |  j  j |  j j   St d   d  S)Nz-No URL to redirect to. Provide a success_url.)r    r6   r/   r7   r   )r   r   r   r   r"      s    	zDeletionMixin.get_success_url)r*   r+   r,   r-   r    rJ   r>   r"   r   r   r   r   rI      s
   rI   c               @   s   e  Z d  Z d Z d S)BaseDeleteViewzx
    Base view for deleting an object.

    Using this base class requires subclassing to provide a response mixin.
    N)r*   r+   r,   r-   r   r   r   r   rK      s   rK   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_deleteN)r*   r+   r,   r-   rE   r   r   r   r   rL      s   rL   N)Zdjango.core.exceptionsr   Zdjango.formsr   r3   Zdjango.httpr   Zdjango.views.generic.baser   r   r   Zdjango.views.generic.detailr   r	   r
   r   r.   r;   r@   rA   rB   rC   rF   rH   rI   rK   rL   r   r   r   r   <module>   s    <;