Skip to content
This repository has been archived by the owner on Mar 15, 2018. It is now read-only.

Commit

Permalink
just data URI not JSON for collection image PUT (bug 909648)
Browse files Browse the repository at this point in the history
  • Loading branch information
Allen Short committed Sep 4, 2013
1 parent 9ed7468 commit 2276eeb
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 34 deletions.
5 changes: 2 additions & 3 deletions docs/api/topics/rocketfuel.rst
Expand Up @@ -278,9 +278,8 @@ Image

.. http:put:: /api/v1/rocketfuel/collections/(int:id)/image/
Set the image for a collection.
Set the image for a collection. Accepts a data URI as the request
body containing the image, rather than a JSON object.

.. note:: Authentication and the 'Apps:Publisher' permission are required.

:param image: A base64 encoded image.
:type image: string
4 changes: 0 additions & 4 deletions mkt/collections/serializers.py
Expand Up @@ -84,7 +84,3 @@ def from_native(self, data):

def to_native(self, value):
return value.name


class CollectionImageSerializer(serializers.Serializer):
image = DataURLImageField()
19 changes: 7 additions & 12 deletions mkt/collections/tests/test_serializers.py
Expand Up @@ -6,9 +6,9 @@
from mkt.api.resources import AppResource
from mkt.collections.constants import COLLECTIONS_TYPE_BASIC
from mkt.collections.models import Collection, CollectionMembership
from mkt.collections.serializers import (CollectionImageSerializer,
CollectionMembershipField,
CollectionSerializer,)
from mkt.collections.serializers import (CollectionMembershipField,
CollectionSerializer,
DataURLImageField)


class CollectionDataMixin(object):
Expand Down Expand Up @@ -129,13 +129,8 @@ def test_to_native_with_apps(self):
"""


class TestCollectionImageSerializer(CollectionDataMixin, amo.tests.TestCase):
class TestDataURLImageField(CollectionDataMixin, amo.tests.TestCase):

def setUp(self):
self.collection = Collection.objects.create(**self.collection_data)
self.serializer = CollectionImageSerializer()

def test_to_native(self):
d = self.serializer.from_native({'image': 'data:image/gif;base64,' +
IMAGE_DATA}, None)
eq_(d['image'].read(), IMAGE_DATA.decode('base64'))
def test_from_native(self):
d = DataURLImageField().from_native('data:image/gif;base64,' + IMAGE_DATA)
eq_(d.read(), IMAGE_DATA.decode('base64'))
9 changes: 4 additions & 5 deletions mkt/collections/tests/test_views.py
Expand Up @@ -858,8 +858,7 @@ def setUp(self):

def test_put(self):
self.grant_permission(self.profile, 'Apps:Publisher')
res = self.client.put(self.url, json.dumps({
'image': 'data:image/gif;base64,' + IMAGE_DATA}))
res = self.client.put(self.url, 'data:image/gif;base64,' + IMAGE_DATA)
eq_(res.status_code, 204)
assert os.path.exists(self.collection.image_path())
im = Image.open(self.collection.image_path())
Expand All @@ -868,16 +867,16 @@ def test_put(self):

def test_put_non_data_uri(self):
self.grant_permission(self.profile, 'Apps:Publisher')
res = self.client.put(self.url, json.dumps({'image': 'some junk'}))
res = self.client.put(self.url, 'some junk')
eq_(res.status_code, 400)

def test_put_non_image(self):
self.grant_permission(self.profile, 'Apps:Publisher')
res = self.client.put(self.url, json.dumps({'image': 'data:text/plain;base64,AAA='}))
res = self.client.put(self.url, 'data:text/plain;base64,AAA=')
eq_(res.status_code, 400)

def test_put_unauthorized(self):
res = self.client.put(self.url, json.dumps({'image': 'some junk'}))
res = self.client.put(self.url, 'some junk')
eq_(res.status_code, 403)

def test_get(self):
Expand Down
19 changes: 9 additions & 10 deletions mkt/collections/views.py
@@ -1,3 +1,4 @@
from django.core.exceptions import ValidationError
from django.core.files.base import File
from django.core.files.storage import default_storage as storage
from django.db import IntegrityError
Expand All @@ -18,7 +19,7 @@
RestSharedSecretAuthentication)

from mkt.api.base import CORSMixin, SlugOrIdMixin
from mkt.collections.serializers import CollectionImageSerializer
from mkt.collections.serializers import DataURLImageField
from mkt.webapps.models import Webapp

from .authorization import PublisherAuthorization
Expand Down Expand Up @@ -142,7 +143,6 @@ def reorder(self, request, pk=None):


class CollectionImageViewSet(CORSMixin, viewsets.ViewSet, generics.RetrieveUpdateAPIView):
serializer_class = CollectionImageSerializer
queryset = Collection.objects.all()
permission_classes = [PublisherAuthorization]
authentication_classes = [RestOAuthAuthentication,
Expand All @@ -156,12 +156,11 @@ def retrieve(self, request, pk=None):

def update(self, request, *a, **kw):
obj = self.get_object()
serializer = self.get_serializer(data=request.DATA, files=request.FILES)
if serializer.is_valid():
i = Image.open(serializer.data['image'])
with storage.open(obj.image_path(), 'wb') as f:
i.save(f, 'png')
return Response(status=204)
else:
try:
img = DataURLImageField().from_native(request.read())
except ValidationError:
return Response(status=400)

i = Image.open(img)
with storage.open(obj.image_path(), 'wb') as f:
i.save(f, 'png')
return Response(status=204)

1 comment on commit 2276eeb

@mattbasta
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perfect timing! i was just talking to @spasovski about this.

Please sign in to comment.