o
    j`dk                     @   sJ   d Z ddlmZ ddlmZ ddlmZ ddlmZ dZ	G dd	 d	Z
d
S )z 
Formtools Preview application.
    )Http404)render)constant_time_compare   	form_hmaczformtools_%sc                   @   s   e Zd ZdZdZdd Zdd Zdd Zd	d
 Zdd Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!S )"FormPreviewzformtools/preview.htmlzformtools/form.htmlc                 C   s   |i | _ | _d S N)formstate)selfr
    r   P/var/www/mitschrieb_seite/venv/lib/python3.10/site-packages/formtools/preview.py__init__   s   zFormPreview.__init__c                 O   sn   ddd |j | dd}| j|g|R i | zt| |d |j  }W ||S  ty6   tw )Npreviewpost)12stage_)	getPOSTunused_nameparse_paramsgetattrmethodlowerAttributeErrorr   )r   requestargskwargsr   r   r   r   r   __call__   s   zFormPreview.__call__c                 C   s2   	 z| j j|  W n
 ty   Y |S w |d7 }q)a  
        Given a first-choice name, adds an underscore to the name until it
        reaches a name that isn't claimed by any field in the form.

        This is calculated rather than being hard-coded so that no field names
        are off-limits for use in the form.
        r   r   )r
   base_fieldsKeyError)r   namer   r   r   r   #   s   zFormPreview.unused_namec                 C   s.   | j |  | |d}t|| j| ||S )zDisplays the form)auto_idinitial)r
   get_auto_idget_initialr   form_templateget_context)r   r   fr   r   r   preview_get3   s   
zFormPreview.preview_getc                 C   st   | j |j|j|  d}| ||}| r3| ||| | d|d< | |||d< t	|| j
|S t	|| j|S )zn
        Validates the POST data. If valid, displays the preview page.
        Else, redisplays form.
        )datafilesr%   hash
hash_field
hash_value)r
   r   FILESr'   r*   is_validprocess_previewr   security_hashr   preview_templater)   )r   r   r+   contextr   r   r   preview_post9   s   zFormPreview.preview_postc                 C   s   |  ||}t||S r	   )r5   r   )r   tokenr   r
   expectedr   r   r   _check_security_hashJ   s   
z FormPreview._check_security_hashc                 C   sh   | j |j|  d}| r)| |j| dd||s"| |S | ||j	S t
|| j| ||S )zY
        Validates the POST data. If valid, calls done(). Else, redisplays form.
        )r%   r/    )r
   r   r'   r3   r;   r   r   failed_hashdonecleaned_datar   r)   r*   r   r   r
   r   r   r   	post_postN   s   
zFormPreview.post_postc                 C   s   t S )z
        Hook to override the ``auto_id`` kwarg for the form. Needed when
        rendering two form previews in the same template.
        )AUTO_ID)r   r   r   r   r'   ^      zFormPreview.get_auto_idc                 C   s   i S )z
        Takes a request argument and returns a dictionary to pass to the form's
        ``initial`` kwarg when the form is being created from an HTTP get.
        r   r   r   r   r   r   r(   e   rC   zFormPreview.get_initialc                 C   s   ||  d| jdS )zContext for template rendering.r   )r
   stage_fieldr   )r   r   r@   r   r   r   r*   l   s   zFormPreview.get_contextc                 O      dS )aL  
        Given captured args and kwargs from the URLconf, saves something in
        self.state and/or raises :class:`~django.http.Http404` if necessary.

        For example, this URLconf captures a user_id variable::

            path('contact/<int:user_id>/', MyFormPreview(MyForm)),

        In this case, the kwargs variable in parse_params would be
        ``{'user_id': 32}`` for a request to ``'/contact/32/'``. You can use
        that ``user_id`` to make sure it's a valid user and/or save it for
        later, for use in :meth:`~formtools.preview.FormPreview.done()`.
        Nr   )r   r   r   r    r   r   r   r   t   s   zFormPreview.parse_paramsc                 C   rF   )aE  
        Given a validated form, performs any extra processing before displaying
        the preview page, and saves any extra data in context.

        By default, this method is empty.  It is called after the form is
        validated, but before the context is modified with hash information
        and rendered.
        Nr   )r   r   r
   r7   r   r   r   r4      s   	zFormPreview.process_previewc                 C   s   t |S )a	  
        Calculates the security hash for the given
        :class:`~django.http.HttpRequest` and :class:`~django.forms.Form`
        instances.

        Subclasses may want to take into account request-specific information,
        such as the IP address.
        r   r@   r   r   r   r5      s   	zFormPreview.security_hashc                 C   s
   |  |S )zp
        Returns an :class:`~django.http.HttpResponse` in the case of
        an invalid security hash.
        )r8   rD   r   r   r   r=      s   
zFormPreview.failed_hashc                 C   s   t d| jj )z
        Does something with the ``cleaned_data`` data and then needs to
        return an :class:`~django.http.HttpResponseRedirect`, e.g. to a
        success page.
        z4You must define a done() method on your %s subclass.)NotImplementedError	__class____name__)r   r   r?   r   r   r   r>      s   zFormPreview.doneN)rI   
__module____qualname__r6   r)   r   r!   r   r,   r8   r;   rA   r'   r(   r*   r   r4   r5   r=   r>   r   r   r   r   r      s$    	r   N)__doc__django.httpr   django.shortcutsr   django.utils.cryptor   utilsr   rB   r   r   r   r   r   <module>   s    