# HG changeset patch # User cavaliet # Date 1369845639 -7200 # Node ID 4f0e84a8fff86745a62a572eb9ae625240ad1c9d # Parent 9e14b6f964fe6840e1303eb0922f8dceb11d1b1b image url management. diff -r 9e14b6f964fe -r 4f0e84a8fff8 src/metadatacomposer/urls.py --- a/src/metadatacomposer/urls.py Wed May 29 18:14:23 2013 +0200 +++ b/src/metadatacomposer/urls.py Wed May 29 18:40:39 2013 +0200 @@ -4,7 +4,7 @@ MetadataComposerModalContent, MetadataComposerModalImage,\ MetadataComposerImagePagination, MetadataComposerModalImageLibrary,\ MetadataComposerRemoveImage, MetadataComposerRemoveContent,\ - MetadataComposerRemoveProject + MetadataComposerRemoveProject, MetadataComposerImage urlpatterns = patterns('', url(r'^jsi18n/(?P\S+?)/$', 'django.views.i18n.javascript_catalog', name='jsi18n'), @@ -12,13 +12,14 @@ url(r'^(?P.*)/resourcelist/$', MetadataComposerResourceList.as_view(), name="composer_resource_list"), url(r'^(?P.*)/imagepage/$', MetadataComposerImagePagination.as_view(), name="composer_image_page"), url(r'^(?P.*)/contentpage/$', MetadataComposerContentPagination.as_view(), name="composer_content_page"), - url(r'^(?P.*)/modalcontent/(?P[\w-]+)$', MetadataComposerModalContent.as_view(), name="composer_modal_content"), + url(r'^(?P.*)/modalcontent/(?P[\w-]+)/$', MetadataComposerModalContent.as_view(), name="composer_modal_content"), url(r'^(?P.*)/modalcontent/$', MetadataComposerModalContent.as_view(), name="composer_modal_content"), url(r'^(?P.*)/modalimage/$', MetadataComposerModalImage.as_view(), name="composer_modal_image"), url(r'^(?P.*)/modalimagelibrary/$', MetadataComposerModalImageLibrary.as_view(), name="composer_modal_image_library"), url(r'^(?P.*)/removecontent/$', MetadataComposerRemoveContent.as_view(), name="composer_remove_content"), url(r'^(?P.*)/removeimage/$', MetadataComposerRemoveImage.as_view(), name="composer_remove_image"), url(r'^(?P.*)/removeproject/$', MetadataComposerRemoveProject.as_view(), name="composer_remove_project"), + url(r'^(?P.*)/image/(?P[\w-]+)/$', MetadataComposerImage.as_view(), name="composer_image"), url(r'^(?P.*)/$', MetadataComposerHome.as_view(), name="composer_home"), url(r'^$', MetadataComposerHome.as_view(), name="composer_home"), ) diff -r 9e14b6f964fe -r 4f0e84a8fff8 src/metadatacomposer/views.py --- a/src/metadatacomposer/views.py Wed May 29 18:14:23 2013 +0200 +++ b/src/metadatacomposer/views.py Wed May 29 18:40:39 2013 +0200 @@ -1,6 +1,7 @@ from django.conf import settings from django.contrib.auth.decorators import login_required from django.core.paginator import Paginator, InvalidPage, EmptyPage +from django.http import HttpResponse, HttpResponseBadRequest from django.shortcuts import redirect, get_object_or_404 from django.utils.decorators import method_decorator from django.views.decorators.cache import never_cache @@ -11,6 +12,8 @@ from ldt.security.permissionchecker import check_object_perm_for_user from metadatacomposer.forms import ImageUploadModelForm from metadatacomposer.models import Image +from sorl.thumbnail import get_thumbnail +import os import logging #@UnresolvedImport @@ -346,3 +349,67 @@ +class MetadataComposerImage(View): + + @method_decorator(login_required) + def get(self, request, branding="iri", image_pk=None, **kwargs): + self.branding = branding + image_data = None + if image_pk: + image = get_object_or_404(Image, pk=image_pk) + width = request.GET.get("width") or None + height = request.GET.get("height") or None + maxwidth = request.GET.get("maxwidth") or None + maxheight = request.GET.get("maxheight") or None + image_file = None + image_data = None + # Check w and h + if width: + try: + width = str(int(width)) + except: + return HttpResponseBadRequest("width parameter must be integer.") + if height: + try: + height = str(int(height)) + except: + return HttpResponseBadRequest("height parameter must be integer.") + if maxwidth: + try: + maxwidth = int(maxwidth) + except: + return HttpResponseBadRequest("maxwidth parameter must be integer.") + if maxheight: + try: + maxheight = int(maxheight) + except: + return HttpResponseBadRequest("maxheight parameter must be integer.") + if (maxwidth and not maxheight) or (not maxwidth and maxheight): + return HttpResponseBadRequest("maxwidth AND maxheight parameters must be set.") + if not width and not height and not maxwidth and not maxheight: + # No w and no h parameter : raw file 200px width + image_file = get_thumbnail(os.path.join(settings.MEDIA_ROOT, image.image_file.path), '200', crop='center', format='PNG', quality=99) + #image_data = open(os.path.join(settings.MEDIA_ROOT, image.image_file.path), "rb").read() + elif width and not height: + # W and no h parameter : thumbnail with fixed width + image_file = get_thumbnail(os.path.join(settings.MEDIA_ROOT, image.image_file.path), width, crop='center', format='PNG', quality=99) + elif not width and height: + # H and no w parameter : thumbnail with fixed height + image_file = get_thumbnail(os.path.join(settings.MEDIA_ROOT, image.image_file.path), 'x'+height, crop='center', format='PNG', quality=99) + elif width and height: + # H and no w parameter : thumbnail with fixed height + image_file = get_thumbnail(os.path.join(settings.MEDIA_ROOT, image.image_file.path), width+'x'+height, crop='center', format='PNG', quality=99) + elif maxwidth and maxheight: + # We compare wanted format with image's format and we resize W or H + if (image.image_file.width/image.image_file.width) < (maxwidth/maxheight): + # Image ratio < wanted ratio, we resize with fixed height + image_file = get_thumbnail(os.path.join(settings.MEDIA_ROOT, image.image_file.path), 'x'+str(maxheight), crop='center', format='PNG', quality=99) + else: + # Image ratio > wanted ratio, we resize with fixed width + image_file = get_thumbnail(os.path.join(settings.MEDIA_ROOT, image.image_file.path), str(maxwidth), crop='center', format='PNG', quality=99) + if image_file: + image_data = image_file.read() + return HttpResponse(image_data, mimetype="image/png") + + +