fix credit management command

pull/94/head
eric 2021-07-07 11:11:00 -04:00
parent c0d02d880e
commit 9d1bb7f8bf
1 changed files with 21 additions and 12 deletions

View File

@ -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 <username> <amount> <action>\
amount is dollars or 'all' "
args = "<username> <amount> <action>"
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))