<s> from datetime import datetime <EOL> from decimal import Decimal <EOL> from itertools import chain <EOL> import warnings <EOL> from django . apps import apps <EOL> from django . conf import settings <EOL> from django . db . backends . utils import format_number <EOL> from django . db import IntegrityError <EOL> from django . db . models . query import QuerySet <EOL> from django . utils import timezone <EOL> from google . appengine . api import datastore <EOL> from google . appengine . api . datastore import Key , Query <EOL> try : <EOL> from django . db . models . expressions import BaseExpression <EOL> except ImportError : <EOL> from django . db . models . expressions import ExpressionNode as BaseExpression <EOL> from djangae . utils import memoized <EOL> from djangae . db . backends . appengine . indexing import special_indexes_for_column , REQUIRES_SPECIAL_INDEXES <EOL> from djangae . db . backends . appengine . dbapi import CouldBeSupportedError <EOL> from djangae . db . backends . appengine import POLYMODEL_CLASS_ATTRIBUTE <EOL> def make_timezone_naive ( value ) : <EOL> if value is None : <EOL> return None <EOL> if timezone . is_aware ( value ) : <EOL> if settings . USE_TZ : <EOL> value = value . astimezone ( timezone . utc ) . replace ( tzinfo = None ) <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> return value <EOL> @ memoized <EOL> def get_model_from_db_table ( db_table ) : <EOL> kwargs = { <EOL> "<STR_LIT>" : True , <EOL> "<STR_LIT>" : True <EOL> } <EOL> for model in apps . get_models ( ** kwargs ) : <EOL> if model . _meta . db_table == db_table : <EOL> return model <EOL> def decimal_to_string ( value , max_digits = <NUM_LIT:16> , decimal_places = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> if value . is_signed ( ) : <EOL> sign = u'<STR_LIT:->' <EOL> value = abs ( value ) <EOL> else : <EOL> sign = u'<STR_LIT>' <EOL> value = format_number ( value , max_digits , decimal_places ) <EOL> n = value . find ( '<STR_LIT:.>' ) <EOL> if n < <NUM_LIT:0> : <EOL> n = len ( value ) <EOL> if n < max_digits - decimal_places : <EOL> value = u'<STR_LIT:0>' * ( max_digits - decimal_places - n ) + value <EOL> return sign + value <EOL> def normalise_field_value ( value ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( value , datetime ) : <EOL> return make_timezone_naive ( value ) <EOL> elif isinstance ( value , Decimal ) : <EOL> return decimal_to_string ( value ) <EOL> return value <EOL> def get_datastore_kind ( model ) : <EOL> return get_top_concrete_parent ( model ) . _meta . db_table <EOL> def get_prepared_db_value ( connection , instance , field , raw = False ) : <EOL> value = getattr ( instance , field . attname ) if raw else field . pre_save ( instance , instance . _state . adding ) <EOL> if isinstance ( value , BaseExpression ) : <EOL> from djangae . db . backends . appengine . expressions import evaluate_expression <EOL> value = evaluate_expression ( value , instance , connection ) <EOL> if hasattr ( value , "<STR_LIT>" ) : <EOL> value = value . prepare_database_save ( field ) <EOL> else : <EOL> value = field . get_db_prep_save ( <EOL> value , <EOL> connection = connection <EOL> ) <EOL> value = connection . ops . value_for_db ( value , field ) <EOL> return value <EOL> def get_concrete_parents ( model , ignore_leaf = False ) : <EOL> ret = [ x for x in model . mro ( ) if hasattr ( x , "<STR_LIT>" ) and not x . _meta . abstract and not x . _meta . proxy ] <EOL> if ignore_leaf : <EOL> ret = [ x for x in ret if x != model ] <EOL> return ret <EOL> @ memoized <EOL> def get_top_concrete_parent ( model ) : <EOL> return get_concrete_parents ( model ) [ - <NUM_LIT:1> ] <EOL> def get_concrete_fields ( model , ignore_leaf = False ) : <EOL> """<STR_LIT>""" <EOL> concrete_classes = get_concrete_parents ( model , ignore_leaf ) <EOL> fields = [ ] <EOL> for klass in concrete_classes : <EOL> fields . extend ( klass . _meta . fields ) <EOL> return fields <EOL> @ memoized <EOL> def get_concrete_db_tables ( model ) : <EOL> return [ x . _meta . db_table for x in get_concrete_parents ( model ) ] <EOL> @ memoized <EOL> def has_concrete_parents ( model ) : <EOL> return get_concrete_parents ( model ) != [ model ] <EOL> @ memoized <EOL> def get_field_from_column ( model , column ) : <EOL> for field in model . _meta . fields : <EOL> if field . column == column : <EOL> return field <EOL> return None <EOL> def django_instance_to_entity ( connection , model , fields , raw , instance , check_null = True ) : <EOL> inheritance_root = get_top_concrete_parent ( model ) <EOL> db_table = get_datastore_kind ( inheritance_root ) <EOL> def value_from_instance ( _instance , _field ) : <EOL> value = get_prepared_db_value ( connection , _instance , _field , raw ) <EOL> if value is None and _field . has_default ( ) and not _field . null : <EOL> value = connection . ops . value_for_db ( _field . get_default ( ) , _field ) <EOL> if check_null and ( not _field . null and not _field . primary_key ) and value is None : <EOL> raise IntegrityError ( "<STR_LIT>" <EOL> "<STR_LIT>" % _field . name ) <EOL> is_primary_key = False <EOL> if _field . primary_key and _field . model == inheritance_root : <EOL> is_primary_key = True <EOL> return value , is_primary_key <EOL> field_values = { } <EOL> primary_key = None <EOL> for field in fields : <EOL> value , is_primary_key = value_from_instance ( instance , field ) <EOL> if is_primary_key : <EOL> primary_key = value <EOL> else : <EOL> field_values [ field . column ] = value <EOL> for index in special_indexes_for_column ( model , field . column ) : <EOL> indexer = REQUIRES_SPECIAL_INDEXES [ index . split ( '<STR_LIT>' ) [ <NUM_LIT:0> ] ] <EOL> values = indexer . prep_value_for_database ( value , index ) <EOL> if values is None : <EOL> continue <EOL> if not hasattr ( values , "<STR_LIT>" ) : <EOL> values = [ values ] <EOL> for v in values : <EOL> column = indexer . indexed_column_name ( field . column , v , index ) <EOL> if column in field_values : <EOL> if not isinstance ( field_values [ column ] , list ) : <EOL> field_values [ column ] = [ field_values [ column ] , v ] <EOL> else : <EOL> field_values [ column ] . append ( v ) <EOL> else : <EOL> field_values [ column ] = v <EOL> kwargs = { } <EOL> if primary_key : <EOL> if isinstance ( primary_key , ( int , long ) ) : <EOL> kwargs [ "<STR_LIT:id>" ] = primary_key <EOL> elif isinstance ( primary_key , basestring ) : <EOL> if len ( primary_key ) > <NUM_LIT> : <EOL> warnings . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> RuntimeWarning ) <EOL> primary_key = primary_key [ : <NUM_LIT> ] <EOL> kwargs [ "<STR_LIT:name>" ] = primary_key <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> namespace = connection . settings_dict . get ( "<STR_LIT>" ) <EOL> entity = datastore . Entity ( db_table , namespace = namespace , ** kwargs ) <EOL> entity . update ( field_values ) <EOL> classes = get_concrete_db_tables ( model ) <EOL> if len ( classes ) > <NUM_LIT:1> : <EOL> entity [ POLYMODEL_CLASS_ATTRIBUTE ] = list ( set ( classes ) ) <EOL> return entity <EOL> def get_datastore_key ( model , pk , namespace ) : <EOL> """<STR_LIT>""" <EOL> kind = get_top_concrete_parent ( model ) . _meta . db_table <EOL> return Key . from_path ( kind , pk , namespace = namespace ) <EOL> class MockInstance ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , ** kwargs ) : <EOL> is_adding = kwargs . pop ( '<STR_LIT>' , False ) <EOL> self . _original = kwargs . pop ( '<STR_LIT>' , None ) <EOL> self . _meta = kwargs . pop ( '<STR_LIT>' , None ) <EOL> class State : <EOL> adding = is_adding <EOL> self . fields = { } <EOL> for field_name , value in kwargs . items ( ) : <EOL> self . fields [ field_name ] = value <EOL> self . _state = State ( ) <EOL> def __getattr__ ( self , attr ) : <EOL> if attr in self . fields : <EOL> return self . fields [ attr ] <EOL> raise AttributeError ( attr ) <EOL> def key_exists ( key ) : <EOL> qry = Query ( keys_only = True , namespace = key . namespace ( ) ) <EOL> qry . Ancestor ( key ) <EOL> return qry . Count ( limit = <NUM_LIT:1> ) > <NUM_LIT:0> <EOL> def lt ( x , y ) : <EOL> if x is None and y is not None : <EOL> return True <EOL> elif x is not None and y is None : <EOL> return False <EOL> else : <EOL> return x < y <EOL> def gt ( x , y ) : <EOL> if x is None and y is not None : <EOL> return False <EOL> elif x is not None and y is None : <EOL> return True <EOL> else : <EOL> return x > y <EOL> def gte ( x , y ) : <EOL> return not lt ( x , y ) <EOL> def lte ( x , y ) : <EOL> return not gt ( x , y ) <EOL> def django_ordering_comparison ( ordering , lhs , rhs ) : <EOL> if not ordering : <EOL> return - <NUM_LIT:1> <EOL> ASCENDING = <NUM_LIT:1> <EOL> DESCENDING = <NUM_LIT:2> <EOL> for order , direction in ordering : <EOL> if lhs is not None : <EOL> lhs_value = lhs . key ( ) if order == "<STR_LIT>" else lhs . get ( order ) <EOL> else : <EOL> lhs_value = None <EOL> if rhs is not None : <EOL> rhs_value = rhs . key ( ) if order == "<STR_LIT>" else rhs . get ( order ) <EOL> else : <EOL> rhs_value = None <EOL> if direction == ASCENDING and lhs_value != rhs_value : <EOL> return - <NUM_LIT:1> if lt ( lhs_value , rhs_value ) else <NUM_LIT:1> <EOL> elif direction == DESCENDING and lhs_value != rhs_value : <EOL> return <NUM_LIT:1> if lt ( lhs_value , rhs_value ) else - <NUM_LIT:1> <EOL> return <NUM_LIT:0> <EOL> def entity_matches_query ( entity , query ) : <EOL> """<STR_LIT>""" <EOL> OPERATORS = { <EOL> "<STR_LIT:=>" : lambda x , y : x == y , <EOL> "<STR_LIT:<>" : lt , <EOL> "<STR_LIT:>>" : gt , <EOL> "<STR_LIT>" : lte , <EOL> "<STR_LIT>" : gte <EOL> } <EOL> queries = [ query ] <EOL> if isinstance ( query , datastore . MultiQuery ) : <EOL> raise CouldBeSupportedError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> for query in queries : <EOL> comparisons = chain ( <EOL> [ ( "<STR_LIT>" , "<STR_LIT:=>" , "<STR_LIT>" ) ] , <EOL> [ tuple ( x . split ( "<STR_LIT:U+0020>" ) + [ x ] ) for x in query . keys ( ) ] <EOL> ) <EOL> for ent_attr , op , query_attr in comparisons : <EOL> if ent_attr == "<STR_LIT>" : <EOL> continue <EOL> op = OPERATORS [ op ] <EOL> if ent_attr == "<STR_LIT>" : <EOL> ent_attr = entity . kind ( ) <EOL> else : <EOL> ent_attr = entity . get ( ent_attr ) <EOL> if callable ( ent_attr ) : <EOL> ent_attr = ent_attr ( ) <EOL> if not isinstance ( query_attr , ( list , tuple ) ) : <EOL> query_attrs = [ query_attr ] <EOL> else : <EOL> query_attrs = query_attr <EOL> query_attrs = ( <EOL> getattr ( query , x ) if x == "<STR_LIT>" else query . get ( x ) <EOL> for x in query_attrs <EOL> ) <EOL> if not isinstance ( ent_attr , ( list , tuple ) ) : <EOL> ent_attr = [ ent_attr ] <EOL> matches = False <EOL> for query_attr in query_attrs : <EOL> if not any ( op ( attr , query_attr ) for attr in ent_attr ) : <EOL> matches = False <EOL> break <EOL> else : <EOL> matches = True <EOL> if not matches : <EOL> break <EOL> else : <EOL> return True <EOL> return False </s>
