kpsul/gestion/migrations/0002_club_support.py

99 lines
3.7 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
from django.conf import settings
def keep_cof_clubs(apps, schema_editor):
Group = apps.get_model("auth", "Group")
CofClub = apps.get_model("cof", "Club")
NewClub = apps.get_model("gestion", "Club")
Registration = apps.get_model("gestion", "ClubUser")
cof_group = Group.objects.get(name="cof_members")
for oldclub in CofClub.objects.all():
newclub = NewClub.objects.create(
name=oldclub.name,
description=oldclub.description,
)
for user in oldclub.membres.all():
Registration.objects.create(
club=newclub,
user=user,
has_paid=True
)
for user in oldclub.respos.all():
reg = Registration.objects.get_or_create(
user=user,
club=newclub
)
reg.is_respo = True
reg.save()
newclub.associations.add(cof_group)
newclub.save()
def restore_cof_clubs(apps, schema_editor):
Group = apps.get_model("auth", "Group")
Club = apps.get_model("cof", "Club")
Registration = apps.get_model("gestion", "ClubUser")
cof_group = Group.objects.get(name="cof_members")
for newclub in cof_group.clubs.all():
club = Club.objects.create(
name=newclub.name,
description=newclub.description
)
for reg in Registration.objects.filter(club=newclub):
if reg.is_respo:
club.respos.add(reg.user)
else:
club.membres.add(reg.user)
class Migration(migrations.Migration):
dependencies = [
('auth', '0006_require_contenttypes_0002'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('cof', '0011_delete_clipper_and_custommail'),
('gestion', '0001_initial'),
]
operations = [
migrations.CreateModel(
name='Club',
fields=[
('id', models.AutoField(primary_key=True, verbose_name='ID', auto_created=True, serialize=False)),
('name', models.CharField(unique=True, max_length=200, verbose_name='Nom')),
('description', models.TextField(verbose_name='Description', blank=True)),
('price', models.DecimalField(verbose_name='Cotisation (€)', decimal_places=2, default=0, blank=True, max_digits=5)),
('cotisation_frequency', models.CharField(choices=[('ANN', 'Annuel'), ('SEM', 'Semestriel'), ('COU', 'Au cours')], max_length=3, verbose_name='Fréquence de la cotisation', default='ANN', blank=True)),
('associations', models.ManyToManyField(to='auth.Group', related_name='clubs')),
],
),
migrations.CreateModel(
name='ClubUser',
fields=[
('id', models.AutoField(primary_key=True, verbose_name='ID', auto_created=True, serialize=False)),
('is_respo', models.BooleanField(verbose_name='Est responsable du club')),
('has_paid', models.BooleanField(verbose_name='A payé sa cotisation')),
('club', models.ForeignKey(
to='gestion.Club',
on_delete=models.CASCADE)),
('user', models.ForeignKey(
on_delete=models.CASCADE,
to=settings.AUTH_USER_MODEL)),
],
),
migrations.AddField(
model_name='club',
name='members',
field=models.ManyToManyField(to=settings.AUTH_USER_MODEL, related_name='in_clubs', through='gestion.ClubUser', blank=True),
),
migrations.RunPython(keep_cof_clubs, restore_cof_clubs),
]