IdHub/idhub/management/commands/initial_datas.py

48 lines
1.6 KiB
Python
Raw Normal View History

2023-11-07 16:43:23 +00:00
import os
import csv
from pathlib import Path
2023-11-03 12:29:15 +00:00
from django.core.management.base import BaseCommand, CommandError
from django.contrib.auth import get_user_model
2023-11-07 16:43:23 +00:00
from decouple import config
2023-11-03 12:29:15 +00:00
from idhub.models import Organization
User = get_user_model()
class Command(BaseCommand):
help = "Insert minimum datas for the project"
def handle(self, *args, **kwargs):
2023-11-07 16:43:23 +00:00
ADMIN_EMAIL = config('ADMIN_EMAIL', 'admin@example.org')
ADMIN_PASSWORD = config('ADMIN_PASSWORD', '1234')
USER_EMAIL = config('USER_EMAIL', 'user1@example.org')
USER_PASSWORD = config('USER_PASSWORD', '1234')
self.create_admin_users(ADMIN_EMAIL, ADMIN_PASSWORD)
self.create_users(USER_EMAIL, USER_PASSWORD)
BASE_DIR = Path(__file__).resolve().parent.parent.parent.parent
ORGANIZATION = os.path.join(BASE_DIR, 'examples/organizations.csv')
with open(ORGANIZATION, newline='\n') as csvfile:
f = csv.reader(csvfile, delimiter=';', quotechar='"')
for r in f:
self.create_organizations(r[0].strip(), r[1].strip())
2023-11-03 12:29:15 +00:00
def create_admin_users(self, email, password):
2024-01-04 15:27:27 +00:00
su = User.objects.create_superuser(email=email, password=password)
su.set_encrypted_sensitive_data(password)
su.save()
2023-11-03 12:29:15 +00:00
def create_users(self, email, password):
2024-01-04 15:27:27 +00:00
u = User.objects.create(email=email, password=password)
2023-11-03 12:29:15 +00:00
u.set_password(password)
2024-01-04 15:27:27 +00:00
u.set_encrypted_sensitive_data(password)
2023-11-03 12:29:15 +00:00
u.save()
def create_organizations(self, name, url):
Organization.objects.create(name=name, url=url)