fix credit management command
parent
c0d02d880e
commit
9d1bb7f8bf
|
@ -1,24 +1,33 @@
|
||||||
|
from decimal import Decimal
|
||||||
|
|
||||||
from django.core.management.base import BaseCommand
|
from django.core.management.base import BaseCommand
|
||||||
from django.contrib.auth.models import User
|
from django.contrib.auth.models import User
|
||||||
|
|
||||||
|
ACTIONS = ("debit", "redeem", "credit", "cash")
|
||||||
class Command(BaseCommand):
|
class Command(BaseCommand):
|
||||||
help = "grant (or debit or redeem) credit to a user. \
|
help = "grant (or debit or redeem) credit to a user. \
|
||||||
Usage: grant_user_credit <username> <amount> <action>\
|
Usage: grant_user_credit <username> <amount> <action>\
|
||||||
amount is dollars or 'all' "
|
amount is dollars or 'all' "
|
||||||
args = "<username> <amount> <action>"
|
|
||||||
|
|
||||||
def handle(self, username, amount, action="credit", *args, **kwargs):
|
def add_arguments(self, parser):
|
||||||
if action not in ("debit", "redeem", "credit"):
|
parser.add_argument('username', type=str, help="user to credit")
|
||||||
print('action should be in ("debit", "redeem", "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
|
return
|
||||||
user = User.objects.get(username=username)
|
user = User.objects.get(username=username)
|
||||||
if amount == 'all':
|
if amount == 'all':
|
||||||
amount = user.credit.available
|
amount = user.credit.available
|
||||||
if action in ("debit", "redeem" ):
|
if action == 'redeem':
|
||||||
amount = -int(amount)
|
user.credit.use_pledge(amount)
|
||||||
elif action == "credit":
|
else:
|
||||||
amount = int(amount)
|
amount = Decimal(amount)
|
||||||
notify = action != "redeem"
|
if action in ("debit", "cash"):
|
||||||
user.credit.add_to_balance(amount, notify=notify)
|
amount = - amount
|
||||||
print("{}ed ${} from {}".format(action, amount, username))
|
user.credit.add_to_balance(amount, notify=action != "cash")
|
||||||
print("{} now has a balance of {} credits".format(username, user.credit.balance))
|
self.stdout.write("{}ed ${} from {}".format(action, amount, username))
|
||||||
|
self.stdout.write("{} now has a balance of {} credits".format(
|
||||||
|
username, user.credit.balance))
|
||||||
|
|
Loading…
Reference in New Issue