<s> import json <EOL> from psycopg2 . extras import DateRange , DateTimeTZRange , NumericRange , Range <EOL> from django . contrib . postgres import forms , lookups <EOL> from django . db import models <EOL> from django . utils import six <EOL> from . utils import AttributeSetter <EOL> __all__ = [ <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> ] <EOL> class RangeField ( models . Field ) : <EOL> empty_strings_allowed = False <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> self . base_field = self . base_field ( ) <EOL> super ( RangeField , self ) . __init__ ( * args , ** kwargs ) <EOL> @ property <EOL> def model ( self ) : <EOL> try : <EOL> return self . __dict__ [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> raise AttributeError ( "<STR_LIT>" % self . __class__ . __name__ ) <EOL> @ model . setter <EOL> def model ( self , model ) : <EOL> self . __dict__ [ '<STR_LIT>' ] = model <EOL> self . base_field . model = model <EOL> def get_prep_value ( self , value ) : <EOL> if value is None : <EOL> return None <EOL> elif isinstance ( value , Range ) : <EOL> return value <EOL> elif isinstance ( value , ( list , tuple ) ) : <EOL> return self . range_type ( value [ <NUM_LIT:0> ] , value [ <NUM_LIT:1> ] ) <EOL> return value <EOL> def to_python ( self , value ) : <EOL> if isinstance ( value , six . string_types ) : <EOL> vals = json . loads ( value ) <EOL> for end in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> if end in vals : <EOL> vals [ end ] = self . base_field . to_python ( vals [ end ] ) <EOL> value = self . range_type ( ** vals ) <EOL> elif isinstance ( value , ( list , tuple ) ) : <EOL> value = self . range_type ( value [ <NUM_LIT:0> ] , value [ <NUM_LIT:1> ] ) <EOL> return value <EOL> def set_attributes_from_name ( self , name ) : <EOL> super ( RangeField , self ) . set_attributes_from_name ( name ) <EOL> self . base_field . set_attributes_from_name ( name ) <EOL> def value_to_string ( self , obj ) : <EOL> value = self . value_from_object ( obj ) <EOL> if value is None : <EOL> return None <EOL> if value . isempty : <EOL> return json . dumps ( { "<STR_LIT>" : True } ) <EOL> base_field = self . base_field <EOL> result = { "<STR_LIT>" : value . _bounds } <EOL> for end in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> val = getattr ( value , end ) <EOL> if val is None : <EOL> result [ end ] = None <EOL> else : <EOL> obj = AttributeSetter ( base_field . attname , val ) <EOL> result [ end ] = base_field . value_to_string ( obj ) <EOL> return json . dumps ( result ) <EOL> def formfield ( self , ** kwargs ) : <EOL> kwargs . setdefault ( '<STR_LIT>' , self . form_field ) <EOL> return super ( RangeField , self ) . formfield ( ** kwargs ) <EOL> class IntegerRangeField ( RangeField ) : <EOL> base_field = models . IntegerField <EOL> range_type = NumericRange <EOL> form_field = forms . IntegerRangeField <EOL> def db_type ( self , connection ) : <EOL> return '<STR_LIT>' <EOL> class BigIntegerRangeField ( RangeField ) : <EOL> base_field = models . BigIntegerField <EOL> range_type = NumericRange <EOL> form_field = forms . IntegerRangeField <EOL> def db_type ( self , connection ) : <EOL> return '<STR_LIT>' <EOL> class FloatRangeField ( RangeField ) : <EOL> base_field = models . FloatField <EOL> range_type = NumericRange <EOL> form_field = forms . FloatRangeField <EOL> def db_type ( self , connection ) : <EOL> return '<STR_LIT>' <EOL> class DateTimeRangeField ( RangeField ) : <EOL> base_field = models . DateTimeField <EOL> range_type = DateTimeTZRange <EOL> form_field = forms . DateTimeRangeField <EOL> def db_type ( self , connection ) : <EOL> return '<STR_LIT>' <EOL> class DateRangeField ( RangeField ) : <EOL> base_field = models . DateField <EOL> range_type = DateRange <EOL> form_field = forms . DateRangeField <EOL> def db_type ( self , connection ) : <EOL> return '<STR_LIT>' <EOL> RangeField . register_lookup ( lookups . DataContains ) <EOL> RangeField . register_lookup ( lookups . ContainedBy ) <EOL> RangeField . register_lookup ( lookups . Overlap ) <EOL> class RangeContainedBy ( models . Lookup ) : <EOL> lookup_name = '<STR_LIT>' <EOL> type_mapping = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:date>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> def as_sql ( self , qn , connection ) : <EOL> field = self . lhs . output_field <EOL> if isinstance ( field , models . FloatField ) : <EOL> sql = '<STR_LIT>' . format ( self . type_mapping [ field . db_type ( connection ) ] ) <EOL> else : <EOL> sql = '<STR_LIT>' . format ( self . type_mapping [ field . db_type ( connection ) ] ) <EOL> lhs , lhs_params = self . process_lhs ( qn , connection ) <EOL> rhs , rhs_params = self . process_rhs ( qn , connection ) <EOL> params = lhs_params + rhs_params <EOL> return sql % ( lhs , rhs ) , params <EOL> def get_prep_lookup ( self ) : <EOL> return RangeField ( ) . get_prep_lookup ( self . lookup_name , self . rhs ) <EOL> models . DateField . register_lookup ( RangeContainedBy ) <EOL> models . DateTimeField . register_lookup ( RangeContainedBy ) <EOL> models . IntegerField . register_lookup ( RangeContainedBy ) <EOL> models . BigIntegerField . register_lookup ( RangeContainedBy ) <EOL> models . FloatField . register_lookup ( RangeContainedBy ) <EOL> @ RangeField . register_lookup <EOL> class FullyLessThan ( lookups . PostgresSimpleLookup ) : <EOL> lookup_name = '<STR_LIT>' <EOL> operator = '<STR_LIT>' <EOL> @ RangeField . register_lookup <EOL> class FullGreaterThan ( lookups . PostgresSimpleLookup ) : <EOL> lookup_name = '<STR_LIT>' <EOL> operator = '<STR_LIT>' <EOL> @ RangeField . register_lookup <EOL> class NotLessThan ( lookups . PostgresSimpleLookup ) : <EOL> lookup_name = '<STR_LIT>' <EOL> operator = '<STR_LIT>' <EOL> @ RangeField . register_lookup <EOL> class NotGreaterThan ( lookups . PostgresSimpleLookup ) : <EOL> lookup_name = '<STR_LIT>' <EOL> operator = '<STR_LIT>' <EOL> @ RangeField . register_lookup <EOL> class AdjacentToLookup ( lookups . PostgresSimpleLookup ) : <EOL> lookup_name = '<STR_LIT>' <EOL> operator = '<STR_LIT>' <EOL> @ RangeField . register_lookup <EOL> class RangeStartsWith ( models . Transform ) : <EOL> lookup_name = '<STR_LIT>' <EOL> function = '<STR_LIT>' <EOL> @ property <EOL> def output_field ( self ) : <EOL> return self . lhs . output_field . base_field <EOL> @ RangeField . register_lookup <EOL> class RangeEndsWith ( models . Transform ) : <EOL> lookup_name = '<STR_LIT>' <EOL> function = '<STR_LIT>' <EOL> @ property <EOL> def output_field ( self ) : <EOL> return self . lhs . output_field . base_field <EOL> @ RangeField . register_lookup <EOL> class IsEmpty ( models . Transform ) : <EOL> lookup_name = '<STR_LIT>' <EOL> function = '<STR_LIT>' <EOL> output_field = models . BooleanField ( ) </s>
