From 803b0d1abc58721b800fc64badae3a0f0674b33c Mon Sep 17 00:00:00 2001 From: Timon Engelke Date: Tue, 19 Feb 2019 01:58:36 +0100 Subject: [PATCH 1/3] Database improvements on image set page --- .../images/templates/images/imageset.html | 4 +-- imagetagger/imagetagger/images/views.py | 32 ++++--------------- 2 files changed, 9 insertions(+), 27 deletions(-) diff --git a/imagetagger/imagetagger/images/templates/images/imageset.html b/imagetagger/imagetagger/images/templates/images/imageset.html index 15a59edc..bba6f048 100644 --- a/imagetagger/imagetagger/images/templates/images/imageset.html +++ b/imagetagger/imagetagger/images/templates/images/imageset.html @@ -210,9 +210,9 @@

{{imageset.name}} {{ {{annotationcount}}
diff --git a/imagetagger/imagetagger/images/views.py b/imagetagger/imagetagger/images/views.py index 66697ed5..a6e09971 100644 --- a/imagetagger/imagetagger/images/views.py +++ b/imagetagger/imagetagger/images/views.py @@ -3,7 +3,7 @@ from django.contrib.auth.decorators import login_required from django.core.paginator import Paginator from django.db import transaction -from django.db.models import Count, Q +from django.db.models import Count, Q, Sum from django.db.models.expressions import F from django.views.decorators.http import require_http_methods from django.urls import reverse @@ -388,18 +388,6 @@ def delete_images(request, image_id): return redirect(reverse('annotations:annotate', args=(next_image,))) -def count_annotations_of_type(annotations, annotation_type): - annotations = annotations.filter(annotation_type=annotation_type) - annotation_count = annotations.count() - neg_annotation_count = annotations.filter(vector=None).count() - return ( - annotation_type.name, - annotation_count, - annotation_count - neg_annotation_count, - neg_annotation_count, - ) - - @login_required def view_imageset(request, image_set_id): imageset = get_object_or_404(ImageSet, id=image_set_id) @@ -419,30 +407,24 @@ def view_imageset(request, image_set_id): annotations__annotation_type_id=request.POST.get("selected_annotation_type")) # a list of annotation types used in the imageset all_annotation_types = AnnotationType.objects.filter(active=True) - annotation_types = set() - annotations = Annotation.objects.select_related().filter( + annotations = Annotation.objects.filter( image__in=images, annotation_type__active=True).order_by("id") - annotation_types = annotation_types.union( - [annotation.annotation_type for annotation in annotations]) - annotation_type_count = sorted(list( - map( - lambda at: count_annotations_of_type(annotations, at), - annotation_types)), - key=lambda at_tuple: at_tuple[1], - reverse=True) + annotation_types = AnnotationType.objects.filter(annotation__image__image_set=imageset, active=True).distinct()\ + .annotate(count=Count('annotation'), + in_image_count=Count('annotation', filter=Q(annotation__vector__isnull=False)), + not_in_image_count=Count('annotation', filter=Q(annotation__vector__isnull=True))) first_annotation = annotations.first() user_teams = Team.objects.filter(members=request.user) imageset_edit_form = ImageSetEditForm(instance=imageset) imageset_edit_form.fields['main_annotation_type'].queryset = AnnotationType.objects.filter(active=True) return render(request, 'images/imageset.html', { 'images': images, - 'annotationcount': len(annotations), + 'annotationcount': annotations.count(), 'imageset': imageset, 'annotationtypes': annotation_types, 'annotation_types': annotation_types, 'all_annotation_types': all_annotation_types, - 'annotation_type_count': annotation_type_count, 'first_annotation': first_annotation, 'exports': exports, 'filtered': filtered, From e78c2bf031870977a540310e1ae7170259851597 Mon Sep 17 00:00:00 2001 From: Timon Engelke Date: Tue, 19 Feb 2019 22:24:13 +0100 Subject: [PATCH 2/3] Prefetch image set tags --- imagetagger/imagetagger/users/views.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/imagetagger/imagetagger/users/views.py b/imagetagger/imagetagger/users/views.py index 8ab730b6..16bc350c 100644 --- a/imagetagger/imagetagger/users/views.py +++ b/imagetagger/imagetagger/users/views.py @@ -238,7 +238,8 @@ def view_team(request, team_id): admins = team.admins imagesets = ImageSet.objects.filter(team=team).annotate( - image_count_agg=Count('images')).order_by('-public', 'name') + image_count_agg=Count('images')).prefetch_related('set_tags').\ + order_by('-public', 'name') export_formats = ExportFormat.objects.filter( team=team).prefetch_related('annotations_types').order_by('name') From a4009e7214e4cb09e445047d32e80dae9b99cc30 Mon Sep 17 00:00:00 2001 From: Timon Engelke Date: Tue, 19 Feb 2019 22:31:00 +0100 Subject: [PATCH 3/3] Show only the test image sets belonging to the team --- imagetagger/imagetagger/users/views.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/imagetagger/imagetagger/users/views.py b/imagetagger/imagetagger/users/views.py index 16bc350c..9348c7fe 100644 --- a/imagetagger/imagetagger/users/views.py +++ b/imagetagger/imagetagger/users/views.py @@ -248,7 +248,7 @@ def view_team(request, team_id): imagesets = imagesets.filter(public=True) export_format_forms = (ExportFormatEditForm(instance=format_instance) for format_instance in export_formats) - test_imagesets = ImageSet.objects.filter(set_tags__name='test').order_by('-public', 'name') + test_imagesets = imagesets.filter(set_tags__name='test').order_by('-public', 'name') return render(request, 'users/view_team.html', { 'team': team,