From e130ca948c1d949702d8c4147a9f51f6a9de1270 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 17 Sep 2024 17:28:14 +0200 Subject: [PATCH] add Institution in the model --- reset.sh | 3 +- user/management/commands/add_institution.py | 12 +++++ user/management/commands/add_user.py | 13 +++-- user/migrations/0001_initial.py | 55 ++++++++++++++++++++- user/models.py | 36 ++++++++++++-- 5 files changed, 109 insertions(+), 10 deletions(-) create mode 100644 user/management/commands/add_institution.py diff --git a/reset.sh b/reset.sh index e9c2543..7e0d236 100644 --- a/reset.sh +++ b/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 diff --git a/user/management/commands/add_institution.py b/user/management/commands/add_institution.py new file mode 100644 index 0000000..622dff8 --- /dev/null +++ b/user/management/commands/add_institution.py @@ -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']) diff --git a/user/management/commands/add_user.py b/user/management/commands/add_user.py index d9bb0a4..7d31ab7 100644 --- a/user/management/commands/add_user.py +++ b/user/management/commands/add_user.py @@ -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() diff --git a/user/migrations/0001_initial.py b/user/migrations/0001_initial.py index 7faf363..3bd26d8 100644 --- a/user/migrations/0001_initial.py +++ b/user/migrations/0001_initial.py @@ -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, diff --git a/user/models.py b/user/models.py index 1112d59..5fd8424 100644 --- a/user/models.py +++ b/user/models.py @@ -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 - -