Add users to group by email

This commit is contained in:
Yingtong Li 2020-02-12 16:02:12 +11:00
parent 1975c48273
commit f091b20773
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
2 changed files with 39 additions and 0 deletions

View File

@ -150,6 +150,7 @@ SOCIAL_AUTH_PIPELINE = (
'social_core.pipeline.social_auth.social_uid', 'social_core.pipeline.social_auth.social_uid',
'social_core.pipeline.social_auth.social_user', 'social_core.pipeline.social_auth.social_user',
'social_core.pipeline.user.get_username', 'social_core.pipeline.user.get_username',
'social_core.pipeline.social_auth.associate_by_email',
'social_core.pipeline.user.create_user', 'social_core.pipeline.user.create_user',
'social_core.pipeline.social_auth.associate_user', 'social_core.pipeline.social_auth.associate_user',
'social_core.pipeline.social_auth.load_extra_data', 'social_core.pipeline.social_auth.load_extra_data',

View File

@ -0,0 +1,38 @@
# 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)