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 bonuses in action ' 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))