# Generated by Django 4.0.6 on 2022-08-19 10:14

from django.db import migrations


class Migration(migrations.Migration):

    dependencies = [
        ('question', '0013_manually_fix_question_choices'),
    ]

    # On 2022.11.20 It took 7 minutes to perform this migration on HAROON's local machine
    def modify(apps, schema_editor):

        Question = apps.get_model("question", "Question")

        print("reading Question data from database")

        db_alias = schema_editor.connection.alias
        questions = Question.objects.using(db_alias).all()

        i = 0
        count = len(questions)
        previous_percent = -1

        for question in questions:

            percent = 100 * i // count
            if previous_percent != percent:
                print(str(percent) + "%")
                previous_percent = percent
            i += 1

            if question.question_type == 'multiple-choice':

                question.input_rule = {
                    "max_selectable_choices": question.answer_rules.get('max_selectable_choices') or len(question.correct_choices)
                }
            elif question.question_type == 'descriptive':
                question.input_rule = {
                    "max_characters": question.answer_rules.get('max_characters') or 1000, "attachment": question.answer_rules.get("general_file_available") or False}
            question.save()

    operations = [
        migrations.RunPython(modify),
    ]
