<s> import re <EOL> import struct <EOL> from django import forms <EOL> from django . core . validators import MaxValueValidator <EOL> from django . core . validators import MinValueValidator <EOL> from django . core . validators import RegexValidator <EOL> from django . db import models , connection <EOL> from django . utils import six <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> UNSIGNED_64BIT_INT_MIN_VALUE = <NUM_LIT:0> <EOL> UNSIGNED_64BIT_INT_MAX_VALUE = <NUM_LIT:2> ** <NUM_LIT:64> - <NUM_LIT:1> <EOL> __all__ = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> hex_re = re . compile ( r"<STR_LIT>" ) <EOL> signed_integer_engines = [ <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" <EOL> ] <EOL> def _using_signed_storage ( ) : <EOL> return connection . settings_dict [ "<STR_LIT>" ] in signed_integer_engines <EOL> def _signed_to_unsigned_integer ( value ) : <EOL> return struct . unpack ( "<STR_LIT>" , struct . pack ( "<STR_LIT:q>" , value ) ) [ <NUM_LIT:0> ] <EOL> def _unsigned_to_signed_integer ( value ) : <EOL> return struct . unpack ( "<STR_LIT:q>" , struct . pack ( "<STR_LIT>" , value ) ) [ <NUM_LIT:0> ] <EOL> def _hex_string_to_unsigned_integer ( value ) : <EOL> return int ( value , <NUM_LIT:16> ) <EOL> def _unsigned_integer_to_hex_string ( value ) : <EOL> return hex ( value ) . rstrip ( "<STR_LIT:L>" ) <EOL> class HexadecimalField ( forms . CharField ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . default_validators = [ RegexValidator ( hex_re , _ ( "<STR_LIT>" ) , "<STR_LIT>" ) ] <EOL> super ( HexadecimalField , self ) . __init__ ( * args , ** kwargs ) <EOL> def prepare_value ( self , value ) : <EOL> if value and not isinstance ( value , six . string_types ) and connection . vendor in ( "<STR_LIT>" , "<STR_LIT>" ) : <EOL> value = _unsigned_integer_to_hex_string ( value ) <EOL> return super ( forms . CharField , self ) . prepare_value ( value ) <EOL> class HexIntegerField ( models . BigIntegerField ) : <EOL> """<STR_LIT>""" <EOL> validators = [ <EOL> MinValueValidator ( UNSIGNED_64BIT_INT_MIN_VALUE ) , <EOL> MaxValueValidator ( UNSIGNED_64BIT_INT_MAX_VALUE ) <EOL> ] <EOL> def db_type ( self , connection ) : <EOL> engine = connection . settings_dict [ "<STR_LIT>" ] <EOL> if "<STR_LIT>" in engine : <EOL> return "<STR_LIT>" <EOL> elif "<STR_LIT>" in engine : <EOL> return "<STR_LIT>" <EOL> else : <EOL> return super ( HexIntegerField , self ) . db_type ( connection = connection ) <EOL> def get_prep_value ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if value is None or value == "<STR_LIT>" : <EOL> return None <EOL> if isinstance ( value , six . string_types ) : <EOL> value = _hex_string_to_unsigned_integer ( value ) <EOL> if _using_signed_storage ( ) : <EOL> value = _unsigned_to_signed_integer ( value ) <EOL> return value <EOL> def from_db_value ( self , value , expression , connection , context ) : <EOL> """<STR_LIT>""" <EOL> if value is None : <EOL> return value <EOL> if _using_signed_storage ( ) : <EOL> value = _signed_to_unsigned_integer ( value ) <EOL> return value <EOL> def to_python ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( value , six . string_types ) : <EOL> return value <EOL> if value is None : <EOL> return value <EOL> return _unsigned_integer_to_hex_string ( value ) <EOL> def formfield ( self , ** kwargs ) : <EOL> defaults = { "<STR_LIT>" : HexadecimalField } <EOL> defaults . update ( kwargs ) <EOL> return super ( models . IntegerField , self ) . formfield ( ** defaults ) <EOL> def run_validators ( self , value ) : <EOL> value = _hex_string_to_unsigned_integer ( value ) <EOL> return super ( models . BigIntegerField , self ) . run_validators ( value ) </s>
