Exemple #1
0
def coffee_bill_pay(username, bill_id):
    try:
        user = userprovider.get_CoffeeUser(username)
    except KeyError:
        return render_error(404, "User not found!")

    if user.splitwise_id == -1:
        return render_error(
            401, "User has no splitwise account assigned! Cannot pay bill!")

    bill = user.bills.get(bill_id)
    if bill is None:
        return render_error(404, f"Bill {bill_id} not found!")

    # Only current bill can be payed!
    if not bill == user.current_bill:
        return render_error(403, "You can only pay the current bill!")

    # Trigger transaction on splitwise
    splitwise = SplitwiseWrapper()
    try:
        transaction_id = splitwise.pay_bill(paying_user_id=user.splitwise_id,
                                            bill=bill)
    except RuntimeError as e:
        return render_error(500, f"{e}")

    # If successful set id and return to previous page
    user.pay_bill(transaction_id)
    return render_template("coffee/success_bill_payed.html",
                           user=user,
                           msg="Rechnung bezahlt!")
Exemple #2
0
def auth_splitwise_redirect():
    state = request.args.get('state')
    code = request.args.get('code')

    if state is None or code is None:
        return render_error(
            500,
            "Splitwise authentication failed. Could not find `state` or `code` in response."
        )

    # Save token with SplitwiseWrapper
    splitwise = SplitwiseWrapper()
    redirect_uri = url_for("auth_splitwise_redirect", _external=True)
    token = splitwise.s.getOAuth2AccessToken(code, redirect_uri)
    splitwise.set_access_token(token)

    return redirect(url_for('auth_splitwise_info'))
Exemple #3
0
    def test_save_auth_token(self):
        token = {"access_token": "aaa", "token_type": "bearer"}

        s = SplitwiseWrapper(self.config_non_auth)
        s.set_access_token(token)

        # Read file back and check that saved token is identical to input `token`
        s = SplitwiseWrapper(self.config_non_auth)
        self.assertDictEqual(s._access_token, token)
Exemple #4
0
def auth_splitwise_info():
    splitwise = SplitwiseWrapper()

    # Redirect to login if not authenticated
    if splitwise._access_token is None:
        redirect(url_for('auth_splitwise'))

    s_user = splitwise.s.getCurrentUser()
    s_friends = splitwise.s.getFriends()

    # Filter coffee users which are linked to a splitwise id
    splitwise_coffee_users = {
        c.splitwise_id: c
        for c in userprovider.list_CoffeeUsers().values()
        if c.splitwise_id != -1
    }

    return render_template("splitwise/info.html",
                           s_user=s_user,
                           s_friends=s_friends,
                           coffee_users=splitwise_coffee_users)
Exemple #5
0
def auth_splitwise_logout():
    splitwise = SplitwiseWrapper()
    splitwise.set_access_token({"access_token": "", "token_type": ""})

    return redirect(url_for('index'))
Exemple #6
0
def auth_splitwise():
    splitwise = SplitwiseWrapper()
    redirect_uri = url_for("auth_splitwise_redirect", _external=True)
    url, state = splitwise.s.getOAuth2AuthorizeURL(redirect_uri)
    return redirect(url)
Exemple #7
0
 def test_read_non_auth_config(self):
     s = SplitwiseWrapper(self.config_non_auth)
     self.assertEqual(s._consumer_key, 'AAA')
     self.assertEqual(s._consumer_secret, 'BBB')
Exemple #8
0
 def test_read_empty_config(self):
     with self.assertRaises(ValueError):
         s = SplitwiseWrapper(self.config_file_empty)