# -*- coding: utf-8 -*-
# Generated by Django 1.11.18 on 2019-04-29 15:44


import logging

from django.db import migrations


def update_backend(apps, schema_editor):
    from django.conf import settings
    log = logging.getLogger(__name__)
    ProctoredExam = apps.get_model('edx_proctoring', 'ProctoredExam')
    default_backend = settings.PROCTORING_BACKENDS.get('DEFAULT', None)
    if default_backend:
        for exam in ProctoredExam.objects.filter(backend__isnull=True):
            exam.backend = default_backend
            exam.save()
            log.info("updated exam %d with backend %s", exam.id, default_backend)
    else:
        log.warning("settings.PROCTORING_BACKENDS['DEFAULT'] is not set. Unable to do migration.")


class Migration(migrations.Migration):

    dependencies = [
        ('edx_proctoring', '0009_proctoredexamreviewpolicy_remove_rules'),
    ]

    operations = [
        migrations.RunPython(update_backend, migrations.RunPython.noop)
    ]
