<s> from djangoappengine . db . utils import get_cursor , set_cursor , set_config <EOL> from google . appengine . api . datastore import Key <EOL> from mapreduce . datastore_range_iterators import AbstractKeyRangeIterator , _KEY_RANGE_ITERATORS <EOL> from mapreduce . input_readers import AbstractDatastoreInputReader , _get_params , BadReaderParamsError <EOL> from mapreduce import util <EOL> class DjangoModelIterator ( AbstractKeyRangeIterator ) : <EOL> def __iter__ ( self ) : <EOL> k_range = self . _key_range <EOL> if k_range . namespace : <EOL> return <EOL> model_class = util . for_name ( self . _query_spec . model_class_path ) <EOL> q = model_class . objects . all ( ) <EOL> if k_range . key_start : <EOL> if k_range . include_start : <EOL> q = q . filter ( pk__gte = k_range . key_start . id_or_name ( ) ) <EOL> else : <EOL> q = q . filter ( pk__gt = k_range . key_start . id_or_name ( ) ) <EOL> if k_range . key_end : <EOL> if k_range . include_end : <EOL> q = q . filter ( pk__lte = k_range . key_end . id_or_name ( ) ) <EOL> else : <EOL> q = q . filter ( pk__lt = k_range . key_end . id_or_name ( ) ) <EOL> filters = self . _query_spec . filters <EOL> if filters : <EOL> q = q . filter ( ** dict ( [ [ filter [ <NUM_LIT:0> ] , filter [ <NUM_LIT:2> ] ] for filter in filters ] ) ) <EOL> q = q . order_by ( '<STR_LIT>' ) <EOL> q = set_config ( q , batch_size = self . _query_spec . batch_size ) <EOL> if self . _cursor : <EOL> q = set_cursor ( q , self . _cursor ) <EOL> self . _query = q <EOL> for entity in self . _query . iterator ( ) : <EOL> yield entity <EOL> def _get_cursor ( self ) : <EOL> if self . _query is not None : <EOL> return get_cursor ( self . _query ) <EOL> _KEY_RANGE_ITERATORS [ DjangoModelIterator . __name__ ] = DjangoModelIterator <EOL> class DjangoModelInputReader ( AbstractDatastoreInputReader ) : <EOL> """<STR_LIT>""" <EOL> _KEY_RANGE_ITER_CLS = DjangoModelIterator <EOL> @ classmethod <EOL> def _get_raw_entity_kind ( cls , entity_kind ) : <EOL> """<STR_LIT>""" <EOL> model_class = util . for_name ( entity_kind ) <EOL> return model_class . _meta . db_table <EOL> @ classmethod <EOL> def validate ( cls , mapper_spec ) : <EOL> super ( DjangoModelInputReader , cls ) . validate ( mapper_spec ) <EOL> params = _get_params ( mapper_spec ) <EOL> if cls . NAMESPACE_PARAM in params : <EOL> raise BadReaderParamsError ( "<STR_LIT>" ) <EOL> entity_kind_name = params [ cls . ENTITY_KIND_PARAM ] <EOL> try : <EOL> util . for_name ( entity_kind_name ) <EOL> except ImportError , e : <EOL> raise BadReaderParamsError ( "<STR_LIT>" % e ) </s>
