from django.contrib.auth import get_user_model
from django.core.management.base import BaseCommand, CommandError

UserModel = get_user_model()


class Command(BaseCommand):
    """
    A management command for suspending one or more users.
    """

    help = 'Suspend users by username'

    def add_arguments(self, parser):
        parser.add_argument('-u', '--username',
                            dest='username',
                            required=False,
                            help=('one or more usernames, comma separated'))
        parser.add_argument('-i', '--id',
                            dest='id',
                            required=False,
                            help=('one or more ids, comma separated'))

    def handle(self, *args, **options):
        users_to_suspend = []

        if options['username']:
            usernames = options['username'].split(',')
            for username in usernames:
                try:
                    users_to_suspend.append(
                        UserModel.objects.get(username=username))
                except UserModel.DoesNotExist:
                    raise CommandError('Username "{}" does not exist!'
                                       .format(username))

        if options['id']:
            ids = options['id'].split(',')
            for id_str in ids:
                try:
                    users_to_suspend.append(
                        UserModel.objects.get(id=int(id_str)))
                except UserModel.DoesNotExist:
                    raise CommandError('User ID "{}" does not exist!'
                                       .format(id_str))

        for user in users_to_suspend:
            user.is_active = False
            user.save()
            print('{} (ID: {}) is suspended.'.format(user.username, user.id))

@@ -40,16 +40,84 @@
 r_model%0A
+from django.core.mail import get_connection, EmailMultiAlternatives%0A
 from dja
@@ -174,16 +174,16 @@
 ndError%0A
-
 %0AUserMod
@@ -796,16 +796,212 @@
 rated'))
+%0A        parser.add_argument('-e', '--email',%0A                            dest='email',%0A                            action='store_true',%0A                            help='send notification email')
 %0A%0A    de
@@ -1899,24 +1899,108 @@
 t(id_str))%0A%0A
+        if options%5B'email'%5D:%0A            self.email_notification(users_to_suspend)%0A%0A
         for 
@@ -2084,16 +2084,16 @@
 .save()%0A
-
         
@@ -2162,8 +2162,888 @@
 er.id))%0A
+%0A    def _body_text(self, username):%0A        body_text = %22%22%22%7B0%7D,%0A%0AYour Open Humans account (username: %22%7B0%7D%22) has been suspended for%0Amisuse of the platform. We want to ensure that the site is not misused,%0Ae.g. for advertising spam.%0A%0AIf this was done in error, please reply to this email to let us know!%0AThere are sometimes mistakes in detecting spam accounts and other abuses.%0A%0ASincerely,%0A%0AOpen Humans%0A%22%22%22.format(username)%0A        return body_text%0A%0A    def email_notification(self, users):%0A        subject = 'Open Humans: Account suspension notification'%0A%0A        messages = %5B%5D%0A        for user in users:%0A            body_text = self._body_text(user.username)%0A            message = EmailMultiAlternatives(%0A                subject, body_text, None, %5Buser.email%5D)%0A            messages.append(message)%0A%0A        connection = get_connection()%0A        connection.send_messages(messages)%0A

Add email notification to suspend_users command