From 775b20a7772dabc9c8d001864825ad48206a9bf9 Mon Sep 17 00:00:00 2001 From: Brad Campbell Date: Mon, 22 Feb 2016 03:20:39 -0500 Subject: [PATCH] more float fixes, round purchase --- chezbetty/datalayer.py | 2 +- chezbetty/templates/user/deposit_cc_custom.jinja2 | 6 +++--- chezbetty/templates/user/deposit_cc_row.jinja2 | 6 +++--- chezbetty/utility.py | 3 +-- chezbetty/views_admin.py | 5 ++++- chezbetty/views_user.py | 4 ++-- 6 files changed, 14 insertions(+), 12 deletions(-) diff --git a/chezbetty/datalayer.py b/chezbetty/datalayer.py index aae2c89..c5c1f90 100644 --- a/chezbetty/datalayer.py +++ b/chezbetty/datalayer.py @@ -189,7 +189,7 @@ def purchase(user, account, items): DBSession.add(pli) amount += line_amount if discount: - amount = amount - (amount * discount) + amount = round(amount - (amount * discount), 2) t.update_amount(amount) if isinstance(account, Pool): diff --git a/chezbetty/templates/user/deposit_cc_custom.jinja2 b/chezbetty/templates/user/deposit_cc_custom.jinja2 index b0b24a7..6756bf7 100644 --- a/chezbetty/templates/user/deposit_cc_custom.jinja2 +++ b/chezbetty/templates/user/deposit_cc_custom.jinja2 @@ -21,15 +21,15 @@
{% if account == 'user' %} -{% set balance = user.balance|float %} -{% set target = balance + amount %} +{% set balance = user.balance %} +{% set target = balance|float + amount|float %}
{{ _('Account Balance') }}
{{ user.balance|format_currency|safe }}
{% else %} {% set balance = pool.balance|float %} -{% set target = balance + amount %} +{% set target = balance + amount|float %}
{{ _('%(pool_name)s Balance')|format(pool_name=pool.name) }}
{{ pool.balance|format_currency|safe }}
diff --git a/chezbetty/templates/user/deposit_cc_row.jinja2 b/chezbetty/templates/user/deposit_cc_row.jinja2 index 0a690f3..aae623d 100644 --- a/chezbetty/templates/user/deposit_cc_row.jinja2 +++ b/chezbetty/templates/user/deposit_cc_row.jinja2 @@ -1,8 +1,8 @@ {% macro deposit_cc_custom(account, amount, balance, stripe_pk, class='') -%} - {% set charge = (amount + 0.3) / 0.971 %} - {% set fee = charge - amount %} + {% set charge = (amount|float + 0.3) / 0.971 %} + {% set fee = charge - amount|float %} {% set total_cents = (charge*100)|round|int %} - {% set new_balance = balance|float + amount %} + {% set new_balance = balance|float + amount|float %} {{ amount|format_currency|safe }} diff --git a/chezbetty/utility.py b/chezbetty/utility.py index d8c626c..8b7301f 100644 --- a/chezbetty/utility.py +++ b/chezbetty/utility.py @@ -293,14 +293,13 @@ def post_stripe_payment( # See http://stripe.com/docs/tutorials/charges stripe.api_key = request.registry.settings['stripe.secret_key'] - charge = (amount + 0.3) / 0.971 + charge = (amount + Decimal('0.3')) / Decimal('0.971') fee = charge - amount if total_cents != int(round((amount + fee)*100)): print("Stripe total mismatch. total_cents {} != {}".format( total_cents, int(round((amount + fee)*100)))) request.session.flash('Unexpected error processing transaction. Card NOT charged.', 'error') return False - amount = Decimal(amount) if amount <= 0.0: request.session.flash( diff --git a/chezbetty/views_admin.py b/chezbetty/views_admin.py index 2207c11..9657452 100644 --- a/chezbetty/views_admin.py +++ b/chezbetty/views_admin.py @@ -553,7 +553,10 @@ def admin_restock(request): values = packed_values.split(',') if len(values) == 1: # This is the global cost value - global_cost = round(Decimal(values[0]), 2) + try: + global_cost = round(Decimal(values[0] or 0), 2) + except: + global_cost = Decimal(0) else: line_values = {} line_type = values[0] diff --git a/chezbetty/views_user.py b/chezbetty/views_user.py index 4e0c261..b4e20d8 100644 --- a/chezbetty/views_user.py +++ b/chezbetty/views_user.py @@ -184,7 +184,7 @@ def user_deposit_cc_custom(request): pool = None return {'user': request.user, 'stripe_pk': request.registry.settings['stripe.publishable_key'], - 'amount': round(float(request.GET['deposit-amount']), 2), + 'amount': round(Decimal(request.GET['deposit-amount']), 2), 'account': account, 'pool': pool, } @@ -194,7 +194,7 @@ def user_deposit_cc_custom(request): permission='user') def user_deposit_cc_submit(request): token = request.POST['stripeToken'] - amount = float(request.POST['betty_amount']) + amount = Decimal(request.POST['betty_amount']) total_cents = int(request.POST['betty_total_cents']) to_account = request.POST['betty_to_account']