示例#1
0
文件: views.py 项目: WoLpH/splitwise
    def get_expenses(self):
        parser = reqparse.RequestParser()
        parser.add_argument('group_id', type=int)
        parser.add_argument('friendship_id', type=int)
        parser.add_argument('dated_after', type=api_date)
        parser.add_argument('dated_before', type=api_date)
        parser.add_argument('updated_after', type=api_date)
        parser.add_argument('updated_before', type=api_date)
        parser.add_argument('limit', type=int, default=20)
        parser.add_argument('offset', type=int, default=0)
        kwargs = parser.parse_args()

        if app.config.get('OFFLINE'):
            data = flask.json.load(open('offline/expenses.json'))
            expenses = data['expenses']

            if kwargs['group_id'] is not None:
                expenses = [e for e in expenses
                            if (e['group_id'] or 0) == kwargs['group_id']]

            if kwargs['offset']:
                expenses = expenses[kwargs['offset']:]

            if kwargs['limit']:
                expenses = expenses[:kwargs['limit']]

            data['expenses'] = expenses
            return data
        else:
            return splitwise.get_expenses(kwargs)
示例#2
0
    def get_expenses(self):
        parser = reqparse.RequestParser()
        parser.add_argument('group_id', type=int)
        parser.add_argument('friendship_id', type=int)
        parser.add_argument('dated_after', type=api_date)
        parser.add_argument('dated_before', type=api_date)
        parser.add_argument('updated_after', type=api_date)
        parser.add_argument('updated_before', type=api_date)
        parser.add_argument('limit', type=int, default=20)
        parser.add_argument('offset', type=int, default=0)
        kwargs = parser.parse_args()

        if app.config.get('OFFLINE'):
            data = flask.json.load(open('offline/expenses.json'))
            expenses = data['expenses']

            if kwargs['group_id'] is not None:
                expenses = [
                    e for e in expenses
                    if (e['group_id'] or 0) == kwargs['group_id']
                ]

            if kwargs['offset']:
                expenses = expenses[kwargs['offset']:]

            if kwargs['limit']:
                expenses = expenses[:kwargs['limit']]

            data['expenses'] = expenses
            return data
        else:
            return splitwise.get_expenses(kwargs)
示例#3
0
文件: views.py 项目: WoLpH/splitwise
    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
示例#4
0
    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