o
    øÇ[hP#  ã                   @   s.  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eeƒZG dd„ deeƒZdS ) é    )Ú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   ú€/home/ubuntu/experiments/live_experiments/Pythonexperiments/Otree/venv/lib/python3.10/site-packages/django/views/generic/edit.pyÚget_initial   s   
zFormMixin.get_initialc                 C   ó   | j S )z#Return the prefix to use for forms.)Úprefixr   r   r   r   Ú
get_prefix   ó   zFormMixin.get_prefixc                 C   r   )zReturn the form class to use.)Ú
form_classr   r   r   r   Úget_form_class   r   zFormMixin.get_form_classc                 C   s"   |du r|   ¡ }|di |  ¡ ¤ŽS )z7Return an instance of the form to be used in this view.Nr   )r   Úget_form_kwargs)r   r   r   r   r   Úget_form!   s   zFormMixin.get_formc                 C   s:   |   ¡ |  ¡ dœ}| jjdv r| | 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_url7   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_invalidA   s   zFormMixin.form_invalidc                    s&   d|vr
|   ¡ |d< tƒ jdi |¤ŽS )z&Insert the form into the context dict.r,   Nr   )r   Úsuperr/   r$   ©Ú	__class__r   r   r/   E   s   zFormMixin.get_context_data©N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r(   r   r   r   r   r   r   r*   r-   r0   r/   Ú__classcell__r   r   r2   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ur| jrtdƒ‚| jr| jS | jdur| j}nt| ddƒdur(| jj}n|  ¡ j}| j du r:td| jj ƒ‚t	j
|| j 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)r<   r   r   ÚmodelÚgetattrr;   r3   Zget_querysetr5   Úmodel_formsZmodelform_factory)r   r=   r   r   r   r   Q   s$   ÿ



ÿÿzModelFormMixin.get_form_classc                    s(   t ƒ  ¡ }t| dƒr| d| ji¡ |S )r   r;   Úinstance)r1   r   Úhasattrr#   r;   r$   r2   r   r   r   n   s   

zModelFormMixin.get_form_kwargsc                 C   sH   | j r| j jdi | jj¤Ž}|S z| j ¡ }W |S  ty#   tdƒ‚w )r&   z^No URL to redirect to.  Either provide a url or define a get_absolute_url method on the Model.Nr   )r(   Úformatr;   Ú__dict__Zget_absolute_urlÚAttributeErrorr   )r   Úurlr   r   r   r*   u   s   	ùûÿÿzModelFormMixin.get_success_urlc                    s   |  ¡ | _tƒ  |¡S )z0If the form is valid, save the associated model.)Úsaver;   r1   r-   r+   r2   r   r   r-   ƒ   s   
zModelFormMixin.form_valid)
r5   r6   r7   r8   r<   r   r   r*   r-   r9   r   r   r2   r   r:   L   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   |   |  ¡ ¡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 )zˆ
        Handle POST requests: instantiate a form instance with the passed
        POST variables and then check if it's valid.
        )r   Úis_validr-   r0   ©r   r!   rI   r%   r,   r   r   r   Úpost   s   

zProcessFormView.postc                 O   s   | j |i |¤ŽS r4   )rM   )r   rI   r%   r   r   r   Úput   s   zProcessFormView.putN)r5   r6   r7   r8   rJ   rM   rN   r   r   r   r   rG   ‰   s
    rG   c                   @   ó   e Zd ZdZdS )ÚBaseFormViewz"A base view for displaying a form.N©r5   r6   r7   r8   r   r   r   r   rP   ¡   ó    rP   c                   @   rO   )ÚFormViewz?A view for displaying a form and rendering a template response.NrQ   r   r   r   r   rS   ¥   rR   rS   c                       ó,   e Zd ZdZ‡ fdd„Z‡ fdd„Z‡  ZS )ÚBaseCreateViewzs
    Base view for creating a new object instance.

    This requires subclassing to provide a response mixin.
    c                    ó    d | _ tƒ j|g|¢R i |¤ŽS r4   )r;   r1   rJ   rH   r2   r   r   rJ   °   ó   zBaseCreateView.getc                    rV   r4   )r;   r1   rM   rH   r2   r   r   rM   ´   rW   zBaseCreateView.post©r5   r6   r7   r8   rJ   rM   r9   r   r   r2   r   rU   ©   ó    rU   c                   @   ó   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©r5   r6   r7   r8   Ztemplate_name_suffixr   r   r   r   r[   ¹   s    r[   c                       rT   )ÚBaseUpdateViewzp
    Base view for updating an existing object.

    This requires subclassing to provide a response mixin.
    c                    ó$   |   ¡ | _tƒ j|g|¢R i |¤ŽS r4   )Ú
get_objectr;   r1   rJ   rH   r2   r   r   rJ   È   ó   
zBaseUpdateView.getc                    r_   r4   )r`   r;   r1   rM   rH   r2   r   r   rM   Ì   ra   zBaseUpdateView.postrX   r   r   r2   r   r^   Á   rY   r^   c                   @   rZ   )Ú
UpdateViewzDView for updating an object, with a response rendered by a template.r\   Nr]   r   r   r   r   rb   Ñ   s    rb   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.
        )r`   r;   r*   Údeleter   )r   r!   rI   r%   r(   r   r   r   rd   Ü   s   

zDeletionMixin.deletec                 O   s   | j |g|¢R i |¤ŽS r4   )rd   rH   r   r   r   rM   ç   s   zDeletionMixin.postc                 C   s$   | j r| j jdi | jj¤ŽS tdƒ‚)Nr'   r   )r(   rB   r;   rC   r   r   r   r   r   r*   ê   s   zDeletionMixin.get_success_url)r5   r6   r7   r8   r(   rd   rM   r*   r   r   r   r   rc   ×   s    rc   c                   @   s$   e Zd ZdZeZdd„ Zdd„ ZdS )ÚBaseDeleteViewzg
    Base view for deleting an object.

    This requires subclassing to provide a response mixin.
    c                 O   s.   |   ¡ | _|  ¡ }| ¡ r|  |¡S |  |¡S r4   )r`   r;   r   rK   r-   r0   rL   r   r   r   rM   ú   s
   


zBaseDeleteView.postc                 C   s   |   ¡ }| j ¡  t|ƒS r4   )r*   r;   rd   r   )r   r,   r(   r   r   r   r-     s   
zBaseDeleteView.form_validN)r5   r6   r7   r8   r   r   rM   r-   r   r   r   r   re   ñ   s
    re   c                   @   rZ   )Ú
DeleteViewzs
    View for deleting an object retrieved with self.get_object(), with a
    response rendered by a template.
    Z_confirm_deleteNr]   r   r   r   r   rf     s    rf   N)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   r:   rG   rP   rS   rU   r[   r^   rb   rc   re   rf   r   r   r   r   Ú<module>   s$    ?=