a
    Sic                  	   @   s  d dl Z d dl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 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 d dlmZmZ d dlmZ e dddZ!ee dddZ"eeeedf e#dddZ$d.eeedf e#dddZ%d/eeedf e#dddZ&e dddZ'e(dddZ)e) Z*ed d!d"Z+d#d$ Z,e(dd%d&Z-edd'd(Z.eed)d0e	ee e#ee# ee# e(d+d,d-Z/dS )1    N)Mapping)datetime)OptionalUnionAny)settings)AnonymousUserUser)Signer)reverse)get_random_string)	force_str)	mark_safe)validate_arguments)	constantsPYDANTIC_CONFIG)QRCodeOptions)returnc                   C   s   t jdt jtjt jdt jdiS )N   Zqr_code_url_protection_saltF)r   TOKEN_LENGTHSIGNING_KEYr   
SECRET_KEYSIGNING_SALT,ALLOWS_EXTERNAL_REQUESTS_FOR_REGISTERED_USER r   r   P/var/www/html/django/DPS/env/lib/python3.9/site-packages/qr_code/qrcode/serve.py#_get_default_url_protection_options   s
    r   c                   C   s    t tdrttjtrtjS d S )NQR_CODE_URL_PROTECTION)hasattrr   
isinstancer   r   r   r   r   r   _get_url_protection_settings   s    r    )url_protection_optionsuserr   c                 C   sL   t | tj r$| tj |pt }n$| tj du rD|o@|jo@|j}nd}|S )NTF)callabler   r   r   pkis_authenticated)r!   r"   Zallows_external_requestr   r   r   _options_allow_external_request#   s    r&   )r"   r   c                 C   s   t t |  S Nr&   get_url_protection_optionsr"   r   r   r   requires_url_protection_token.   s    r+   c                 C   s   t t | S r'   r(   r*   r   r   r   !allows_external_request_from_user2   s    r,   c                  C   s$   t  } t }|d ur | tj | S r'   )r   r    updater   r   )optionsZsettings_optionsr   r   r   r)   6   s
    r)   c                  C   s   t  } t| tj S r'   )r)   r   r   r   )r!   r   r   r   _make_random_token>   s    r/   )qr_code_optionsc                 C   s2   t  }t|tj |tj d}|t| t}|S )z2Generate a signed token to handle view protection.)keysalt)r)   r
   r   r   r   signget_qr_url_protection_token_RANDOM_TOKEN)r0   r!   signertokenr   r   r   "get_qr_url_protection_signed_tokenF   s    r8   c                 C   s.   d ttt| j| j| jpd| j| j|fS )a  
    Generate a random token for the QR code image.

    The token contains image attributes so that a user cannot use a token provided somewhere on a website to
    generate bigger QR codes. The random_token part ensures that the signed token is not predictable.
    . )	joinlistmapstrsizeborderversionimage_formaterror_correction)r0   Zrandom_tokenr   r   r   r4   N   s    r4   c                 C   s&   d| j  d| j  dtj  dS )N":z	:version_)pathGET	urlencoder   QR_CODE_GENERATION_VERSION_DATE	isoformat)requestr   r   r   qr_code_etagf   s    rL   c                 C   s   t jS r'   )r   rI   )_requestr   r   r   qr_code_last_modifiedj   s    rN   )configT)datar0   
force_textcache_enabledurl_signature_enabledr   c                 C   s  |du rt  n|}|du r tj}|du r.tj}|r6dnd}|rhttt| ddd}t	||d}nZt
| trt	| |d}nBt
| trtt| d}n
t| }t|dd}t	||d}|jtjkr|j|d	< |jtjkr|j|d
< |jtjkr|j|d< |jtjkr|j|d< |jtjkr6|j|d< |jrFd|d< |jrVd|d< |jrfd|d< |jrt|jnd|d< ||  td}	|rt|}
|
|d< |	 dtj | }t!|S )a&  Build an URL to a view that handle serving QR code image from the given parameters.

    Any invalid argument related to the size or the format of the image is silently
    converted into the default value for that argument.

    :param str data: Data to encode into a QR code.
    :param QRCodeOptions qr_code_options: The rendering options for the QR code.
    :param bool force_text: Tells whether we want to force the `data` to be considered as text string and encoded in
        byte mode.
    :param bool cache_enabled: Allows to skip caching the QR code (when set to *False*) when caching has
        been enabled.
    :param bool url_signature_enabled: Tells whether the random token for protecting the URL against
        external requests is added to the returned URL. It defaults to *True*.
    N   r   zutf-8)encoding)textrR   )intrR   )bytesrR   r?   r@   rA   rB   rC   microeciboost_errorr:   rU   zqr_code:serve_qr_code_imager7   ?)"r   r   DEFAULT_URL_SIGNATURE_ENABLEDDEFAULT_CACHE_ENABLEDr>   base64	b64encoder   encodedictr   rW   r?   DEFAULT_MODULE_SIZEr@   DEFAULT_BORDER_SIZErA   DEFAULT_VERSIONrB   DEFAULT_IMAGE_FORMATrC   DEFAULT_ERROR_CORRECTIONrY   rZ   r[   rU   r-   Zcolor_mappingr   r8   urllibparserH   r   )rP   r0   rQ   rR   rS   Zcache_enabled_argencoded_dataparamsZb64datarF   r7   urlr   r   r   make_qr_code_urln   sP    







rm   )N)N)NTNN)0r_   urllib.parserh   collections.abcr   r   typingr   r   r   django.confr   django.contrib.auth.modelsr   r	   Zdjango.core.signingr
   django.urlsr   django.utils.cryptor   django.utils.encodingr   django.utils.safestringr   pydanticr   qr_code.qrcoder   r   qr_code.qrcode.utilsr   rb   r   r    boolr&   r+   r,   r)   r>   r/   r5   r8   r4   rL   rN   rm   r   r   r   r   <module>   sN   	    