ó
O'—^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 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.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(    (    (    s;   /tmp/pip-unpacked-wheel-MTFMX_/push_notifications/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(    (    s;   /tmp/pip-unpacked-wheel-MTFMX_/push_notifications/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   (    (    s;   /tmp/pip-unpacked-wheel-MTFMX_/push_notifications/fields.pyt   _unsigned_to_signed_integer!   s    c         C   s   t  |  d ƒ S(   Ni   (   t   int(   R   (    (    s;   /tmp/pip-unpacked-wheel-MTFMX_/push_notifications/fields.pyt   _hex_string_to_unsigned_integer%   s    c         C   s   t  |  ƒ j d ƒ S(   Nt   L(   t   hext   rstrip(   R   (    (    s;   /tmp/pip-unpacked-wheel-MTFMX_/push_notifications/fields.pyt   _unsigned_integer_to_hex_string)   s    c           B   s    e  Z d  Z d „  Z d „  Z RS(   s6   
	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(    (    s;   /tmp/pip-unpacked-wheel-MTFMX_/push_notifications/fields.pyR!   1   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   (    (    s;   /tmp/pip-unpacked-wheel-MTFMX_/push_notifications/fields.pyR+   5   s    (   t   __name__t
   __module__t   __doc__R!   R+   (    (    (    s;   /tmp/pip-unpacked-wheel-MTFMX_/push_notifications/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(    (    s;   /tmp/pip-unpacked-wheel-MTFMX_/push_notifications/fields.pyR/   O   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   (    (    s;   /tmp/pip-unpacked-wheel-MTFMX_/push_notifications/fields.pyt   get_prep_valueX   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(    (    s;   /tmp/pip-unpacked-wheel-MTFMX_/push_notifications/fields.pyt   from_db_valueb   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   (    (    s;   /tmp/pip-unpacked-wheel-MTFMX_/push_notifications/fields.pyt	   to_pythonj   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(    (    s;   /tmp/pip-unpacked-wheel-MTFMX_/push_notifications/fields.pyR;   r   s    c         C   s%   t  | ƒ } t t j |  ƒ j | ƒ S(   N(   R   R    R   t   BigIntegerFieldt   run_validators(   R"   R   (    (    s;   /tmp/pip-unpacked-wheel-MTFMX_/push_notifications/fields.pyR>   x   s    (   R,   R-   R.   R   t   UNSIGNED_64BIT_INT_MIN_VALUER   t   UNSIGNED_64BIT_INT_MAX_VALUEt
   validatorsR/   R3   R6   R7   R;   R>   (    (    (    s;   /tmp/pip-unpacked-wheel-MTFMX_/push_notifications/fields.pyR	   =   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	   (    (    (    s;   /tmp/pip-unpacked-wheel-MTFMX_/push_notifications/fields.pyt   <module>   s,   						