a
    ù!f¶
  ã                   @   sR   d Z ddlZddlZddlmZ ddlmZ ddlZddlZG dd„ dej	ƒZ
dS )z1Contains classes used for the Django ORM storage.é    N)Úmodels)Úencodingc                       sH   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Zdd	„ Zd
d„ Zdd„ Z	‡  Z
S )ÚCredentialsFieldz0Django ORM field for storing OAuth2 Credentials.c                    s*   d|vrd|d< t t| ƒj|i |¤Ž d S )NÚnullT)Úsuperr   Ú__init__)ÚselfÚargsÚkwargs©Ú	__class__© úh/var/www/html/python-backend/venv/lib/python3.9/site-packages/oauth2client/contrib/django_util/models.pyr      s    zCredentialsField.__init__c                 C   s   dS )NZBinaryFieldr   )r   r   r   r   Úget_internal_type#   s    z"CredentialsField.get_internal_typec                 C   s
   |   |¡S )z„Overrides ``models.Field`` method. This converts the value
        returned from the database to an instance of this class.
        )Ú	to_python)r   ÚvalueZ
expressionÚ
connectionÚcontextr   r   r   Úfrom_db_value&   s    zCredentialsField.from_db_valuec              
   C   sh   |du rdS t |tjjƒr|S zt t t 	|¡¡ ¡ ¡W S  t
yb   t t t 	|¡¡¡ Y S 0 dS )z~Overrides ``models.Field`` method. This is used to convert
        bytes (from serialization etc) to an instance of this classN)Ú
isinstanceÚoauth2clientÚclientÚCredentialsÚ
jsonpickleÚdecodeÚbase64Ú	b64decoder   Zsmart_bytesÚ
ValueErrorÚpickleÚloads©r   r   r   r   r   r   ,   s    ÿÿzCredentialsField.to_pythonc                 C   s*   |du rdS t  t t |¡ ¡ ¡¡S dS )z®Overrides ``models.Field`` method. This is used to convert
        the value from an instances of this class to bytes that can be
        inserted into the database.
        N)r   Z
smart_textr   Ú	b64encoder   Úencoder    r   r   r   Úget_prep_value;   s
    ÿzCredentialsField.get_prep_valuec                 C   s   |   |¡}|  |¡S )zçConvert the field value from the provided model to a string.

        Used during model serialization.

        Args:
            obj: db.Model, model object

        Returns:
            string, the serialized field value
        )Z_get_val_from_objr#   )r   Úobjr   r   r   r   Úvalue_to_stringF   s    
z CredentialsField.value_to_string)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r#   r%   Ú__classcell__r   r   r   r   r      s   r   )r)   r   r   Z	django.dbr   Zdjango.utilsr   r   r   ÚFieldr   r   r   r   r   Ú<module>   s   