kpsul/cof/migrations/0014_move_profile.py

148 lines
4.3 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
from django.db.models import F
def profiles_to_gestion(apps, schema_editor):
OldProfile = apps.get_model('cof', 'CofProfile')
NewProfile = apps.get_model('gestion', 'Profile')
# New profiles are massively imported from the old profiles.
# IDs are kept identical to ease the migration of models which were
# referencing the CofProfile model.
new_profiles = []
for old_p in OldProfile.objects.values().iterator():
new_profiles.append(
NewProfile(
id=old_p['id'],
user_id=old_p['user_id'],
login_clipper=old_p['login_clipper'],
phone=old_p['phone'],
occupation=old_p['occupation'],
departement=old_p['departement'],
comments=old_p['comments'],
)
)
NewProfile.objects.bulk_create(new_profiles)
OldProfile.objects.all().update(profile_id=F('id'))
def cof_status_to_gestion(apps, schema_editor):
Association = apps.get_model('gestion', 'Association')
CofProfile = apps.get_model('cof', 'CofProfile')
cof_assoc = Association.objects.get(name='COF')
cof_pks = (
CofProfile.objects
.select_related('profile')
.values_list('profile__user_id', flat=True)
)
cof_assoc.members_group.user_set.add(
*cof_pks.filter(is_cof=True)
)
cof_assoc.staff_group.user_set.add(
*cof_pks.filter(is_buro=True)
)
class Migration(migrations.Migration):
"""
BDS support changes how users data is organized.
Data is migrated to the new schema.
"""
dependencies = [
('cof', '0013_pei'),
('gestion', '0002_create_cof_bds'),
# Migrate the K-Fêt app up to the pre-BDS state before performing the
# BDS-related stuff
('kfet', '0061_add_perms_config'),
]
operations = [
# Temporarly authorize 'profile' as nullable to allow migrating data.
migrations.AddField(
model_name='cofprofile',
name='profile',
field=models.OneToOneField(
on_delete=models.CASCADE,
to='gestion.Profile',
null=True,
related_name='cof'
),
preserve_default=False,
),
migrations.RunPython(profiles_to_gestion),
# Data is migrated, unset nullable on 'profile'.
migrations.AlterField(
model_name='cofprofile',
name='profile',
field=models.OneToOneField(
on_delete=models.CASCADE,
to='gestion.Profile',
related_name='cof'
),
),
# Remove fields no longer used.
migrations.RemoveField(
model_name='cofprofile',
name='user',
),
migrations.RemoveField(
model_name='cofprofile',
name='comments',
),
migrations.RemoveField(
model_name='cofprofile',
name='departement',
),
migrations.RemoveField(
model_name='cofprofile',
name='login_clipper',
),
migrations.RemoveField(
model_name='cofprofile',
name='occupation',
),
migrations.RemoveField(
model_name='cofprofile',
name='phone',
),
# Keep cof member/staff status.
migrations.RunPython(cof_status_to_gestion),
# Remove the last no longer used fields.
migrations.RemoveField(
model_name='cofprofile',
name='is_cof',
),
migrations.RemoveField(
model_name='cofprofile',
name='is_buro',
),
# Now we are safe, let's do basic operations.
migrations.AlterModelOptions(
name='cofprofile',
options={
'permissions': (('member', 'Is a COF member'),
('buro', 'Is part of COF staff')),
'verbose_name': 'Profil COF',
'verbose_name_plural': 'Profils COF'},
),
migrations.RenameField(
model_name='cofprofile',
old_name='mailing_cof',
new_name='mailing',
),
]