def snapshots_to_iterations(apps, schema_editor):
    Iteration = apps.get_model('orchestra', 'Iteration')
    Task = apps.get_model('orchestra', 'Task')
    TaskAssignment = apps.get_model('orchestra', 'TaskAssignment')

    for task in Task.objects.all():
        task_snapshots = get_ordered_snapshots(task)
        for task_snap_index, snapshot in enumerate(task_snapshots):
            assignment = snapshot['assignment']
            iteration = Iteration.objects.create(
                assignment=assignment,
                end_datetime=parse(snapshot['datetime']),
                submitted_data=snapshot['data'],
                status=snapshot_type_to_iteration_status[snapshot['type']])
            if snapshot['assignment_snap_index'] == 0:
                # Snapshot is the first for its assignment, so its start
                # datetime will be the same as its assignment
                # NOTE: This should cover the case where task_snap_index == 0
                iteration.start_datetime = assignment.start_datetime
            else:
                iteration.start_datetime = (
                    task_snapshots[task_snap_index - 1]['datetime'])
            iteration.save()

        processing_assignments = task.assignments.filter(
            status=AssignmentStatus.PROCESSING)
        if processing_assignments.exists():
            if processing_assignments.count() > 1:
                logger.error(
                    'Task {} has too many processing assignments'.format(
                        task.id))
            else:
                # If task has a processing assignment, create a final
                # processing iteration
                processing_assignment = processing_assignments.first()
                if (not task_snapshots
                        or not processing_assignment.iterations.exists()):
                    final_start_datetime = processing_assignment.start_datetime
                else:
                    final_start_datetime = task_snapshots[-1]['datetime']
                iteration = Iteration.objects.create(
                    assignment=processing_assignment,
                    start_datetime=final_start_datetime,
                    status=IterationStatus.PROCESSING)

        try:
            verify_iterations(task.id)
        except __HOLE__:
            logger.error('Iterations invalid for task {}.'.format(task.id))
dataset/ETHPy150Open unlimitedlabs/orchestra/orchestra/migrations/0028_snapshots_to_iterations.py/snapshots_to_iterations