Advertisement
Guest User

Untitled

a guest
Nov 21st, 2014
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.14 KB | None | 0 0
  1.     def alter_subpool_assigned_items(self, lock, quantity):
  2.         """Choose subpool for given lock and alter it's assigned items using given quantity.
  3.  
  4.        quantity can be both positive and negative
  5.  
  6.        :param: quantity: `int` quantity to use for alteration.
  7.        """
  8.         session = inspect(self).session
  9.         expression = (
  10.             Subpool.__table__.update()
  11.             .values(locked=Subpool.locked + quantity, id=func.last_insert_id(Subpool.id))
  12.             .where(Subpool.pool == self)
  13.         )
  14.  
  15.         if quantity > 0:
  16.             expression = expression.where(Subpool.locked < Subpool.availability | Subpool.availability.is_(None))
  17.  
  18.         if (
  19.             session.execute(
  20.                 unicode(expression.compile(compile_kwargs={"literal_binds": True}))
  21.                 + ' ORDER BY {0} LIMIT 1'.format(Subpool.locked.name), dict(param_1=self.id)).rowcount != 1
  22.         ):
  23.             if quantity > 0:
  24.                 raise NotEnoughSubpoolAvailabilityError()
  25.             else:
  26.                 raise NotEnoughSubpoolLockedError()
  27.         lock.subpool_id = session.execute(func.last_insert_id()).scalar()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement