@staff_member_required
def select_key_frames(request, task_id, image_id):
    try:
        image_sequence = ImageSequence.objects.get(pk=image_id)
        task = Task.objects.get(pk=task_id)
    except ImageSequence.DoesNotExist:
        raise Http404('Image sequence does not exist')
    except Task.DoesNotExist:
        raise Http404('Task does not exist')
    if (request.method == 'POST'):
        frame_list = request.POST.getlist('frames')
        if (len(frame_list) == 0):
            messages.error(request, 'You must select at least 1 frame')
        else:
            try:
                annotation = ImageAnnotation.objects.get(image_id=image_id, task_id=task_id)
            except ImageAnnotation.DoesNotExist:
                annotation = ImageAnnotation()
                annotation.image_id = image_id
                annotation.task_id = task_id
                annotation.rejected = False
                annotation.user = request.user
                annotation.finished = False
                annotation.save()
            for frame_nr in frame_list:
                print(frame_nr)
                try:
                    key_frame = KeyFrameAnnotation.objects.get(image_annotation=annotation, frame_nr=frame_nr)
                except KeyFrameAnnotation.DoesNotExist:
                    key_frame = KeyFrameAnnotation()
                    key_frame.image_annotation = annotation
                    key_frame.frame_nr = frame_nr
                    key_frame.save()
                    if annotation.finished:
                        annotation.finished = False
                        annotation.save()
            to_delete = KeyFrameAnnotation.objects.filter(image_annotation=annotation).exclude(frame_nr__in=frame_list)
            deleted_count = len(to_delete)
            to_delete.delete()
            messages.success(request, (((('The ' + str(len(frame_list))) + ' key frames were stored. ') + str(deleted_count)) + ' key frames were deleted.'))
            return redirect('task', task_id)
    else:
        frames = KeyFrameAnnotation.objects.filter(image_annotation__image=image_sequence, image_annotation__task=task)
        return render(request, 'annotationweb/add_key_frames.html', {'image_sequence': image_sequence, 'task': task, 'frames': frames})
@staff_member_required
def select_key_frames(request, task_id, image_id):
    try:
        image_sequence = ImageSequence.objects.get(pk=image_id)
        task = Task.objects.get(pk=task_id)
    except ImageSequence.DoesNotExist:
        raise Http404('Image sequence does not exist')
    except Task.DoesNotExist:
        raise Http404('Task does not exist')
    if (request.method == 'POST'):
        frame_list = request.POST.getlist('frames')
        if (len(frame_list) == 0):
            messages.error(request, 'You must select at least 1 frame')
        else:
            try:
                annotation = ImageAnnotation.objects.get(image_id=image_id, task_id=task_id)
            except ImageAnnotation.DoesNotExist:
                annotation = ImageAnnotation()
                annotation.image_id = image_id
                annotation.task_id = task_id
                annotation.rejected = False
                annotation.user = request.user
                annotation.finished = False
                annotation.save()
            for frame_nr in frame_list:
                print(frame_nr)
                try:
                    key_frame = KeyFrameAnnotation.objects.get(image_annotation=annotation, frame_nr=frame_nr)
                except KeyFrameAnnotation.DoesNotExist:
                    key_frame = KeyFrameAnnotation()
                    key_frame.image_annotation = annotation
                    key_frame.frame_nr = frame_nr
                    key_frame.save()
                    if annotation.finished:
                        annotation.finished = False
                        annotation.save()
            to_delete = KeyFrameAnnotation.objects.filter(image_annotation=annotation).exclude(frame_nr__in=frame_list)
            deleted_count = len(to_delete)
            to_delete.delete()
            messages.success(request, (((('The ' + str(len(frame_list))) + ' key frames were stored. ') + str(deleted_count)) + ' key frames were deleted.'))
            return redirect('task', task_id)
    else:
        frames = KeyFrameAnnotation.objects.filter(image_annotation__image=image_sequence, image_annotation__task=task)
        return render(request, 'annotationweb/add_key_frames.html', {'image_sequence': image_sequence, 'task': task, 'frames': frames})