def task(request, task_id):
    try:
        task = Task.objects.get(pk=task_id)
    except Task.DoesNotExist:
        return Http404('The Task does not exist')
    search_filters = SearchFilter(request, task)
    if (request.method == 'POST'):
        form = search_filters.create_form(data=request.POST)
    else:
        form = search_filters.create_form()
    queryset = ImageSequence.objects.all()
    sort_by = search_filters.get_value('sort_by')
    subjects_selected = search_filters.get_value('subject')
    users_selected = search_filters.get_value('user')
    image_quality = search_filters.get_value('image_quality')
    metadata = search_filters.get_value('metadata')
    if (len(metadata) > 0):
        metadata_dict = {}
        for item in metadata:
            parts = item.split(': ')
            if (len(parts) != 2):
                raise Exception('Error: must be 2 parts')
            name = parts[0]
            value = parts[1]
            if (name in metadata_dict.keys()):
                metadata_dict[name].append(value)
            else:
                metadata_dict[name] = [value]
        for (name, values) in metadata_dict.items():
            queryset = queryset.filter(imagemetadata__name=name, imagemetadata__value__in=values)
    if (sort_by == ImageListForm.SORT_IMAGE_ID):
        queryset = queryset.filter(subject__dataset__task=task, subject__in=subjects_selected)
    elif (sort_by == ImageListForm.SORT_NOT_ANNOTATED_IMAGE_ID):
        queryset = queryset.filter(subject__dataset__task=task, subject__in=subjects_selected).exclude(imageannotation__task=task, imageannotation__finished=True)
    else:
        if (task.type == Task.CLASSIFICATION):
            labels_selected = search_filters.get_value('label')
            queryset = queryset.filter(imageannotation__image_quality__in=image_quality, imageannotation__task=task, imageannotation__finished=True, imageannotation__user__in=users_selected, imageannotation__keyframeannotation__imagelabel__in=labels_selected, subject__in=subjects_selected)
        else:
            queryset = queryset.filter(imageannotation__image_quality__in=image_quality, imageannotation__task=task, imageannotation__finished=True, imageannotation__user__in=users_selected, subject__in=subjects_selected)
        if (sort_by == ImageListForm.SORT_DATE_DESC):
            queryset = queryset.order_by('-imageannotation__date')
        else:
            queryset = queryset.order_by('imageannotation__date')
    paginator = Paginator(queryset, 12)
    page = request.GET.get('page')
    try:
        images = paginator.page(page)
    except PageNotAnInteger:
        images = paginator.page(1)
    except EmptyPage:
        images = paginator.page(paginator.num_pages)
    for image in images:
        try:
            image.annotation = ImageAnnotation.objects.get(image=image, task=task)
            image.annotation_frames = KeyFrameAnnotation.objects.filter(image_annotation=image.annotation)
        except:
            pass
    return_url = reverse('task', kwargs={'task_id': task_id})
    if (page is not None):
        return_url += ('?page=' + str(page))
    request.session['return_to_url'] = return_url
    return render(request, 'annotationweb/task.html', {'images': images, 'task': task, 'form': form})
def task(request, task_id):
    try:
        task = Task.objects.get(pk=task_id)
    except Task.DoesNotExist:
        return Http404('The Task does not exist')
    search_filters = SearchFilter(request, task)
    if (request.method == 'POST'):
        form = search_filters.create_form(data=request.POST)
    else:
        form = search_filters.create_form()
    queryset = ImageSequence.objects.all()
    sort_by = search_filters.get_value('sort_by')
    subjects_selected = search_filters.get_value('subject')
    users_selected = search_filters.get_value('user')
    image_quality = search_filters.get_value('image_quality')
    metadata = search_filters.get_value('metadata')
    if (len(metadata) > 0):
        metadata_dict = {}
        for item in metadata:
            parts = item.split(': ')
            if (len(parts) != 2):
                raise Exception('Error: must be 2 parts')
            name = parts[0]
            value = parts[1]
            if (name in metadata_dict.keys()):
                metadata_dict[name].append(value)
            else:
                metadata_dict[name] = [value]
        for (name, values) in metadata_dict.items():
            queryset = queryset.filter(imagemetadata__name=name, imagemetadata__value__in=values)
    if (sort_by == ImageListForm.SORT_IMAGE_ID):
        queryset = queryset.filter(subject__dataset__task=task, subject__in=subjects_selected)
    elif (sort_by == ImageListForm.SORT_NOT_ANNOTATED_IMAGE_ID):
        queryset = queryset.filter(subject__dataset__task=task, subject__in=subjects_selected).exclude(imageannotation__task=task, imageannotation__finished=True)
    else:
        if (task.type == Task.CLASSIFICATION):
            labels_selected = search_filters.get_value('label')
            queryset = queryset.filter(imageannotation__image_quality__in=image_quality, imageannotation__task=task, imageannotation__finished=True, imageannotation__user__in=users_selected, imageannotation__keyframeannotation__imagelabel__in=labels_selected, subject__in=subjects_selected)
        else:
            queryset = queryset.filter(imageannotation__image_quality__in=image_quality, imageannotation__task=task, imageannotation__finished=True, imageannotation__user__in=users_selected, subject__in=subjects_selected)
        if (sort_by == ImageListForm.SORT_DATE_DESC):
            queryset = queryset.order_by('-imageannotation__date')
        else:
            queryset = queryset.order_by('imageannotation__date')
    paginator = Paginator(queryset, 12)
    page = request.GET.get('page')
    try:
        images = paginator.page(page)
    except PageNotAnInteger:
        images = paginator.page(1)
    except EmptyPage:
        images = paginator.page(paginator.num_pages)
    for image in images:
        try:
            image.annotation = ImageAnnotation.objects.get(image=image, task=task)
            image.annotation_frames = KeyFrameAnnotation.objects.filter(image_annotation=image.annotation)
        except:
            pass
    return_url = reverse('task', kwargs={'task_id': task_id})
    if (page is not None):
        return_url += ('?page=' + str(page))
    request.session['return_to_url'] = return_url
    return render(request, 'annotationweb/task.html', {'images': images, 'task': task, 'form': form})