From d0086437741fcb5684de03de249e1602381d176b Mon Sep 17 00:00:00 2001 From: Andy Summers <andrew.summers@wisc.edu> Date: Mon, 19 Nov 2018 14:00:51 -0600 Subject: [PATCH] Fix image handling for thumbnails, use `path()` --- .gitignore | 1 + spotseeker_server/models.py | 4 +- spotseeker_server/test/images/__init__.py | 8 ++- spotseeker_server/urls.py | 71 ++++++++++------------- spotseeker_server/views/thumbnail.py | 4 +- 5 files changed, 42 insertions(+), 46 deletions(-) diff --git a/.gitignore b/.gitignore index 0e4057b..0819b1c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .vscode/ *.py[cod] *.egg-info/ +space_images/ \ No newline at end of file diff --git a/spotseeker_server/models.py b/spotseeker_server/models.py index 1906d50..ed39fb0 100644 --- a/spotseeker_server/models.py +++ b/spotseeker_server/models.py @@ -359,7 +359,7 @@ class SpotImage(models.Model): self.image.file.multiple_chunks()): img = Image.open(self.image.file.temporary_file_path()) else: - img = Image.open(self.image.name) + img = Image.open(self.image.file) except: raise ValidationError('Not a valid image format') @@ -560,7 +560,7 @@ class ItemImage(models.Model): self.image.file.multiple_chunks()): img = Image.open(self.image.file.temporary_file_path()) else: - img = Image.open(self.image) + img = Image.open(self.image.file) except: raise ValidationError('Not a valid image format') diff --git a/spotseeker_server/test/images/__init__.py b/spotseeker_server/test/images/__init__.py index 3c70494..25c2690 100644 --- a/spotseeker_server/test/images/__init__.py +++ b/spotseeker_server/test/images/__init__.py @@ -1,3 +1,5 @@ +from io import BytesIO + from spotseeker_server.test import SpotServerTestCase @@ -6,9 +8,11 @@ class ImageTestCase(SpotServerTestCase): def upload_image(self, image_file_name, url, extra_args=None): c = self.client with open(image_file_name, 'rb') as f: - res_args = {'image': f} + img = BytesIO(f.read()) + img.name = image_file_name + res_args = {'image': img} if extra_args: res_args.update(extra_args) - response = c.post(url, res_args) + response = c.post(url, data=res_args) return response diff --git a/spotseeker_server/urls.py b/spotseeker_server/urls.py index 6b3e8a8..659ff91 100644 --- a/spotseeker_server/urls.py +++ b/spotseeker_server/urls.py @@ -22,6 +22,7 @@ from django.conf.urls import include, url from django.views.decorators.csrf import csrf_exempt +from django.urls import path from spotseeker_server.views.buildings import BuildingListView from spotseeker_server.views.spot import SpotView from spotseeker_server.views.search import SearchView @@ -41,65 +42,55 @@ from spotseeker_server.views.add_item_image import AddItemImageView from spotseeker_server.views.item_thumbnail import ItemThumbnailView urlpatterns = [ - url(r'v1/null$', csrf_exempt(NullView().run)), + path('v1/null', csrf_exempt(NullView().run)), url(r'v1/spot/(?P<spot_id>(\d+|external:[\w-]+))$', csrf_exempt(SpotView().run), name='spot'), url(r'v1/spot/?$', csrf_exempt(SearchView().run), name='spot-search'), - url(r'v1/spot/all$', + path('v1/spot/all', csrf_exempt(AllSpotsView().run), name='spots'), url(r'v1/buildings/?$', csrf_exempt(BuildingListView().run), name='buildings'), - url(r'v1/schema$', - csrf_exempt(SchemaGenView().run), - name='schema'), - url(r'v1/spot/(?P<spot_id>\d+)/image$', - csrf_exempt(AddImageView().run)), - url(r'v1/spot/(?P<spot_id>\d+)/image/' - '(?P<image_id>\d+)$', - csrf_exempt(ImageView().run), - name='spot-image'), - url(r'v1/spot/(?P<spot_id>\d+)/image/' - '(?P<image_id>\d+)/thumb/constrain/' - '(?P<thumb_dimensions>.+)?$', - csrf_exempt(ThumbnailView().run), - {'constrain': True}), - url(r'v1/spot/(?P<spot_id>\d+)/image/' - '(?P<image_id>\d+)/thumb/' - '(?P<thumb_dimensions>.+)?$', - csrf_exempt(ThumbnailView().run), - name='spot-image-thumb'), - url(r'v1/item/(?P<item_id>\d+)/image$', - csrf_exempt(AddItemImageView().run)), - url(r'v1/item/(?P<item_id>\d+)/image/' - '(?P<image_id>\d+)$', - csrf_exempt(ItemImageView().run), - name='item-image'), - url(r'v1/item/(?P<item_id>\d+)/image/' - '(?P<image_id>\d+)/thumb/constrain/' - '(?P<thumb_dimensions>.+)?$', + path('v1/schema', + csrf_exempt(SchemaGenView().run), + name='schema'), + path('v1/spot/<int:spot_id>/image', + csrf_exempt(AddImageView().run)), + path('v1/spot/<int:spot_id>/image/<int:image_id>', + csrf_exempt(ImageView().run), + name='spot-image'), + path('v1/spot/<int:spot_id>/image/<int:image_id>/thumb/constrain/<str:thumb_dimensions>', + csrf_exempt(ThumbnailView().run), + {'constrain': True}), + path('v1/spot/<int:spot_id>/image/<int:image_id>/thumb/<str:thumb_dimensions>', + csrf_exempt(ThumbnailView().run), + name='spot-image-thumb'), + path('v1/item/<int:item_id>/image', + csrf_exempt(AddItemImageView().run)), + path('v1/item/<int:item_id>/image/<int:image_id>', + csrf_exempt(ItemImageView().run), + name='item-image'), + path('v1/item/<int:item_id>/image/<int:image_id>/thumb/constrain/<str:thumb_dimensions>', csrf_exempt(ItemThumbnailView().run), {'constrain': True}), - url(r'v1/item/(?P<item_id>\d+)/image/' - '(?P<image_id>\d+)/thumb/' - '(?P<thumb_dimensions>.+)?$', + path('v1/item/<int:item_id>/image/<int:image_id>/thumb/<str:thumb_dimensions>', csrf_exempt(ItemThumbnailView().run), name='item-image-thumb'), - url(r'v1/spot/(?P<spot_id>\d+)/reviews$', - csrf_exempt(ReviewsView().run)), + path('v1/spot/<int:spot_id>/reviews', + csrf_exempt(ReviewsView().run)), url(r'v1/user/me/favorites/?$', csrf_exempt(FavoritesView().run)), - url(r'v1/user/me$', + path('v1/user/me', csrf_exempt(PersonView().run)), - url(r'v1/user/me/favorite/(?P<spot_id>\d+)$', + path('v1/user/me/favorite/<int:spot_id>', csrf_exempt(FavoritesView().run)), - url(r'v1/spot/(?P<spot_id>\d+)/share$', + path('v1/spot/<int:spot_id>/share', csrf_exempt(ShareSpaceView().run)), - url(r'v1/spot/(?P<spot_id>\d+)/shared$', + path('v1/spot/<int:spot_id>/shared', csrf_exempt(SharedSpaceReferenceView().run)), - url(r'v1/reviews/unpublished$', + path('v1/reviews/unpublished', csrf_exempt(UnpublishedReviewsView().run)), ] diff --git a/spotseeker_server/views/thumbnail.py b/spotseeker_server/views/thumbnail.py index 15e552c..affbf28 100644 --- a/spotseeker_server/views/thumbnail.py +++ b/spotseeker_server/views/thumbnail.py @@ -25,7 +25,7 @@ from spotseeker_server.models import SpotImage, Spot from django.http import HttpResponse from django.utils.http import http_date from spotseeker_server.require_auth import app_auth_required -from io import StringIO +from io import BytesIO from PIL import Image import time import re @@ -90,7 +90,7 @@ class ThumbnailView(RESTDispatch): else: thumb = im.resize((thumb_width, thumb_height), Image.ANTIALIAS) - tmp = StringIO() + tmp = BytesIO() thumb.save(tmp, im.format, quality=95) tmp.seek(0) -- GitLab