add Institution in the model

This commit is contained in:
Cayo Puigdefabregas 2024-09-17 17:28:14 +02:00
parent aafaf4b1d7
commit afe545de64
5 changed files with 109 additions and 10 deletions

View File

@ -1,4 +1,5 @@
rm db/* rm db/*
python3 manage.py migrate 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 python3 manage.py up_snapshots example/snapshots/ user@example.org

View 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'])

View File

@ -1,6 +1,7 @@
from django.core.management.base import BaseCommand from django.core.management.base import BaseCommand
from django.contrib.auth import get_user_model from django.contrib.auth import get_user_model
from lot.models import LotTag from lot.models import LotTag
from user.models import Institution
User = get_user_model() User = get_user_model()
@ -10,17 +11,23 @@ class Command(BaseCommand):
help = "Create a new user" help = "Create a new user"
def add_arguments(self, parser): def add_arguments(self, parser):
parser.add_argument('institution', type=str, help='institution')
parser.add_argument('email', type=str, help='email') parser.add_argument('email', type=str, help='email')
parser.add_argument('password', type=str, help='password') parser.add_argument('password', type=str, help='password')
def handle(self, *args, **kwargs): def handle(self, *args, **kwargs):
email = kwargs['email'] email = kwargs['email']
password = kwargs['password'] 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() self.create_lot_tags()
def create_user(self, email, password): def create_user(self, institution, email, password):
self.u = User.objects.create(email=email, password=password) self.u = User.objects.create(
institution=institution,
email=email,
password=password
)
self.u.set_password(password) self.u.set_password(password)
self.u.save() self.u.save()

View File

@ -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 from django.db import migrations, models
@ -10,6 +11,51 @@ class Migration(migrations.Migration):
dependencies = [] dependencies = []
operations = [ 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( migrations.CreateModel(
name="User", name="User",
fields=[ fields=[
@ -56,6 +102,13 @@ class Migration(migrations.Migration):
), ),
), ),
("accept_gdpr", models.BooleanField(default=False)), ("accept_gdpr", models.BooleanField(default=False)),
(
"institution",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
to="user.institution",
),
),
], ],
options={ options={
"abstract": False, "abstract": False,

View File

@ -5,8 +5,32 @@ from django.contrib.auth.models import BaseUserManager, AbstractBaseUser
# Create your models here. # 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): 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 Creates and saves a User with the given email, date of
birth and password. birth and password.
@ -16,19 +40,21 @@ class UserManager(BaseUserManager):
user = self.model( user = self.model(
email=self.normalize_email(email), email=self.normalize_email(email),
institution=institution
) )
user.set_password(password) user.set_password(password)
user.save(using=self._db) user.save(using=self._db)
return user 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 Creates and saves a superuser with the given email, date of
birth and password. birth and password.
""" """
user = self.create_user( user = self.create_user(
email, email,
institution=institution,
password=password, password=password,
) )
user.is_admin = True user.is_admin = True
@ -47,11 +73,13 @@ class User(AbstractBaseUser):
first_name = models.CharField(_("First name"), max_length=255, blank=True, null=True) 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) last_name = models.CharField(_("Last name"), max_length=255, blank=True, null=True)
accept_gdpr = models.BooleanField(default=False) accept_gdpr = models.BooleanField(default=False)
institution = models.ForeignKey(Institution, on_delete=models.CASCADE)
objects = UserManager() objects = UserManager()
USERNAME_FIELD = "email" USERNAME_FIELD = "email"
REQUIRED_FIELDS = [] REQUIRED_FIELDS = ['institution']
def __str__(self): def __str__(self):
return self.email return self.email
@ -76,5 +104,3 @@ class User(AbstractBaseUser):
def username(self): def username(self):
"Is the email of the user" "Is the email of the user"
return self.email return self.email