From 4cee7347b4619b36ee95af9ffedea0b94f8655e0 Mon Sep 17 00:00:00 2001 From: "John N. Milner" Date: Tue, 21 May 2019 14:08:42 -0400 Subject: [PATCH] Load thumbnails in small, delayed chunks and limit the gallery to 2,500 images. Fixes #555 --- onadata/apps/main/templates/form_photos.html | 34 +++++++++++++++----- onadata/apps/main/views.py | 20 ++++++++++-- 2 files changed, 43 insertions(+), 11 deletions(-) diff --git a/onadata/apps/main/templates/form_photos.html b/onadata/apps/main/templates/form_photos.html index f8c8f0bdf..b18405852 100644 --- a/onadata/apps/main/templates/form_photos.html +++ b/onadata/apps/main/templates/form_photos.html @@ -8,16 +8,11 @@ {% block body %} {% load i18n %} +{% load l10n %} {% include "topbar.html" %} {% if images|length %} - + {% else %}
{% trans "There are no photos in this project." %} @@ -30,16 +25,39 @@ {% endif %} {% endblock %} diff --git a/onadata/apps/main/views.py b/onadata/apps/main/views.py index a6409a810..7fd78cac0 100644 --- a/onadata/apps/main/views.py +++ b/onadata/apps/main/views.py @@ -1053,6 +1053,10 @@ def download_media_data(request, username, id_string, data_id): def form_photos(request, username, id_string): + GALLERY_IMAGE_COUNT_LIMIT = 2500 + GALLERY_THUMBNAIL_CHUNK_SIZE = 25 + GALLERY_THUMBNAIL_CHUNK_DELAY = 5000 # ms + xform, owner = check_and_set_user_and_form(username, id_string, request) if not xform: @@ -1063,9 +1067,16 @@ def form_photos(request, username, id_string): data['content_user'] = owner data['xform'] = xform image_urls = [] - - for instance in xform.instances.all(): - for attachment in instance.attachments.all(): + too_many_images = False + + # Show the most recent images first + for instance in xform.instances.all().order_by('-pk'): + attachments = instance.attachments.all() + # If we have to truncate, don't include a partial instance + if len(image_urls) + attachments.count() > GALLERY_IMAGE_COUNT_LIMIT: + too_many_images = True + break + for attachment in attachments: # skip if not image e.g video or file if not attachment.mimetype.startswith('image'): continue @@ -1080,6 +1091,9 @@ def form_photos(request, username, id_string): image_urls.append(data) data['images'] = image_urls + data['too_many_images'] = too_many_images + data['thumbnail_chunk_size'] = GALLERY_THUMBNAIL_CHUNK_SIZE + data['thumbnail_chunk_delay'] = GALLERY_THUMBNAIL_CHUNK_DELAY data['profilei'], created = UserProfile.objects.get_or_create(user=owner) return render(request, 'form_photos.html', data)