You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

34 lines
1.2 KiB

from decimal import Decimal
from django.core.management.base import BaseCommand
from django.db.models import F
from apps.payment.models import Payment, AuthorBalance, UserBonus
from apps.user.models import User
class Command(BaseCommand):
help = 'Give to all users with role==USER <amount> bonuses in action <action_name>'
def add_arguments(self, parser):
parser.add_argument(
'action_name', type=str,
help='Name of action',
)
parser.add_argument(
'amount', type=int,
help='Bonuses amount',
)
def handle(self, *args, **options):
action_name = options.get('action_name')
amount = options.get('amount')
excluded_users = UserBonus.objects.filter(is_service=True, action_name=action_name).values_list('user_id', flat=True)
for user in User.objects.filter(role=User.USER_ROLE, is_active=True).exclude(
id__in=excluded_users).values_list('id', flat=True):
UserBonus.objects.create(user_id=user, amount=amount, is_service=True, action_name=action_name)
print('%d bonuses was sent to user %d in action %s' % (amount, user, action_name))