diff --git a/cof/migrations/0012_remove_club.py b/cof/migrations/0012_remove_club.py new file mode 100644 index 00000000..ebc48d37 --- /dev/null +++ b/cof/migrations/0012_remove_club.py @@ -0,0 +1,23 @@ +from django.db import migrations + + +class Migration(migrations.Migration): + + dependencies = [ + ('cof', '0011_delete_clipper_and_custommail'), + ('gestion', '0002_club_support') + ] + + operations = [ + migrations.RemoveField( + model_name='club', + name='membres', + ), + migrations.RemoveField( + model_name='club', + name='respos', + ), + migrations.DeleteModel( + name='Club', + ), + ] diff --git a/cof/models.py b/cof/models.py index dafe9996..1d1f5b07 100644 --- a/cof/models.py +++ b/cof/models.py @@ -79,18 +79,6 @@ class CofProfile(models.Model): return self.profile.user.username -@python_2_unicode_compatible -class Club(models.Model): - name = models.CharField("Nom", max_length=200, unique=True) - description = models.TextField("Description", blank=True) - respos = models.ManyToManyField(User, related_name="clubs_geres", - blank=True) - membres = models.ManyToManyField(User, related_name="clubs", blank=True) - - def __str__(self): - return self.name - - @python_2_unicode_compatible class Event(models.Model): title = models.CharField("Titre", max_length=200) diff --git a/gestion/migrations/0002_club_support.py b/gestion/migrations/0002_club_support.py new file mode 100644 index 00000000..6b23882b --- /dev/null +++ b/gestion/migrations/0002_club_support.py @@ -0,0 +1,95 @@ +# -*- 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.RunPython(lambda: 1/0), + 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')), + ('user', models.ForeignKey(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), + ] diff --git a/gestion/models.py b/gestion/models.py index 81335ddc..98c06d0e 100644 --- a/gestion/models.py +++ b/gestion/models.py @@ -1,4 +1,4 @@ -from django.contrib.auth.models import User +from django.contrib.auth.models import User, Group from django.db import models from django.dispatch import receiver from django.db.models.signals import post_save, post_delete @@ -17,6 +17,7 @@ OCCUPATION_CHOICES = ( ('CST', _("CST")), ) + class Profile(models.Model): user = models.OneToOneField(User, related_name="profile") @@ -39,6 +40,7 @@ class Profile(models.Model): def __str__(self): return self.user.username + @receiver(post_save, sender=User) def create_user_profile(sender, instance, created, **kwargs): if created: @@ -48,3 +50,53 @@ def create_user_profile(sender, instance, created, **kwargs): @receiver(post_delete, sender=Profile) def post_delete_user(sender, instance, *args, **kwargs): instance.user.delete() + + +class Club(models.Model): + ANNUAL = "ANN" + SEMESTER = "SEM" + COURSE = "COU" + + COTISATION_FREQUENCY_CHOICES = [ + (ANNUAL, _("Annuel")), + (SEMESTER, _("Semestriel")), + (COURSE, _("Au cours")) + ] + + associations = models.ManyToManyField(Group, related_name="clubs") + name = models.CharField(_("Nom"), max_length=200, unique=True) + description = models.TextField("Description", blank=True) + members = models.ManyToManyField( + User, + through="ClubUser", + related_name="in_clubs", + blank=True + ) + price = models.DecimalField( + _("Cotisation (€)"), + decimal_places=2, + max_digits=5, + blank=True, + default=0 + ) + cotisation_frequency = models.CharField( + _("Fréquence de la cotisation"), + default=ANNUAL, + choices=COTISATION_FREQUENCY_CHOICES, + max_length=3, + blank=True + ) + + def __str__(self): + template = ( + self.price and "{name} ({price}€ / {cotisation_frequency})" + or "{name}" + ) + return template.format(**vars(self)) + + +class ClubUser(models.Model): + user = models.ForeignKey(User) + club = models.ForeignKey(Club) + is_respo = models.BooleanField(_("Est responsable du club")) + has_paid = models.BooleanField(_("A payé sa cotisation"))