Skip to content
0004_add_session_course_field.py 1.05 KiB
Newer Older
Andreas Klöckner's avatar
Andreas Klöckner committed
from django.db import migrations, models


def set_course(apps, schema_editor):
    broken_anonymous_sessions = []

    FlowSession = apps.get_model("course", "FlowSession")
    for session in FlowSession.objects.all():
        if session.participation is not None:
            session.course = session.participation.course
            session.save()
        else:
            broken_anonymous_sessions.append(session)

    for session in broken_anonymous_sessions:
        session.delete()


class Migration(migrations.Migration):

    dependencies = [
        ('course', '0003_add_instant_messaging'),
    ]

    operations = [
        migrations.AlterModelOptions(
            name='flowsession',
            options={'ordering': ('course', 'participation', '-start_time')},
        ),
        migrations.AddField(
            model_name='flowsession',
            name='course',
            field=models.ForeignKey(to='course.Course', null=True, on_delete=models.CASCADE),
            preserve_default=True,
        ),
        migrations.RunPython(set_course),
    ]