kpsul/cof/migrations/0009_generic_profiles.py
2017-08-06 18:28:07 +01:00

120 lines
3.4 KiB
Python

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.contrib.auth.models import Group
from django.db import migrations, models
def create_profile(apps, schema_editor):
CofProfile = apps.get_model("cof", "CofProfile")
Profile = apps.get_model("gestion", "Profile")
for p in CofProfile.objects.all():
profile = Profile.objects.create(
id=p.id,
user=p.user,
login_clipper=p.login_clipper,
phone=p.phone,
occupation=p.occupation,
departement=p.departement,
comments=p.comments
)
p.profile = profile
p.save()
def preserve_perms(apps, schema_editor):
COFProfile = apps.get_model("cof", "CofProfile")
# create the groups for COF members and staff
member = Group.objects.create(name='cof_members')
buro = Group.objects.create(name='cof_buro')
cprofiles = COFProfile.objects.select_related("profile__user")
for cofp in cprofiles.filter(is_cof=True):
cofp.profile.user.groups.add(member)
for cofp in cprofiles.filter(is_buro=True):
cofp.profile.user.groups.add(buro)
def remove_profile(apps, schema_editor):
raise NotImplementedError
fuckup_perms = remove_profile
class Migration(migrations.Migration):
dependencies = [
('gestion', '0001_initial'),
('cof', '0008_py3'),
]
operations = [
migrations.RenameField(
model_name='cofprofile',
old_name='mailing_cof',
new_name='mailing',
),
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.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.RunPython(create_profile, remove_profile),
migrations.AlterField(
model_name='cofprofile',
name='profile',
field=models.OneToOneField(
on_delete=models.CASCADE,
to='gestion.Profile',
related_name='cof'
),
),
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',
),
migrations.RunPython(preserve_perms, fuckup_perms),
migrations.RemoveField(
model_name='cofprofile',
name='is_cof',
),
migrations.RemoveField(
model_name='cofprofile',
name='is_buro',
),
migrations.RemoveField(
model_name='cofprofile',
name='user',
),
]