app/soc/logic/models/base.py
changeset 2756 054810192277
parent 2736 8f3935f0f4ba
child 2757 7d0d0ce76bd9
equal deleted inserted replaced
2755:e8b599ba7b37 2756:054810192277
    16 
    16 
    17 """Helpers functions for updating different kinds of models in datastore.
    17 """Helpers functions for updating different kinds of models in datastore.
    18 """
    18 """
    19 
    19 
    20 __authors__ = [
    20 __authors__ = [
       
    21   '"Madhusudan C.S." <madhusudancs@gmail.com>',
    21   '"Todd Larsen" <tlarsen@google.com>',
    22   '"Todd Larsen" <tlarsen@google.com>',
    22   '"Sverre Rabbelier" <sverre@rabbelier.nl>',
    23   '"Sverre Rabbelier" <sverre@rabbelier.nl>',
    23   '"Lennard de Rijk" <ljvderijk@gmail.com>',
    24   '"Lennard de Rijk" <ljvderijk@gmail.com>',
    24   '"Pawel Solyga" <pawel.solyga@gmail.com>',
    25   '"Pawel Solyga" <pawel.solyga@gmail.com>',
    25   ]
    26   ]
   309       'There is no "%(name)s" where %(pairs)s.') % {
   310       'There is no "%(name)s" where %(pairs)s.') % {
   310         'name': self._name, 'pairs': joined_pairs}
   311         'name': self._name, 'pairs': joined_pairs}
   311 
   312 
   312     raise out_of_band.Error(msg, status=404)
   313     raise out_of_band.Error(msg, status=404)
   313 
   314 
   314   def getForFields(self, filter=None, unique=False,
   315   def getForFields(self, filter=None, unique=False, limit=1000, offset=0,
   315                    limit=1000, offset=0, order=None):
   316                    ancestors=None, order=None):
   316     """Returns all entities that have the specified properties.
   317     """Returns all entities that have the specified properties.
   317 
   318 
   318     Args:
   319     Args:
   319       filter: a dict for the properties that the entities should have
   320       filter: a dict for the properties that the entities should have
   320       unique: if set, only the first item from the resultset will be returned
   321       unique: if set, only the first item from the resultset will be returned
   321       limit: the amount of entities to fetch at most
   322       limit: the amount of entities to fetch at most
   322       offset: the position to start at
   323       offset: the position to start at
       
   324       ancestors: list of ancestors properties to set for this query
   323       order: a list with the sort order
   325       order: a list with the sort order
   324     """
   326     """
   325 
   327 
   326     if unique:
   328     if unique:
   327       limit = 1
   329       limit = 1
   328 
   330 
   329     query = self.getQueryForFields(filter=filter, order=order)
   331     query = self.getQueryForFields(filter=filter, 
       
   332                                    ancestors=ancestors, order=order)
   330 
   333 
   331     try:
   334     try:
   332       result = query.fetch(limit, offset)
   335       result = query.fetch(limit, offset)
   333     except db.NeedIndexError, exception:
   336     except db.NeedIndexError, exception:
   334       result = []
   337       result = []
   335       logging.exception("%s, model: %s filter: %s, order: %s" % 
   338       logging.exception("%s, model: %s filter: %s, ancestor: %s, order: %s" % 
   336                         (exception, self._model, filter, order))
   339                         (exception, self._model, filter, ancestor, order))
   337       # TODO: send email
   340       # TODO: send email
   338 
   341 
   339     if unique:
   342     if unique:
   340       return result[0] if result else None
   343       return result[0] if result else None
   341 
   344 
   342     return result
   345     return result
   343 
   346 
   344   def getQueryForFields(self, filter=None, order=None):
   347   def getQueryForFields(self, filter=None, ancestors=None, order=None):
   345     """Returns a query with the specified properties.
   348     """Returns a query with the specified properties.
   346 
   349 
   347     Args:
   350     Args:
   348       filter: a dict for the properties that the entities should have
   351       filter: a dict for the properties that the entities should have
       
   352       ancestors: list with ancestor properties to set for this query
   349       order: a list with the sort order
   353       order: a list with the sort order
   350 
   354 
   351     Returns:
   355     Returns:
   352       - Query object instantiated with the given properties
   356       - Query object instantiated with the given properties
   353     """
   357     """
   354 
   358 
   355     if not filter:
   359     if not filter:
   356       filter = {}
   360       filter = {}
       
   361 
       
   362     if not ancestors:
       
   363       ancestors = []
   357 
   364 
   358     if not order:
   365     if not order:
   359       order = []
   366       order = []
   360 
   367 
   361     orderset = set([i.strip('-') for i in order])
   368     orderset = set([i.strip('-') for i in order])
   370       if isinstance(value, list):
   377       if isinstance(value, list):
   371         op = '%s IN' % key
   378         op = '%s IN' % key
   372         query.filter(op, value)
   379         query.filter(op, value)
   373       else:
   380       else:
   374         query.filter(key, value)
   381         query.filter(key, value)
       
   382 
       
   383     for ancestor in ancestors:
       
   384       query.ancestor(ancestor)
   375 
   385 
   376     for key in order:
   386     for key in order:
   377       query.order(key)
   387       query.order(key)
   378 
   388 
   379     return query
   389     return query