From 9d1bb7f8bf86237322393a02cf49afa25dab5edd Mon Sep 17 00:00:00 2001 From: eric Date: Wed, 7 Jul 2021 11:11:00 -0400 Subject: [PATCH] fix credit management command --- .../management/commands/grant_user_credit.py | 33 ++++++++++++------- 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/payment/management/commands/grant_user_credit.py b/payment/management/commands/grant_user_credit.py index 91ead82f..7eed0048 100644 --- a/payment/management/commands/grant_user_credit.py +++ b/payment/management/commands/grant_user_credit.py @@ -1,24 +1,33 @@ +from decimal import Decimal + from django.core.management.base import BaseCommand from django.contrib.auth.models import User +ACTIONS = ("debit", "redeem", "credit", "cash") class Command(BaseCommand): help = "grant (or debit or redeem) credit to a user. \ Usage: grant_user_credit \ amount is dollars or 'all' " - args = " " - def handle(self, username, amount, action="credit", *args, **kwargs): - if action not in ("debit", "redeem", "credit"): - print('action should be in ("debit", "redeem", "credit")') + def add_arguments(self, parser): + parser.add_argument('username', type=str, help="user to credit") + parser.add_argument('amount', type=str, help="amount to credit") + parser.add_argument('action', type=str, help="credit/debit/redeem/cash") + + def handle(self, username, amount, action, **options): + if action not in ACTIONS: + self.stdout.write('action should be in %s' % str(ACTIONS)) return user = User.objects.get(username=username) if amount == 'all': amount = user.credit.available - if action in ("debit", "redeem" ): - amount = -int(amount) - elif action == "credit": - amount = int(amount) - notify = action != "redeem" - user.credit.add_to_balance(amount, notify=notify) - print("{}ed ${} from {}".format(action, amount, username)) - print("{} now has a balance of {} credits".format(username, user.credit.balance)) + if action == 'redeem': + user.credit.use_pledge(amount) + else: + amount = Decimal(amount) + if action in ("debit", "cash"): + amount = - amount + user.credit.add_to_balance(amount, notify=action != "cash") + self.stdout.write("{}ed ${} from {}".format(action, amount, username)) + self.stdout.write("{} now has a balance of {} credits".format( + username, user.credit.balance))