ķ
ē9^c           @   s*  d  d l  Z  d  d l 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 d  d l m Z d	 Z d Z d d g Z e  j d  Z d d d d g Z d   Z d   Z d   Z d   Z d   Z d e j f d     YZ d e	 j f d     YZ d S(   i˙˙˙˙N(   t   forms(   t   MaxValueValidator(   t   MinValueValidator(   t   RegexValidator(   t   modelst
   connection(   t   six(   t   ugettext_lazyi    i   i@   i   t   HexadecimalFieldt   HexIntegerFields   ^(([0-9A-f])|(0x[0-9A-f]))+$s   django.db.backends.postgresqls&   django.db.backends.postgresql_psycopg2s&   django.contrib.gis.db.backends.postgiss   django.db.backends.sqlite3c           C   s   t  j d t k S(   Nt   ENGINE(   R   t   settings_dictt   signed_integer_engines(    (    (    s3   /tmp/pip-unpacked-wheel-zRhu6W/fcm_django/fields.pyt   _using_signed_storage   s    c         C   s    t  j d t  j d |    d S(   Nt   Qt   qi    (   t   structt   unpackt   pack(   t   value(    (    s3   /tmp/pip-unpacked-wheel-zRhu6W/fcm_django/fields.pyt   _signed_to_unsigned_integer   s    c         C   s    t  j d t  j d |    d S(   NR   R   i    (   R   R   R   (   R   (    (    s3   /tmp/pip-unpacked-wheel-zRhu6W/fcm_django/fields.pyt   _unsigned_to_signed_integer!   s    c         C   s   t  |  d  S(   Ni   (   t   int(   R   (    (    s3   /tmp/pip-unpacked-wheel-zRhu6W/fcm_django/fields.pyt   _hex_string_to_unsigned_integer%   s    c         C   s   t  |   j d  S(   Nt   L(   t   hext   rstrip(   R   (    (    s3   /tmp/pip-unpacked-wheel-zRhu6W/fcm_django/fields.pyt   _unsigned_integer_to_hex_string)   s    c           B   s    e  Z d  Z d   Z d   Z RS(   s<   
    A form field that accepts only hexadecimal numbers
    c         O   s;   t  t t d  d  g |  _ t t |   j | |   d  S(   Ns    Enter a valid hexadecimal numbert   invalid(   R   t   hex_ret   _t   default_validatorst   superR   t   __init__(   t   selft   argst   kwargs(    (    s3   /tmp/pip-unpacked-wheel-zRhu6W/fcm_django/fields.pyR!   2   s    c         C   sP   | r7 t  | t j  r7 t j d k r7 t |  } n  t t j |   j	 |  S(   Nt   mysqlt   sqlite(   R%   R&   (
   t
   isinstanceR   t   string_typesR   t   vendorR   R    R    t	   CharFieldt   prepare_value(   R"   R   (    (    s3   /tmp/pip-unpacked-wheel-zRhu6W/fcm_django/fields.pyR+   9   s    (   t   __name__t
   __module__t   __doc__R!   R+   (    (    (    s3   /tmp/pip-unpacked-wheel-zRhu6W/fcm_django/fields.pyR   -   s   	c           B   s\   e  Z d  Z e e  e e  g Z d   Z d   Z	 d   Z
 d   Z d   Z d   Z RS(   sĪ  
    This field stores a hexadecimal *string* of up to 64 bits as an unsigned integer
    on *all* backends including postgres.

    Reasoning: Postgres only supports signed bigints. Since we don't care about
    signedness, we store it as signed, and cast it to unsigned when we deal with
    the actual value (with struct)

    On sqlite and mysql, native unsigned bigint types are used. In all cases, the
    value we deal with in python is always in hex.
    c         C   sJ   | j  d } d | k r d Sd | k r- d St t |   j d |  Sd  S(   NR
   R%   s   bigint unsignedR&   s   UNSIGNED BIG INTR   (   R   R    R	   t   db_type(   R"   R   t   engine(    (    s3   /tmp/pip-unpacked-wheel-zRhu6W/fcm_django/fields.pyR/   S   s    c         C   sY   | d k s | d k r d St | t j  r= t |  } n  t   rU t |  } n  | S(   s;    Return the integer value to be stored from the hex string t    N(   t   NoneR'   R   R(   R   R   R   (   R"   R   (    (    s3   /tmp/pip-unpacked-wheel-zRhu6W/fcm_django/fields.pyt   get_prep_value\   s    	c         C   s,   | d k r | St   r( t |  } n  | S(   s<    Return an unsigned int representation from all db backends N(   R2   R   R   (   R"   R   t
   expressionR   t   context(    (    s3   /tmp/pip-unpacked-wheel-zRhu6W/fcm_django/fields.pyt   from_db_valuef   s
    	c         C   s0   t  | t j  r | S| d k r& | St |  S(   s0    Return a str representation of the hexadecimal N(   R'   R   R(   R2   R   (   R"   R   (    (    s3   /tmp/pip-unpacked-wheel-zRhu6W/fcm_django/fields.pyt	   to_pythonn   s
    c         K   s3   i t  d 6} | j |  t t j |   j |   S(   Nt
   form_class(   R   t   updateR    R   t   IntegerFieldt	   formfield(   R"   R$   t   defaults(    (    s3   /tmp/pip-unpacked-wheel-zRhu6W/fcm_django/fields.pyR;   v   s    c         C   s%   t  |  } t t j |   j |  S(   N(   R   R    R   t   BigIntegerFieldt   run_validators(   R"   R   (    (    s3   /tmp/pip-unpacked-wheel-zRhu6W/fcm_django/fields.pyR>   |   s    (   R,   R-   R.   R   t   UNSIGNED_64BIT_INT_MIN_VALUER   t   UNSIGNED_64BIT_INT_MAX_VALUEt
   validatorsR/   R3   R6   R7   R;   R>   (    (    (    s3   /tmp/pip-unpacked-wheel-zRhu6W/fcm_django/fields.pyR	   A   s   				
			l            l   ˙˙˙˙ (   t   reR   t   djangoR    t   django.core.validatorsR   R   R   t	   django.dbR   R   t   django.utilsR   t   django.utils.translationR   R   R?   R@   t   __all__t   compileR   R   R   R   R   R   R   R*   R   R=   R	   (    (    (    s3   /tmp/pip-unpacked-wheel-zRhu6W/fcm_django/fields.pyt   <module>   s.   						