2017-02-10 20:10:45 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
from django.db import migrations, models
|
2017-09-05 14:41:13 +02:00
|
|
|
from django.db.models import F
|
2017-02-10 20:10:45 +01:00
|
|
|
|
|
|
|
|
2017-09-05 14:41:13 +02:00
|
|
|
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'],
|
|
|
|
)
|
2017-02-10 20:10:45 +01:00
|
|
|
)
|
|
|
|
|
2017-09-05 14:41:13 +02:00
|
|
|
NewProfile.objects.bulk_create(new_profiles)
|
2017-02-10 20:10:45 +01:00
|
|
|
|
2017-09-05 14:41:13 +02:00
|
|
|
OldProfile.objects.all().update(profile_id=F('id'))
|
2017-02-11 23:05:51 +01:00
|
|
|
|
|
|
|
|
2017-09-05 14:41:13 +02:00
|
|
|
def cof_status_to_gestion(apps, schema_editor):
|
|
|
|
Association = apps.get_model('gestion', 'Association')
|
|
|
|
CofProfile = apps.get_model('cof', 'CofProfile')
|
2017-02-11 23:05:51 +01:00
|
|
|
|
2017-09-05 14:41:13 +02:00
|
|
|
cof_assoc = Association.objects.get(name='COF')
|
2017-02-11 23:05:51 +01:00
|
|
|
|
2017-09-05 14:41:13 +02:00
|
|
|
cof_pks = (
|
|
|
|
CofProfile.objects
|
|
|
|
.select_related('profile')
|
|
|
|
.values_list('profile__user_id', flat=True)
|
|
|
|
)
|
2017-02-10 20:10:45 +01:00
|
|
|
|
2017-09-05 14:41:13 +02:00
|
|
|
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)
|
|
|
|
)
|
2017-02-11 23:05:51 +01:00
|
|
|
|
2017-02-10 20:10:45 +01:00
|
|
|
|
|
|
|
class Migration(migrations.Migration):
|
2017-09-05 14:41:13 +02:00
|
|
|
"""
|
|
|
|
BDS support changes how users data is organized.
|
|
|
|
Data is migrated to the new schema.
|
|
|
|
|
|
|
|
"""
|
2017-02-10 20:10:45 +01:00
|
|
|
|
|
|
|
dependencies = [
|
2017-10-26 11:06:17 +02:00
|
|
|
('cof', '0013_pei'),
|
2017-09-05 14:41:13 +02:00
|
|
|
('gestion', '0002_create_cof_bds'),
|
2017-10-26 11:06:17 +02:00
|
|
|
# Migrate the K-Fêt app up to the pre-BDS state before performing the
|
|
|
|
# BDS-related stuff
|
|
|
|
('kfet', '0061_add_perms_config'),
|
2017-02-10 20:10:45 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
operations = [
|
2017-09-05 14:41:13 +02:00
|
|
|
# Temporarly authorize 'profile' as nullable to allow migrating data.
|
2017-02-10 20:10:45 +01:00
|
|
|
migrations.AddField(
|
|
|
|
model_name='cofprofile',
|
|
|
|
name='profile',
|
|
|
|
field=models.OneToOneField(
|
2017-02-22 19:28:34 +01:00
|
|
|
on_delete=models.CASCADE,
|
2017-02-10 20:10:45 +01:00
|
|
|
to='gestion.Profile',
|
|
|
|
null=True,
|
|
|
|
related_name='cof'
|
|
|
|
),
|
|
|
|
preserve_default=False,
|
|
|
|
),
|
2017-09-05 14:41:13 +02:00
|
|
|
migrations.RunPython(profiles_to_gestion),
|
|
|
|
# Data is migrated, unset nullable on 'profile'.
|
2017-02-11 23:05:51 +01:00
|
|
|
migrations.AlterField(
|
|
|
|
model_name='cofprofile',
|
|
|
|
name='profile',
|
|
|
|
field=models.OneToOneField(
|
2017-02-22 19:28:34 +01:00
|
|
|
on_delete=models.CASCADE,
|
2017-02-11 23:05:51 +01:00
|
|
|
to='gestion.Profile',
|
|
|
|
related_name='cof'
|
|
|
|
),
|
|
|
|
),
|
2017-09-05 14:41:13 +02:00
|
|
|
# Remove fields no longer used.
|
|
|
|
migrations.RemoveField(
|
|
|
|
model_name='cofprofile',
|
|
|
|
name='user',
|
|
|
|
),
|
2017-02-10 20:10:45 +01:00
|
|
|
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',
|
|
|
|
),
|
2017-09-05 14:41:13 +02:00
|
|
|
|
|
|
|
# Keep cof member/staff status.
|
|
|
|
migrations.RunPython(cof_status_to_gestion),
|
|
|
|
# Remove the last no longer used fields.
|
2017-02-11 23:05:51 +01:00
|
|
|
migrations.RemoveField(
|
2017-02-10 20:10:45 +01:00
|
|
|
model_name='cofprofile',
|
2017-02-11 23:05:51 +01:00
|
|
|
name='is_cof',
|
|
|
|
),
|
|
|
|
migrations.RemoveField(
|
|
|
|
model_name='cofprofile',
|
|
|
|
name='is_buro',
|
2017-02-10 20:10:45 +01:00
|
|
|
),
|
2017-09-05 14:41:13 +02:00
|
|
|
|
|
|
|
# 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(
|
2017-02-10 20:10:45 +01:00
|
|
|
model_name='cofprofile',
|
2017-09-05 14:41:13 +02:00
|
|
|
old_name='mailing_cof',
|
|
|
|
new_name='mailing',
|
2017-02-10 20:10:45 +01:00
|
|
|
),
|
|
|
|
]
|