feature/pedimentos-correccion-partidas
This commit is contained in:
57
api/cuser/migrations/0005_customuser_rfc_fk_to_m2m.py
Normal file
57
api/cuser/migrations/0005_customuser_rfc_fk_to_m2m.py
Normal file
@@ -0,0 +1,57 @@
|
||||
import django.db.models.deletion
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
def copiar_rfc_a_m2m(apps, schema_editor):
|
||||
"""Copia el RFC singular (FK) al lado M2M antes de eliminar el FK."""
|
||||
CustomUser = apps.get_model('cuser', 'CustomUser')
|
||||
db_alias = schema_editor.connection.alias
|
||||
for user in CustomUser.objects.using(db_alias).filter(rfc_old__isnull=False):
|
||||
user.rfc.add(user.rfc_old)
|
||||
|
||||
|
||||
def revertir_m2m_a_fk(apps, schema_editor):
|
||||
"""En reversa: toma el primer RFC del M2M y lo pone de vuelta en el FK temporal."""
|
||||
CustomUser = apps.get_model('cuser', 'CustomUser')
|
||||
db_alias = schema_editor.connection.alias
|
||||
for user in CustomUser.objects.using(db_alias).prefetch_related('rfc'):
|
||||
primer_rfc = user.rfc.first()
|
||||
if primer_rfc:
|
||||
user.rfc_old = primer_rfc
|
||||
user.save(update_fields=['rfc_old'])
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('cuser', '0004_alter_customuser_rfc'),
|
||||
('customs', '0015_partida_updated_at'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
# 1. Renombrar el FK actual a rfc_old para preservar los datos
|
||||
migrations.RenameField(
|
||||
model_name='customuser',
|
||||
old_name='rfc',
|
||||
new_name='rfc_old',
|
||||
),
|
||||
# 2. Crear el nuevo campo M2M
|
||||
migrations.AddField(
|
||||
model_name='customuser',
|
||||
name='rfc',
|
||||
field=models.ManyToManyField(
|
||||
blank=True,
|
||||
help_text='RFCs de importadores asociados al usuario',
|
||||
related_name='users',
|
||||
to='customs.importador',
|
||||
),
|
||||
),
|
||||
# 3. Copiar datos del FK al M2M
|
||||
migrations.RunPython(copiar_rfc_a_m2m, revertir_m2m_a_fk),
|
||||
# 4. Eliminar el FK temporal
|
||||
migrations.RemoveField(
|
||||
model_name='customuser',
|
||||
name='rfc_old',
|
||||
),
|
||||
]
|
||||
25
api/cuser/migrations/0006_customuser_active_organization.py
Normal file
25
api/cuser/migrations/0006_customuser_active_organization.py
Normal file
@@ -0,0 +1,25 @@
|
||||
import django.db.models.deletion
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('cuser', '0005_customuser_rfc_fk_to_m2m'),
|
||||
('organization', '0003_organizacion_apply_auto_download'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='customuser',
|
||||
name='active_organization',
|
||||
field=models.ForeignKey(
|
||||
blank=True,
|
||||
help_text='Solo superusuarios: organización activa para contexto de trabajo',
|
||||
null=True,
|
||||
on_delete=django.db.models.deletion.SET_NULL,
|
||||
related_name='superusers_activos',
|
||||
to='organization.organizacion',
|
||||
),
|
||||
),
|
||||
]
|
||||
Reference in New Issue
Block a user