add Institution in the model
This commit is contained in:
parent
aafaf4b1d7
commit
afe545de64
3
reset.sh
3
reset.sh
|
@ -1,4 +1,5 @@
|
|||
rm db/*
|
||||
python3 manage.py migrate
|
||||
python3 manage.py add_user user@example.org 1234
|
||||
python3 manage.py add_institution Pangea
|
||||
python3 manage.py add_user Pangea user@example.org 1234
|
||||
python3 manage.py up_snapshots example/snapshots/ user@example.org
|
||||
|
|
12
user/management/commands/add_institution.py
Normal file
12
user/management/commands/add_institution.py
Normal file
|
@ -0,0 +1,12 @@
|
|||
from django.core.management.base import BaseCommand
|
||||
from user.models import Institution
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = "Create a new Institution"
|
||||
|
||||
def add_arguments(self, parser):
|
||||
parser.add_argument('name', type=str, help='institution')
|
||||
|
||||
def handle(self, *args, **kwargs):
|
||||
Institution.objects.create(name=kwargs['name'])
|
|
@ -1,6 +1,7 @@
|
|||
from django.core.management.base import BaseCommand
|
||||
from django.contrib.auth import get_user_model
|
||||
from lot.models import LotTag
|
||||
from user.models import Institution
|
||||
|
||||
|
||||
User = get_user_model()
|
||||
|
@ -10,17 +11,23 @@ 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):
|
||||
email = kwargs['email']
|
||||
password = kwargs['password']
|
||||
self.create_user(email, password)
|
||||
institution = Institution.objects.get(name=kwargs['institution'])
|
||||
self.create_user(institution, email, password)
|
||||
self.create_lot_tags()
|
||||
|
||||
def create_user(self, email, password):
|
||||
self.u = User.objects.create(email=email, password=password)
|
||||
def create_user(self, institution, email, password):
|
||||
self.u = User.objects.create(
|
||||
institution=institution,
|
||||
email=email,
|
||||
password=password
|
||||
)
|
||||
self.u.set_password(password)
|
||||
self.u.save()
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
# Generated by Django 5.0.6 on 2024-07-17 14:57
|
||||
# Generated by Django 5.0.6 on 2024-09-17 15:21
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
|
@ -10,6 +11,51 @@ class Migration(migrations.Migration):
|
|||
dependencies = []
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name="Institution",
|
||||
fields=[
|
||||
(
|
||||
"id",
|
||||
models.BigAutoField(
|
||||
auto_created=True,
|
||||
primary_key=True,
|
||||
serialize=False,
|
||||
verbose_name="ID",
|
||||
),
|
||||
),
|
||||
(
|
||||
"name",
|
||||
models.CharField(max_length=255, unique=True, verbose_name="Name"),
|
||||
),
|
||||
(
|
||||
"logo",
|
||||
models.CharField(
|
||||
blank=True, max_length=255, null=True, verbose_name="Logo"
|
||||
),
|
||||
),
|
||||
(
|
||||
"location",
|
||||
models.CharField(
|
||||
blank=True, max_length=255, null=True, verbose_name="Location"
|
||||
),
|
||||
),
|
||||
(
|
||||
"responsable_person",
|
||||
models.CharField(
|
||||
blank=True,
|
||||
max_length=255,
|
||||
null=True,
|
||||
verbose_name="Responsable",
|
||||
),
|
||||
),
|
||||
(
|
||||
"supervisor_person",
|
||||
models.CharField(
|
||||
blank=True, max_length=255, null=True, verbose_name="Supervisor"
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name="User",
|
||||
fields=[
|
||||
|
@ -56,6 +102,13 @@ class Migration(migrations.Migration):
|
|||
),
|
||||
),
|
||||
("accept_gdpr", models.BooleanField(default=False)),
|
||||
(
|
||||
"institution",
|
||||
models.ForeignKey(
|
||||
on_delete=django.db.models.deletion.CASCADE,
|
||||
to="user.institution",
|
||||
),
|
||||
),
|
||||
],
|
||||
options={
|
||||
"abstract": False,
|
||||
|
|
|
@ -5,8 +5,32 @@ from django.contrib.auth.models import BaseUserManager, AbstractBaseUser
|
|||
# Create your models here.
|
||||
|
||||
|
||||
class Institution(models.Model):
|
||||
name = models.CharField(
|
||||
_("Name"),
|
||||
max_length=255,
|
||||
blank=False,
|
||||
null=False,
|
||||
unique=True
|
||||
)
|
||||
logo = models.CharField(_("Logo"), max_length=255, blank=True, null=True)
|
||||
location = models.CharField(_("Location"), max_length=255, blank=True, null=True)
|
||||
responsable_person = models.CharField(
|
||||
_("Responsable"),
|
||||
max_length=255,
|
||||
blank=True,
|
||||
null=True
|
||||
)
|
||||
supervisor_person = models.CharField(
|
||||
_("Supervisor"),
|
||||
max_length=255,
|
||||
blank=True,
|
||||
null=True
|
||||
)
|
||||
|
||||
|
||||
class UserManager(BaseUserManager):
|
||||
def create_user(self, email, password=None):
|
||||
def create_user(self, email, institution, password=None):
|
||||
"""
|
||||
Creates and saves a User with the given email, date of
|
||||
birth and password.
|
||||
|
@ -16,19 +40,21 @@ class UserManager(BaseUserManager):
|
|||
|
||||
user = self.model(
|
||||
email=self.normalize_email(email),
|
||||
institution=institution
|
||||
)
|
||||
|
||||
user.set_password(password)
|
||||
user.save(using=self._db)
|
||||
return user
|
||||
|
||||
def create_superuser(self, email, password=None):
|
||||
def create_superuser(self, email, institution, password=None):
|
||||
"""
|
||||
Creates and saves a superuser with the given email, date of
|
||||
birth and password.
|
||||
"""
|
||||
user = self.create_user(
|
||||
email,
|
||||
institution=institution,
|
||||
password=password,
|
||||
)
|
||||
user.is_admin = True
|
||||
|
@ -47,11 +73,13 @@ class User(AbstractBaseUser):
|
|||
first_name = models.CharField(_("First name"), max_length=255, blank=True, null=True)
|
||||
last_name = models.CharField(_("Last name"), max_length=255, blank=True, null=True)
|
||||
accept_gdpr = models.BooleanField(default=False)
|
||||
institution = models.ForeignKey(Institution, on_delete=models.CASCADE)
|
||||
|
||||
|
||||
objects = UserManager()
|
||||
|
||||
USERNAME_FIELD = "email"
|
||||
REQUIRED_FIELDS = []
|
||||
REQUIRED_FIELDS = ['institution']
|
||||
|
||||
def __str__(self):
|
||||
return self.email
|
||||
|
@ -76,5 +104,3 @@ class User(AbstractBaseUser):
|
|||
def username(self):
|
||||
"Is the email of the user"
|
||||
return self.email
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue