def post(self, expense_id): parser = reqparse.RequestParser() parser.add_argument('group_id', type=int) parser.add_argument('friendship_id', type=int) parser.add_argument('expense_bundle_id', type=int) parser.add_argument('description', type=str) parser.add_argument('details', type=str) parser.add_argument('payment', type=int) parser.add_argument('cost', type=int) parser.add_argument('date', type=api_date) parser.add_argument('category_id', type=int) parser.add_argument('users__user_id', type=int) parser.add_argument('users__paid_share', type=int) parser.add_argument('users__owed_share', type=int) return splitwise.update_expense(expense_id, parser.parse_args())
def post(self, expense_id): parser = reqparse.RequestParser() parser.add_argument('group_id', type=int) parser.add_argument('friendship_id', type=int) parser.add_argument('expense_bundle_id', type=int) parser.add_argument('description', type=str) parser.add_argument('details', type=str) parser.add_argument('payment', type=int) parser.add_argument('cost', type=int) parser.add_argument('date', type=api_date) parser.add_argument('category_id', type=int) parser.add_argument('users__user_id', type=int) parser.add_argument('users__paid_share', type=int) parser.add_argument('users__owed_share', type=int) return splitwise.update_expense(expense_id, parser.parse_args())
def put(self): import pprint parser = reqparse.RequestParser() parser.add_argument('group_id', type=int, location='args') parser.add_argument('limit', type=int, default=0, location='args') parser.add_argument('offset', type=int, default=0, location='args') convert = dict() currencies = flask.request.get_json() if isinstance(currencies, dict): currencies = [currencies] for currency in currencies: if currency.get('convert'): currency['exchange'] = decimal.Decimal( currency['exchange']) convert[currency['currency_code']] = currency for expense in splitwise.get_expenses(parser.parse_args())['expenses']: if expense.get('deleted_at'): # Skip deleted continue if expense['currency_code'] in convert: currency = convert[expense['currency_code']] else: # Only process items we will actually convert continue def recalculate(amount): rate = currency['exchange'] amount = decimal.Decimal(amount) return float('%.2f' % (amount / rate)) new_expense = dict() expense['exchange'] = currency['exchange'] new_expense['description'] = ( '%(description)s (%(currency_code)s %(cost)s @ %(exchange).2f)' % expense).encode('utf-8') new_expense['currency_code'] = currency['new_currency_code'] paying_users = 0 owed = 0 paid_total = 0 for i, user in enumerate(expense['users']): def add_user(k, v): new_expense['users__%d__%s' % (i, k)] = v return v add_user('user_id', user['user_id']) paid = add_user('paid_share', recalculate(user['paid_share'])) owed += add_user('owed_share', recalculate(user['owed_share'])) if paid: paying_user = i paying_users += 1 paid_total += paid if paying_users == 1: new_expense['users__%d__paid_share' % paying_user] = owed elif paying_users > 1 and paid_total != owed: print ('Paid total does not match up to owed amount ' '(%s != %s)' % (paid_total, owed)) continue new_expense['cost'] = owed try: new = splitwise.update_expense(int(expense['id']), new_expense) assert not new.get('errors'), new['errors'] except Exception, e: print repr(e), e print new_expense raise
def put(self): import pprint parser = reqparse.RequestParser() parser.add_argument('group_id', type=int, location='args') parser.add_argument('limit', type=int, default=0, location='args') parser.add_argument('offset', type=int, default=0, location='args') convert = dict() currencies = flask.request.get_json() if isinstance(currencies, dict): currencies = [currencies] for currency in currencies: if currency.get('convert'): currency['exchange'] = decimal.Decimal(currency['exchange']) convert[currency['currency_code']] = currency for expense in splitwise.get_expenses(parser.parse_args())['expenses']: if expense.get('deleted_at'): # Skip deleted continue if expense['currency_code'] in convert: currency = convert[expense['currency_code']] else: # Only process items we will actually convert continue def recalculate(amount): rate = currency['exchange'] amount = decimal.Decimal(amount) return float('%.2f' % (amount / rate)) new_expense = dict() expense['exchange'] = currency['exchange'] new_expense['description'] = ( '%(description)s (%(currency_code)s %(cost)s @ %(exchange).2f)' % expense).encode('utf-8') new_expense['currency_code'] = currency['new_currency_code'] paying_users = 0 owed = 0 paid_total = 0 for i, user in enumerate(expense['users']): def add_user(k, v): new_expense['users__%d__%s' % (i, k)] = v return v add_user('user_id', user['user_id']) paid = add_user('paid_share', recalculate(user['paid_share'])) owed += add_user('owed_share', recalculate(user['owed_share'])) if paid: paying_user = i paying_users += 1 paid_total += paid if paying_users == 1: new_expense['users__%d__paid_share' % paying_user] = owed elif paying_users > 1 and paid_total != owed: print( 'Paid total does not match up to owed amount ' '(%s != %s)' % (paid_total, owed)) continue new_expense['cost'] = owed try: new = splitwise.update_expense(int(expense['id']), new_expense) assert not new.get('errors'), new['errors'] except Exception, e: print repr(e), e print new_expense raise