Exemplo n.º 1
0
def bills_by_id(bill_id):
    try:
        bill = models.Bill.select().join(models.Charges).switch(models.Bill).where(models.Bill.id == bill_id).get()
    except peewee.DoesNotExist:
        logger.warning('User {}; Attempted access to bill ID {} that does not exist'.format(flask_login.current_user.login_name, bill_id))
        return flask.render_template('bills/bills-by-id.html', error='Bill id {} does not exit'.format(bill_id))

    for charge in bill.charges.where(models.Charges.paid == False):
        try:
            bill_functions.check_charge(charge)
        except LookupError:
            break
    return flask.render_template('bills/bills-by-id.html', bill=bill)
Exemplo n.º 2
0
def charge_by_id(charge_id):
    try:
        charge = models.Charges.get(models.Charges.id == charge_id)
        error = None
    except peewee.DoesNotExist:
        charge = None
        logger.warning('User {}; Attempted access to charge ID {} that does not exist'.format(flask_login.current_user.login_name, charge_id))
        error = 'Bill id {} does not exit'.format(charge_id)
    else:
        if flask.request.method == 'POST':
            charge.paid = not charge.paid
            charge.save()
        else:
            try:
                bill_functions.check_charge(charge)
            except LookupError:
                pass
    return flask.render_template('bills/charge-by-id.html', charge=charge, error=error)
Exemplo n.º 3
0
def bills():
    new_bill_form = forms.bill_forms.BillForm()
    if flask.request.method == 'POST':
        if new_bill_form.validate_on_submit():
            with getattr(flask.g, 'db', models.db).atomic() as txn:
                try:
                    new_bill = models.Bill(due=new_bill_form.due.data,
                                           name=new_bill_form.name.data,
                                           amount=new_bill_form.amount.data,
                                           maintainer=flask_login.current_user.table_id,
                                           description=new_bill_form.description.data,
                                           private=new_bill_form.private.data)
                    new_bill.save()
                except Exception as e:
                    flask.flash('Problem submitting bill', 'danger')
                    logger.critical('Cannot save bill form: {}'.format(e))
                    txn.rollback()
                    return flask.redirect(flask.url_for('bills'))

                list_of_charges = [(int(charge[charge.index('_') + 1:]), float(flask.request.form[charge])) for charge in flask.request.form if charge.startswith('user_')]
                list_of_charges = [(x, y if y >= 0 else 0) for x, y in list_of_charges]  # make sure value amounts are 0 or more
                maintainer_payment_method = models.PaymentMethod.get(models.PaymentMethod.user == flask_login.current_user.table_id)
                try:
                    for user_id, amount in list_of_charges:
                        # TODO handle multiple payment methods
                        payment_method = models.PaymentMethod.get(models.PaymentMethod.user == user_id)
                        charge = models.Charges(
                            bill=new_bill,
                            payment_method=payment_method,
                            amount=amount,
                        )

                        if payment_method.pay_online and maintainer_payment_method.pay_online and maintainer_payment_method.token and (user_id != flask_login.current_user.table_id):
                            response_json = bill_functions.charge_venmo(maintainer_payment_method.token, payment_method.online_user_id, new_bill.name, amount, 'private' if new_bill.private else 'friends')
                            charge.online_charge_id = response_json['data']['payment']['id']
                        charge.save()

                except Exception as e:
                    flask.flash('Problem charging users')
                    logger.critical('Cannot charge user: {}'.format(e))
                    txn.rollback()
                    return flask.redirect(flask.url_for('bills'))

            flask.flash('Successfully added bill', 'success')
            return flask.redirect(flask.url_for('bills'))
        # TODO There may need to be a redirect here

    # TODO Improve all of this
    payment_methods = list(models.PaymentMethod.select(models.PaymentMethod, models.User).join(models.User).join(models.Permission).where(models.Permission.permission == models.PERMISSION_TYPE['bills']))  # TODO figure out how to handle multiple payment methods
    outstanding_charges = models.Charges.select(models.Charges, models.PaymentMethod).join(models.PaymentMethod).where(models.Charges.paid == False).execute()

    # Get an update on all charges that were made online
    for charge in outstanding_charges:
        try:
            bill_functions.check_charge(charge)
        except LookupError:
            break

    outstanding_charges = models.Charges.select().where(models.Charges.paid == False).execute()  # Get it again if it was updated
    outstanding_user_charges = models.Charges.select(models.Charges, models.PaymentMethod, models.Bill).join(models.PaymentMethod).switch(models.Charges).join(models.Bill).where((models.PaymentMethod.user == flask_login.current_user.table_id) & (models.Charges.paid == False)).execute()
    outstanding_bills_ids = [charge.bill.id for charge in outstanding_charges]
    outstanding_bills = models.Bill.select().where(models.Bill.id.in_(outstanding_bills_ids) & (models.Bill.private == False)).order_by(+models.Bill.due).execute()

    return flask.render_template('/bills/bills.html', title='Bills', new_bill_form=new_bill_form, payment_methods=payment_methods, user_charges=outstanding_user_charges, outstanding_bills=outstanding_bills)