gestioCOF/cof/migrations/0009_generic_profiles.py

129 lines
3.7 KiB
Python

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.contrib.auth.models import Group, Permission
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):
from django.contrib.auth.management import create_permissions
apps.models_module = True
create_permissions(apps, verbosity=0)
apps.models_module = None
CofProfile = apps.get_model("cof", "CofProfile")
memberp = Permission.objects.get(codename='member')
burop = Permission.objects.get(codename='buro')
# creates the groups for COF members and
member = Group.objects.create(name='cof_members')
buro = Group.objects.create(name='cof_buro')
# associate permissions to the respective groups.
buro.permissions = [burop, memberp]
member.permissions = [memberp]
for cofp in CofProfile.objects.filter(is_cof=True):
cofp.profile.user.groups.add(member)
for cofp in CofProfile.objects.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(
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(
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',
),
]