devicehub-django/user/management/commands/add_user.py

37 lines
1.1 KiB
Python
Raw Normal View History

2024-09-27 12:23:25 +00:00
from uuid import uuid4
2024-07-01 10:19:02 +00:00
from django.core.management.base import BaseCommand
from django.contrib.auth import get_user_model
2024-09-17 15:28:14 +00:00
from user.models import Institution
2024-09-27 12:23:25 +00:00
from api.models import Token
2024-07-01 10:19:02 +00:00
User = get_user_model()
class Command(BaseCommand):
help = "Create a new user"
def add_arguments(self, parser):
2024-09-17 15:28:14 +00:00
parser.add_argument('institution', type=str, help='institution')
2024-07-01 10:19:02 +00:00
parser.add_argument('email', type=str, help='email')
parser.add_argument('password', type=str, help='password')
def handle(self, *args, **kwargs):
2024-09-18 16:01:46 +00:00
self.email = kwargs['email']
self.password = kwargs['password']
self.institution = Institution.objects.get(name=kwargs['institution'])
self.create_user()
2024-07-01 10:19:02 +00:00
2024-09-18 16:01:46 +00:00
def create_user(self):
2024-09-17 15:28:14 +00:00
self.u = User.objects.create(
2024-09-18 16:01:46 +00:00
institution=self.institution,
email=self.email,
password=self.password
2024-09-17 15:28:14 +00:00
)
2024-09-18 16:01:46 +00:00
self.u.set_password(self.password)
2024-07-18 15:21:22 +00:00
self.u.save()
2024-09-27 12:23:25 +00:00
token = uuid4()
Token.objects.create(token=token, owner=self.u)
print(f"TOKEN: {token}")