app/soc/logic/models/base.py
changeset 723 69e5130e4a0a
parent 689 46db8c4bbffc
child 961 36a90d0e9211
equal deleted inserted replaced
722:a59eaa177562 723:69e5130e4a0a
    23   '"Lennard de Rijk" <ljvderijk@gmail.com>',
    23   '"Lennard de Rijk" <ljvderijk@gmail.com>',
    24   '"Pawel Solyga" <pawel.solyga@gmail.com>',
    24   '"Pawel Solyga" <pawel.solyga@gmail.com>',
    25   ]
    25   ]
    26 
    26 
    27 
    27 
       
    28 import itertools
       
    29 
    28 from google.appengine.ext import db
    30 from google.appengine.ext import db
    29 
    31 
    30 from django.utils.translation import ugettext_lazy
    32 from django.utils.translation import ugettext_lazy
    31 
    33 
    32 from soc.logic import dicts
    34 from soc.logic import dicts
   302     """
   304     """
   303 
   305 
   304     query = self._model.all()
   306     query = self._model.all()
   305     return query.fetch(limit, offset)
   307     return query.fetch(limit, offset)
   306 
   308 
   307   def getForFields(self, properties, unique=False, limit=1000, offset=0):
   309   def getForFields(self, filter, unique=False):
   308     """Returns all entities that have the specified properties.
   310     """Returns all entities that have the specified properties.
   309 
   311 
   310     Args:
   312     Args:
   311       properties: the properties that the entity should have
   313       properties: the properties that the entity should have
   312       unique: if set, only the first item from the resultset will be returned
   314       unique: if set, only the first item from the resultset will be returned
   313       limit: max amount of entities to return
   315     """
   314       offset: optional number of results to skip first; default zero.
   316 
   315     """
   317     if not filter:
   316 
       
   317     if not properties:
       
   318       raise Error("Properties did not contain any values")
   318       raise Error("Properties did not contain any values")
   319 
   319 
   320     format_text = '%(key)s = :%(key)s'
   320     queries = dicts.split(filter)
   321     msg_pairs = [format_text % {'key': key} for key in properties.iterkeys()]
   321 
   322     joined_pairs = ' AND '.join(msg_pairs)
   322     def toQuery(filter):
   323     condition = 'WHERE %s' % joined_pairs
   323       q = db.Query(self._model)
   324 
   324       for key, value in filter.iteritems():
   325     query = self._model.gql(condition, **properties)
   325         q.filter(key, value)
       
   326       return q
       
   327 
       
   328     result = itertools.chain(*[toQuery(x) for x in queries])
   326 
   329 
   327     if unique:
   330     if unique:
   328       return query.get()
   331       # Return the first item, we need the loop as itertools.chain
   329 
   332       # returns an iterable rather than a list
   330     return query.fetch(limit, offset)
   333       for item in result:
       
   334         return item
       
   335 
       
   336       # In the case result is empty, return None
       
   337       return None
       
   338 
       
   339     return result
   331 
   340 
   332   def updateModelProperties(self, model, model_properties):
   341   def updateModelProperties(self, model, model_properties):
   333     """Update existing model entity using supplied model properties.
   342     """Update existing model entity using supplied model properties.
   334 
   343 
   335     Args:
   344     Args: