<s> """<STR_LIT>""" <EOL> import weakref <EOL> from django . utils . copycompat import deepcopy <EOL> from django . utils import tree <EOL> from django . utils . datastructures import SortedDict <EOL> class CyclicDependency ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class InvalidQuery ( Exception ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class CollectedObjects ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , previously_seen = None ) : <EOL> self . data = { } <EOL> self . children = { } <EOL> if previously_seen : <EOL> self . blocked = previously_seen . blocked <EOL> for cls , seen in previously_seen . data . items ( ) : <EOL> self . blocked . setdefault ( cls , SortedDict ( ) ) . update ( seen ) <EOL> else : <EOL> self . blocked = { } <EOL> def add ( self , model , pk , obj , parent_model , parent_obj = None , nullable = False ) : <EOL> """<STR_LIT>""" <EOL> if pk in self . blocked . get ( model , { } ) : <EOL> return True <EOL> d = self . data . setdefault ( model , SortedDict ( ) ) <EOL> retval = pk in d <EOL> d [ pk ] = obj <EOL> if parent_model is not None and not nullable : <EOL> self . children . setdefault ( parent_model , [ ] ) . append ( model ) <EOL> return retval <EOL> def __contains__ ( self , key ) : <EOL> return self . data . __contains__ ( key ) <EOL> def __getitem__ ( self , key ) : <EOL> return self . data [ key ] <EOL> def __nonzero__ ( self ) : <EOL> return bool ( self . data ) <EOL> def iteritems ( self ) : <EOL> for k in self . ordered_keys ( ) : <EOL> yield k , self [ k ] <EOL> def items ( self ) : <EOL> return list ( self . iteritems ( ) ) <EOL> def keys ( self ) : <EOL> return self . ordered_keys ( ) <EOL> def ordered_keys ( self ) : <EOL> """<STR_LIT>""" <EOL> dealt_with = SortedDict ( ) <EOL> models = self . data . keys ( ) <EOL> while len ( dealt_with ) < len ( models ) : <EOL> found = False <EOL> for model in models : <EOL> if model in dealt_with : <EOL> continue <EOL> children = self . children . setdefault ( model , [ ] ) <EOL> if len ( [ c for c in children if c not in dealt_with ] ) == <NUM_LIT:0> : <EOL> dealt_with [ model ] = None <EOL> found = True <EOL> if not found : <EOL> raise CyclicDependency ( <EOL> "<STR_LIT>" ) <EOL> return dealt_with . keys ( ) <EOL> def unordered_keys ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . data . keys ( ) <EOL> class QueryWrapper ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , sql , params ) : <EOL> self . data = sql , params <EOL> def as_sql ( self , qn = None , connection = None ) : <EOL> return self . data <EOL> class Q ( tree . Node ) : <EOL> """<STR_LIT>""" <EOL> AND = '<STR_LIT>' <EOL> OR = '<STR_LIT>' <EOL> default = AND <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( Q , self ) . __init__ ( children = list ( args ) + kwargs . items ( ) ) <EOL> def _combine ( self , other , conn ) : <EOL> if not isinstance ( other , Q ) : <EOL> raise TypeError ( other ) <EOL> obj = type ( self ) ( ) <EOL> obj . add ( self , conn ) <EOL> obj . add ( other , conn ) <EOL> return obj <EOL> def __or__ ( self , other ) : <EOL> return self . _combine ( other , self . OR ) <EOL> def __and__ ( self , other ) : <EOL> return self . _combine ( other , self . AND ) <EOL> def __invert__ ( self ) : <EOL> obj = type ( self ) ( ) <EOL> obj . add ( self , self . AND ) <EOL> obj . negate ( ) <EOL> return obj <EOL> class DeferredAttribute ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , field_name , model ) : <EOL> self . field_name = field_name <EOL> self . model_ref = weakref . ref ( model ) <EOL> self . loaded = False <EOL> def __get__ ( self , instance , owner ) : <EOL> """<STR_LIT>""" <EOL> from django . db . models . fields import FieldDoesNotExist <EOL> assert instance is not None <EOL> cls = self . model_ref ( ) <EOL> data = instance . __dict__ <EOL> if data . get ( self . field_name , self ) is self : <EOL> try : <EOL> cls . _meta . get_field_by_name ( self . field_name ) <EOL> name = self . field_name <EOL> except FieldDoesNotExist : <EOL> name = [ f . name for f in cls . _meta . fields <EOL> if f . attname == self . field_name ] [ <NUM_LIT:0> ] <EOL> val = getattr ( <EOL> cls . _base_manager . filter ( pk = instance . pk ) . only ( name ) . using ( <EOL> instance . _state . db ) . get ( ) , <EOL> self . field_name <EOL> ) <EOL> data [ self . field_name ] = val <EOL> return data [ self . field_name ] <EOL> def __set__ ( self , instance , value ) : <EOL> """<STR_LIT>""" <EOL> instance . __dict__ [ self . field_name ] = value <EOL> def select_related_descend ( field , restricted , requested , reverse = False ) : <EOL> """<STR_LIT>""" <EOL> if not field . rel : <EOL> return False <EOL> if field . rel . parent_link and not reverse : <EOL> return False <EOL> if restricted : <EOL> if reverse and field . related_query_name ( ) not in requested : <EOL> return False <EOL> if not reverse and field . name not in requested : <EOL> return False <EOL> if not restricted and field . null : <EOL> return False <EOL> return True <EOL> def deferred_class_factory ( model , attrs ) : <EOL> """<STR_LIT>""" <EOL> class Meta : <EOL> pass <EOL> setattr ( Meta , "<STR_LIT>" , True ) <EOL> setattr ( Meta , "<STR_LIT>" , model . _meta . app_label ) <EOL> name = "<STR_LIT>" % ( model . __name__ , '<STR_LIT:_>' . join ( sorted ( list ( attrs ) ) ) ) <EOL> overrides = dict ( [ ( attr , DeferredAttribute ( attr , model ) ) <EOL> for attr in attrs ] ) <EOL> overrides [ "<STR_LIT:Meta>" ] = Meta <EOL> overrides [ "<STR_LIT>" ] = model . __module__ <EOL> overrides [ "<STR_LIT>" ] = True <EOL> return type ( name , ( model , ) , overrides ) <EOL> deferred_class_factory . __safe_for_unpickling__ = True </s>
