society-self-service/ssmain/management/commands/addgroup.py

39 lines
1.4 KiB
Python
Raw Normal View History

2020-02-12 16:02:12 +11:00
# Society Self-Service
# Copyright © 2018-2020 Yingtong Li (RunasSudo)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from django.core.management.base import BaseCommand, CommandError
from django.contrib.auth.models import User, Group
class Command(BaseCommand):
help = 'Adds the users with the specified emails to the specified group'
def add_arguments(self, parser):
parser.add_argument('group')
parser.add_argument('email', nargs='*')
def handle(self, *args, **options):
group = Group.objects.get(name=options['group'])
for email in options['email']:
try:
user = User.objects.get(email=email)
except User.DoesNotExist:
user = User.objects.create_user(email.split('@')[0], email)
user.save()
user.groups.add(group)