ó
‚¾^Yc           @   ss   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 d e j	 f d „  ƒ  YZ
 d S(   s1   Contains classes used for the Django ORM storage.iÿÿÿÿN(   t   models(   t   encodingt   CredentialsFieldc           B   sD   e  Z d  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s0   Django ORM field for storing OAuth2 Credentials.c         O   s6   d | k r t  | d <n  t t |  ƒ j | |   d  S(   Nt   null(   t   Truet   superR   t   __init__(   t   selft   argst   kwargs(    (    sM   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/contrib/django_util/models.pyR      s    c         C   s   d S(   Nt   BinaryField(    (   R   (    (    sM   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/contrib/django_util/models.pyt   get_internal_type#   s    c         C   s   |  j  | ƒ S(   s„   Overrides ``models.Field`` method. This converts the value
        returned from the database to an instance of this class.
        (   t	   to_python(   R   t   valuet
   expressiont
   connectiont   context(    (    sM   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/contrib/django_util/models.pyt   from_db_value&   s    c         C   s†   | d k r d St | t j j ƒ r) | Sy) t j t j t	 j
 | ƒ ƒ j ƒ  ƒ SWn- t k
 r t j t j t	 j
 | ƒ ƒ ƒ SXd S(   s~   Overrides ``models.Field`` method. This is used to convert
        bytes (from serialization etc) to an instance of this classN(   t   Nonet
   isinstancet   oauth2clientt   clientt   Credentialst
   jsonpicklet   decodet   base64t	   b64decodeR   t   smart_bytest
   ValueErrort   picklet   loads(   R   R   (    (    sM   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/contrib/django_util/models.pyR   ,   s    #c         C   s9   | d k r d St j t j t j | ƒ j ƒ  ƒ ƒ Sd S(   s®   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   R   t
   smart_textR   t	   b64encodeR   t   encode(   R   R   (    (    sM   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/contrib/django_util/models.pyt   get_prep_value;   s    c         C   s   |  j  | ƒ } |  j | ƒ S(   sç   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
        (   t   _get_val_from_objR"   (   R   t   objR   (    (    sM   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/contrib/django_util/models.pyt   value_to_stringF   s    (	   t   __name__t
   __module__t   __doc__R   R   R   R   R"   R%   (    (    (    sM   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/contrib/django_util/models.pyR      s   					(   R(   R   R   t	   django.dbR    t   django.utilsR   R   R   t   FieldR   (    (    (    sM   /tmp/pip-build-kpPAdC/oauth2client/oauth2client/contrib/django_util/models.pyt   <module>   s   