4👍
Ended up making a new ModelResource and then setting the Content-Type to: multipart/form-data; boundary=frontier
Took me a while to figure this out, even though the OP was yesterday, 10 hours is a lot of googling and trial and error… smdh.
Here is the Model Resource:
from tastypie import http, fields
from django.conf.urls import url
from tastypie.resources import ModelResource
from api.helper_methods import HelperMethods
from django.conf import settings
from boto.s3.connection import S3Connection
from boto.s3.key import Key
import cStringIO
from PIL import Image, ImageOps
class FileUploadResource(ModelResource):
img = fields.FileField(attribute="img", null=True, blank=True)
class Meta:
allowed_methods = 'post'
resource_name = 'file_upload'
include_resource_uri = False
def prepend_urls(self):
return [
url(r"^(?P<resource_name>%s)/$" % self._meta.resource_name, self.wrap_view('get_profile_picture'), name="api_get_profile_picture"),
url(r"^(?P<resource_name>%s)/profile_picture/$" % self._meta.resource_name, self.wrap_view('post_profile_picture'), name="api_post_profile_picture"),
]
def get_profile_picture(self, request, **kwargs):
return self.create_response(request, HelperMethods.api_return_msg("Bad requested"), response_class=http.HttpBadRequest)
def post_profile_picture(self, request, **kwargs):
if(request.method == 'POST'):
if "multipart/form-data" not in str(request.META['CONTENT_TYPE']):
return self.create_response(request, HelperMethods.api_return_msg("Unsupported media type"), response_class=http.HttpBadRequest)
else:
if('image' in request.FILES):
upload = request.FILES['image']
main_img = Image.open(upload)
profile_img = main_img.resize((200,200), Image.ANTIALIAS)
profile_img_io = cStringIO.StringIO()
profile_img.save(profile_img_io, 'PNG', quality=85)
thumb_img = main_img.resize((80,80), Image.ANTIALIAS)
thumb_img_io = cStringIO.StringIO()
thumb_img.save(thumb_img_io, 'PNG', quality=85)
conn = S3Connection(settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY)
bucket = conn.get_bucket(settings.BUCKET_NAME)
profile_img_key_name = HelperMethods.generate_pic_key() + ".png"
profile_img_key = Key(bucket)
profile_img_key.key = profile_img_key_name
profile_img_key.set_contents_from_string(profile_img_io.getvalue())
profile_img_key.make_public()
thumb_img_key_name = HelperMethods.generate_pic_key() + ".png"
thumb_img_key = Key(bucket)
thumb_img_key.key = thumb_img_key_name
thumb_img_key.set_contents_from_string(thumb_img_io.getvalue())
thumb_img_key.make_public()
return self.create_response(request, {"profile_img" : profile_img_key_name, "thumb_img" : thumb_img_key_name}, response_class=http.HttpResponse)
else:
return self.create_response(request, HelperMethods.api_return_msg("No image found"), response_class=http.HttpBadRequest)
else:
return self.create_response(request, HelperMethods.api_return_msg("Method not allowed"), response_class=http.HttpMethodNotAllowed)
It also uses amazon s3 to store the images after PIL is used to change the image.
Source:stackexchange.com