devicehub-django/user/management/commands/add_user.py
2024-09-18 18:01:46 +02:00

31 lines
935 B
Python

from django.core.management.base import BaseCommand
from django.contrib.auth import get_user_model
from user.models import Institution
User = get_user_model()
class Command(BaseCommand):
help = "Create a new user"
def add_arguments(self, parser):
parser.add_argument('institution', type=str, help='institution')
parser.add_argument('email', type=str, help='email')
parser.add_argument('password', type=str, help='password')
def handle(self, *args, **kwargs):
self.email = kwargs['email']
self.password = kwargs['password']
self.institution = Institution.objects.get(name=kwargs['institution'])
self.create_user()
def create_user(self):
self.u = User.objects.create(
institution=self.institution,
email=self.email,
password=self.password
)
self.u.set_password(self.password)
self.u.save()