2016-06-06 11:05:33 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
|
|
|
from django.db import migrations, models
|
|
|
|
from django.conf import settings
|
|
|
|
from django.utils import timezone
|
|
|
|
|
2017-10-17 14:41:53 +02:00
|
|
|
|
|
|
|
def fill_tirage_fields(apps, schema_editor):
|
|
|
|
"""
|
|
|
|
Create a `Tirage` to fill new field `tirage` of `Participant`
|
|
|
|
and `Spectacle` already existing.
|
|
|
|
"""
|
|
|
|
Participant = apps.get_model("bda", "Participant")
|
|
|
|
Spectacle = apps.get_model("bda", "Spectacle")
|
2016-06-06 11:05:33 +02:00
|
|
|
Tirage = apps.get_model("bda", "Tirage")
|
2017-10-17 14:41:53 +02:00
|
|
|
|
|
|
|
# These querysets only contains instances not linked to any `Tirage`.
|
|
|
|
participants = Participant.objects.filter(tirage=None)
|
|
|
|
spectacles = Spectacle.objects.filter(tirage=None)
|
|
|
|
|
|
|
|
if not participants.count() and not spectacles.count():
|
|
|
|
# No need to create a "trash" tirage.
|
|
|
|
return
|
|
|
|
|
|
|
|
tirage = Tirage.objects.create(
|
|
|
|
title="Tirage de test (migration)",
|
|
|
|
active=False,
|
|
|
|
ouverture=timezone.now(),
|
|
|
|
fermeture=timezone.now(),
|
|
|
|
)
|
|
|
|
|
|
|
|
participants.update(tirage=tirage)
|
|
|
|
spectacles.update(tirage=tirage)
|
|
|
|
|
2016-06-06 11:05:33 +02:00
|
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
|
|
|
|
|
|
dependencies = [
|
|
|
|
('bda', '0001_initial'),
|
|
|
|
]
|
|
|
|
|
|
|
|
operations = [
|
|
|
|
migrations.CreateModel(
|
|
|
|
name='Tirage',
|
|
|
|
fields=[
|
|
|
|
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
|
|
|
|
('title', models.CharField(max_length=300, verbose_name=b'Titre')),
|
|
|
|
('ouverture', models.DateTimeField(verbose_name=b"Date et heure d'ouverture du tirage")),
|
|
|
|
('fermeture', models.DateTimeField(verbose_name=b'Date et heure de fermerture du tirage')),
|
|
|
|
('token', models.TextField(verbose_name=b'Graine du tirage', blank=True)),
|
|
|
|
('active', models.BooleanField(default=True, verbose_name=b'Tirage actif')),
|
|
|
|
],
|
|
|
|
),
|
|
|
|
migrations.AlterField(
|
|
|
|
model_name='participant',
|
|
|
|
name='user',
|
2017-11-19 18:41:39 +01:00
|
|
|
field=models.ForeignKey(to=settings.AUTH_USER_MODEL, on_delete=models.CASCADE),
|
2016-06-06 11:05:33 +02:00
|
|
|
),
|
2017-10-17 14:41:53 +02:00
|
|
|
# Create fields `spectacle` for `Participant` and `Spectacle` models.
|
|
|
|
# These fields are not nullable, but we first create them as nullable
|
|
|
|
# to give a default value for existing instances of these models.
|
2016-06-06 11:05:33 +02:00
|
|
|
migrations.AddField(
|
|
|
|
model_name='participant',
|
|
|
|
name='tirage',
|
2017-11-19 18:41:39 +01:00
|
|
|
field=models.ForeignKey(to='bda.Tirage', null=True, on_delete=models.CASCADE),
|
2016-06-06 11:05:33 +02:00
|
|
|
),
|
|
|
|
migrations.AddField(
|
|
|
|
model_name='spectacle',
|
|
|
|
name='tirage',
|
2017-11-19 18:41:39 +01:00
|
|
|
field=models.ForeignKey(to='bda.Tirage', null=True, on_delete=models.CASCADE),
|
2017-10-17 14:41:53 +02:00
|
|
|
),
|
|
|
|
migrations.RunPython(fill_tirage_fields, migrations.RunPython.noop),
|
|
|
|
migrations.AlterField(
|
|
|
|
model_name='participant',
|
|
|
|
name='tirage',
|
2017-11-19 18:41:39 +01:00
|
|
|
field=models.ForeignKey(to='bda.Tirage', on_delete=models.CASCADE),
|
2017-10-17 14:41:53 +02:00
|
|
|
),
|
|
|
|
migrations.AlterField(
|
|
|
|
model_name='spectacle',
|
|
|
|
name='tirage',
|
2017-11-19 18:41:39 +01:00
|
|
|
field=models.ForeignKey(to='bda.Tirage', on_delete=models.CASCADE),
|
2016-06-06 11:05:33 +02:00
|
|
|
),
|
|
|
|
]
|