From 6a4fa1e82d61d6e591762b33974d7bf98e4211f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Valeria=20L=C3=B3pez?= Date: Mon, 20 Oct 2025 14:22:42 -0600 Subject: [PATCH] =?UTF-8?q?Importaci=C3=B3n=20de=20alumnos=20y=20asistente?= =?UTF-8?q?s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/authentication/admin.py | 81 +++++++++++-------- .../migrations/0013_alter_userprofile_user.py | 21 +++++ backend/authentication/models.py | 2 +- 3 files changed, 71 insertions(+), 33 deletions(-) create mode 100644 backend/authentication/migrations/0013_alter_userprofile_user.py diff --git a/backend/authentication/admin.py b/backend/authentication/admin.py index 53e2535..e596dc5 100644 --- a/backend/authentication/admin.py +++ b/backend/authentication/admin.py @@ -35,25 +35,32 @@ class StudentResource(resources.ModelResource): # Asignar automáticamente user_type como student row['user_type'] = 'student' - def after_save_instance(self, instance, using_transactions, dry_run): + def before_save_instance(self, instance, row, using_transactions, dry_run, **kwargs): """Crear usuario de Django si no existe y asegurar que sea estudiante""" - if not dry_run: - # Asegurar que sea estudiante - if instance.user_type != 'student': - instance.user_type = 'student' - instance.save() + # Asegurar que sea estudiante + instance.user_type = 'student' - # Crear usuario de Django si no existe - if not instance.user_id: - user = User.objects.create_user( - username=instance.account_number, - first_name=instance.full_name, - password=None - ) - user.set_unusable_password() - user.save() + # Crear usuario de Django si no existe + if not instance.user_id: + # Buscar si ya existe el usuario + try: + user = User.objects.get(username=instance.account_number) + except User.DoesNotExist: + if not dry_run: + # Solo crear en la base de datos si no es dry_run + user = User.objects.create_user( + username=instance.account_number, + first_name=instance.full_name, + password=None + ) + user.set_unusable_password() + user.save() + else: + # En dry_run, dejar user como None (ahora permitido) + user = None + + if user: instance.user = user - instance.save() class AssistantResource(resources.ModelResource): @@ -75,26 +82,36 @@ class AssistantResource(resources.ModelResource): # Asignar automáticamente user_type como assistant row['user_type'] = 'assistant' - def after_save_instance(self, instance, using_transactions, dry_run): + def before_save_instance(self, instance, row, using_transactions, dry_run, **kwargs): """Crear usuario de Django si no existe y asegurar que sea asistente""" - if not dry_run: - # Asegurar que sea asistente - if instance.user_type != 'assistant': - instance.user_type = 'assistant' - instance.save() + # Asegurar que sea asistente + instance.user_type = 'assistant' - # Crear usuario de Django si no existe - if not instance.user_id: - user = User.objects.create_user( - username=instance.account_number, - first_name=instance.full_name, - password=None - ) - user.set_unusable_password() - user.save() + # Crear usuario de Django si no existe + if not instance.user_id: + # Buscar si ya existe el usuario + try: + user = User.objects.get(username=instance.account_number) + except User.DoesNotExist: + if not dry_run: + # Solo crear en la base de datos si no es dry_run + user = User.objects.create_user( + username=instance.account_number, + first_name=instance.full_name, + password=None + ) + user.set_unusable_password() + user.save() + else: + # En dry_run, dejar user como None (ahora permitido) + user = None + + if user: instance.user = user - instance.save() + def after_save_instance(self, instance, using_transactions, dry_run): + """Crear automáticamente el registro de Asistente después de guardar""" + if not dry_run: # Crear automáticamente el registro de Asistente si no existe from .models import Asistente Asistente.objects.get_or_create( diff --git a/backend/authentication/migrations/0013_alter_userprofile_user.py b/backend/authentication/migrations/0013_alter_userprofile_user.py new file mode 100644 index 0000000..770c4b2 --- /dev/null +++ b/backend/authentication/migrations/0013_alter_userprofile_user.py @@ -0,0 +1,21 @@ +# Generated by Django 5.2.6 on 2025-10-20 19:19 + +import django.db.models.deletion +from django.conf import settings +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('authentication', '0012_systemconfiguration_minutes_after_start_and_more'), + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ] + + operations = [ + migrations.AlterField( + model_name='userprofile', + name='user', + field=models.OneToOneField(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL), + ), + ] diff --git a/backend/authentication/models.py b/backend/authentication/models.py index 060a349..bc27805 100644 --- a/backend/authentication/models.py +++ b/backend/authentication/models.py @@ -10,7 +10,7 @@ class UserProfile(models.Model): ('assistant', 'Asistente'), ) - user = models.OneToOneField(User, on_delete=models.CASCADE) + user = models.OneToOneField(User, on_delete=models.CASCADE, null=True, blank=True) account_number = models.CharField( max_length=7, unique=True,