def _edit_change(self, to_bc): dbFacade = self.dbFacade() if to_bc.uid is not None: if not to_bc.balance or not to_bc.balance.can_modify_balance(h.authenticated_user().uid): return { "failure": Messages.permissionDenied() } # If it's a transfer check if user can modify the other balance if to_bc.transfer: old_from_balance = dbFacade.balances.balanceDao.find_by_uid(to_bc.transfer.balance_uid) if not old_from_balance or not old_from_balance.can_modify_balance(h.authenticated_user().uid): return { "failure": Messages.permissionDenied() } to_balance_uid = request.params.get('to_balance_uid') to_balance = dbFacade.balances.balanceDao.find_by_uid(to_balance_uid) if not to_balance or not to_balance.can_modify_balance(h.authenticated_user().uid): return { "failure": Messages.permissionDenied() } try: with localcontext() as ctx: ctx.prec = 2 to_bc.amount = h.parse_decimal(request.params['amount']) except: log.error("Invalid number: %s" % request.params.get('amount'), exc_info=1) return { "failure": Messages.invalidCurrencyFormat() } try: to_bc.occurred_on = h.parse_date(request.params['occurred_on']) except: return { "failure": Messages.invalidDate() } try: to_bc.change_category_uid = int(request.params['change_category_uid']) except: log.error("Failed to parse arguments %s" % str(request.params), exc_info=1) return { "failure": Messages.invalidArguments() } to_bc.balance = to_balance to_bc.tags = [dbFacade.model.ChangeTag(tag=tag) for tag in dbFacade.balances.elements_from_string(request.params.get('tags', ''))] to_bc.description = request.params.get('description', '') to_bc.user_uid = h.authenticated_user().uid from_balance_uid = request.params.get('from_balance_uid') if from_balance_uid is not None: if from_balance_uid == to_balance_uid: return { "failure": Messages.pleaseSelectDifferentAccounts() } from_balance = dbFacade.balances.balanceDao.find_by_uid(from_balance_uid) if not from_balance or not from_balance.can_modify_balance(h.authenticated_user().uid): return { "failure": Messages.permissionDenied() } from_bc = to_bc.transfer and to_bc.transfer or dbFacade.model.BalanceChange() to_bc.amount = abs(to_bc.amount) from_bc.amount = -to_bc.amount from_bc.occurred_on = to_bc.occurred_on from_bc.change_category_uid = to_bc.change_category_uid from_bc.balance = from_balance from_bc.tags = [dbFacade.model.ChangeTag(tag=tag) for tag in dbFacade.balances.elements_from_string(request.params.get('tags', ''))] from_bc.description = to_bc.description from_bc.user_uid = h.authenticated_user().uid from_bc.transfer = to_bc to_bc.transfer = from_bc elif to_bc.transfer is not None: from_bc = to_bc.transfer from_bc.transfer = None to_bc.transfer = None dbFacade.db.flush() dbFacade.db.delete(from_bc) dbFacade.balances.changeDao.save(to_bc) dbFacade.db.commit() return { "id" : to_bc.uid }