<s> import copy <EOL> from django . db . models . query import QuerySet , EmptyQuerySet , insert_query <EOL> from django . db . models import signals <EOL> from django . db . models . fields import FieldDoesNotExist <EOL> def ensure_default_manager ( sender , ** kwargs ) : <EOL> cls = sender <EOL> if not getattr ( cls , '<STR_LIT>' , None ) and not cls . _meta . abstract : <EOL> try : <EOL> cls . _meta . get_field ( '<STR_LIT>' ) <EOL> raise ValueError , "<STR_LIT>" % cls . __name__ <EOL> except FieldDoesNotExist : <EOL> pass <EOL> cls . add_to_class ( '<STR_LIT>' , Manager ( ) ) <EOL> signals . class_prepared . connect ( ensure_default_manager ) <EOL> class Manager ( object ) : <EOL> creation_counter = <NUM_LIT:0> <EOL> def __init__ ( self ) : <EOL> super ( Manager , self ) . __init__ ( ) <EOL> self . _set_creation_counter ( ) <EOL> self . model = None <EOL> self . _inherited = False <EOL> def contribute_to_class ( self , model , name ) : <EOL> self . model = model <EOL> setattr ( model , name , ManagerDescriptor ( self ) ) <EOL> if not getattr ( model , '<STR_LIT>' , None ) or self . creation_counter < model . _default_manager . creation_counter : <EOL> model . _default_manager = self <EOL> if model . _meta . abstract or self . _inherited : <EOL> model . _meta . abstract_managers . append ( ( self . creation_counter , name , <EOL> self ) ) <EOL> def _set_creation_counter ( self ) : <EOL> """<STR_LIT>""" <EOL> self . creation_counter = Manager . creation_counter <EOL> Manager . creation_counter += <NUM_LIT:1> <EOL> def _copy_to_model ( self , model ) : <EOL> """<STR_LIT>""" <EOL> assert issubclass ( model , self . model ) <EOL> mgr = copy . copy ( self ) <EOL> mgr . _set_creation_counter ( ) <EOL> mgr . model = model <EOL> mgr . _inherited = True <EOL> return mgr <EOL> def get_empty_query_set ( self ) : <EOL> return EmptyQuerySet ( self . model ) <EOL> def get_query_set ( self ) : <EOL> """<STR_LIT>""" <EOL> return QuerySet ( self . model ) <EOL> def none ( self ) : <EOL> return self . get_empty_query_set ( ) <EOL> def all ( self ) : <EOL> return self . get_query_set ( ) <EOL> def count ( self ) : <EOL> return self . get_query_set ( ) . count ( ) <EOL> def dates ( self , * args , ** kwargs ) : <EOL> return self . get_query_set ( ) . dates ( * args , ** kwargs ) <EOL> def distinct ( self , * args , ** kwargs ) : <EOL> return self . get_query_set ( ) . distinct ( * args , ** kwargs ) <EOL> def extra ( self , * args , ** kwargs ) : <EOL> return self . get_query_set ( ) . extra ( * args , ** kwargs ) <EOL> def get ( self , * args , ** kwargs ) : <EOL> return self . get_query_set ( ) . get ( * args , ** kwargs ) <EOL> def get_or_create ( self , ** kwargs ) : <EOL> return self . get_query_set ( ) . get_or_create ( ** kwargs ) <EOL> def create ( self , ** kwargs ) : <EOL> return self . get_query_set ( ) . create ( ** kwargs ) <EOL> def filter ( self , * args , ** kwargs ) : <EOL> return self . get_query_set ( ) . filter ( * args , ** kwargs ) <EOL> def complex_filter ( self , * args , ** kwargs ) : <EOL> return self . get_query_set ( ) . complex_filter ( * args , ** kwargs ) <EOL> def exclude ( self , * args , ** kwargs ) : <EOL> return self . get_query_set ( ) . exclude ( * args , ** kwargs ) <EOL> def in_bulk ( self , * args , ** kwargs ) : <EOL> return self . get_query_set ( ) . in_bulk ( * args , ** kwargs ) <EOL> def iterator ( self , * args , ** kwargs ) : <EOL> return self . get_query_set ( ) . iterator ( * args , ** kwargs ) <EOL> def latest ( self , * args , ** kwargs ) : <EOL> return self . get_query_set ( ) . latest ( * args , ** kwargs ) <EOL> def order_by ( self , * args , ** kwargs ) : <EOL> return self . get_query_set ( ) . order_by ( * args , ** kwargs ) <EOL> def select_related ( self , * args , ** kwargs ) : <EOL> return self . get_query_set ( ) . select_related ( * args , ** kwargs ) <EOL> def values ( self , * args , ** kwargs ) : <EOL> return self . get_query_set ( ) . values ( * args , ** kwargs ) <EOL> def values_list ( self , * args , ** kwargs ) : <EOL> return self . get_query_set ( ) . values_list ( * args , ** kwargs ) <EOL> def update ( self , * args , ** kwargs ) : <EOL> return self . get_query_set ( ) . update ( * args , ** kwargs ) <EOL> def reverse ( self , * args , ** kwargs ) : <EOL> return self . get_query_set ( ) . reverse ( * args , ** kwargs ) <EOL> def _insert ( self , values , ** kwargs ) : <EOL> return insert_query ( self . model , values , ** kwargs ) <EOL> def _update ( self , values , ** kwargs ) : <EOL> return self . get_query_set ( ) . _update ( values , ** kwargs ) <EOL> class ManagerDescriptor ( object ) : <EOL> def __init__ ( self , manager ) : <EOL> self . manager = manager <EOL> def __get__ ( self , instance , type = None ) : <EOL> if instance != None : <EOL> raise AttributeError , "<STR_LIT>" % type . __name__ <EOL> return self . manager <EOL> class EmptyManager ( Manager ) : <EOL> def get_query_set ( self ) : <EOL> return self . get_empty_query_set ( ) </s>
