app/soc/logic/models/base.py
changeset 1626 fe455c93cbf6
parent 1614 797f5ae462e7
child 1672 9b6cc71dd70c
equal deleted inserted replaced
1625:cd7174032b56 1626:fe455c93cbf6
    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 
       
    30 from google.appengine.ext import db
    28 from google.appengine.ext import db
    31 
    29 
    32 from django.utils.translation import ugettext
    30 from django.utils.translation import ugettext
    33 
    31 
    34 from soc.cache import sidebar
    32 from soc.cache import sidebar
    40   """Base class for all exceptions raised by this module.
    38   """Base class for all exceptions raised by this module.
    41   """
    39   """
    42 
    40 
    43   pass
    41   pass
    44 
    42 
       
    43 
    45 class InvalidArgumentError(Error):
    44 class InvalidArgumentError(Error):
    46   """Raised when an invalid argument is passed to a method.
    45   """Raised when an invalid argument is passed to a method.
    47 
    46 
    48   For example, if an argument is None, but must always be non-False.
    47   For example, if an argument is None, but must always be non-False.
    49   """
    48   """
    50 
    49 
    51   pass
    50   pass
       
    51 
    52 
    52 
    53 class NoEntityError(InvalidArgumentError):
    53 class NoEntityError(InvalidArgumentError):
    54   """Raised when no entity is passed to a method that requires one.
    54   """Raised when no entity is passed to a method that requires one.
    55   """
    55   """
    56 
    56 
   289 
   289 
   290     orderset = set([i.strip('-') for i in order])
   290     orderset = set([i.strip('-') for i in order])
   291     if len(orderset) != len(order):
   291     if len(orderset) != len(order):
   292       raise InvalidArgumentError
   292       raise InvalidArgumentError
   293 
   293 
   294     q = db.Query(self._model)
   294     query = db.Query(self._model)
   295 
   295 
   296     for key, value in filter.iteritems():
   296     for key, value in filter.iteritems():
   297       if isinstance(value, list):
   297       if isinstance(value, list):
   298         op = '%s IN' % key
   298         op = '%s IN' % key
   299         q.filter(op, value)
   299         query.filter(op, value)
   300       else:
   300       else:
   301         q.filter(key, value)
   301         query.filter(key, value)
   302 
   302 
   303     for key in order:
   303     for key in order:
   304       q.order(key)
   304       query.order(key)
   305 
   305 
   306     result = q.fetch(limit, offset)
   306     result = query.fetch(limit, offset)
   307 
   307 
   308     if unique:
   308     if unique:
   309       return result[0] if result else None
   309       return result[0] if result else None
   310 
   310 
   311     return result
   311     return result
   312 
   312 
   313   def updateEntityProperties(self, entity, entity_properties, silent=False):
   313   def updateEntityProperties(self, entity, entity_properties, silent=False):
   314     """Update existing entity using supplied properties.
   314     """Update existing entity using supplied properties.
   315 
   315 
   316     Args:
   316     Args:
   317       model: a model entity
   317       entity: a model entity
   318       model_properties: keyword arguments that correspond to entity
   318       entity_properties: keyword arguments that correspond to entity
   319         properties and their values
   319         properties and their values
   320       silent: iff True does not call _onUpdate method
   320       silent: iff True does not call _onUpdate method
   321 
   321 
   322     Returns:
   322     Returns:
   323       The original entity with any supplied properties changed.
   323       The original entity with any supplied properties changed.
   435 
   435 
   436     Base classes should override if any special actions need to be
   436     Base classes should override if any special actions need to be
   437     taken when a field is created.
   437     taken when a field is created.
   438 
   438 
   439     Args:
   439     Args:
       
   440       entity_properties: keyword arguments that correspond to entity
       
   441         properties and their values
   440       name: the name of the field to be created
   442       name: the name of the field to be created
   441       value: the value
       
   442     """
   443     """
   443 
   444 
   444     if not entity_properties or (name not in entity_properties):
   445     if not entity_properties or (name not in entity_properties):
   445       raise InvalidArgumentError
   446       raise InvalidArgumentError
   446 
   447 
   451     taken when a field is updated. The field is not updated if the
   452     taken when a field is updated. The field is not updated if the
   452     method does not return a True value.
   453     method does not return a True value.
   453 
   454 
   454     Args:
   455     Args:
   455       entity: the unaltered entity
   456       entity: the unaltered entity
       
   457       entity_properties: keyword arguments that correspond to entity
       
   458         properties and their values
   456       name: the name of the field to be changed
   459       name: the name of the field to be changed
   457       value: the new value
       
   458     """
   460     """
   459 
   461 
   460     if not entity:
   462     if not entity:
   461       raise NoEntityError
   463       raise NoEntityError
   462 
   464