Upload Image

567 views
Skip to first unread message

vernomcrp

unread,
May 15, 2010, 5:47:53 AM5/15/10
to django-piston
I try to create restful webservice that can upload image by django-
piston, But I don't know direction to do it.

--
You received this message because you are subscribed to the Google Groups "django-piston" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-pisto...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-piston?hl=en.

Mario Gonzalez

unread,
Jun 22, 2010, 2:06:11 AM6/22/10
to django-piston
On May 15, 5:47 am, vernomcrp <vernom...@gmail.com> wrote:
> I try to create restful webservice that can upload image by django-
> piston, But I don't know direction to do it.
>
Hello,

I haven't answered because I haven't done this so far but today I
needed to achieve what you say. In my example I upload a single image
to a protected resource with Basic auth method for my user 'jsmith'
with curl and save it in SOME_PATH:

$ curl -u USER:PASSWORD http://server.com/customer/jsmith/image/ -X
POST -F "image=@Desktop/me.jpg (Sure, replace with your path ;-)

And some code:

from piston.handler import BaseHandler
from piston.utils import rc
from PIL import Image

class ImageRESTHandler(BaseHandler):
allowed_methods = ('POST',)

def create(self, request, nickname):
name = request.FILES["image"].name
image = Image.open(request.FILES["image"])
image.save(SOME_PATH+name)
return rc.ALL_OK

As a homework you need to catch the exceptions and save where you
think you should.
Regards.

Alexander Pugachev

unread,
Jun 22, 2010, 2:46:23 AM6/22/10
to django...@googlegroups.com
Define model with ImageField, form with ImageField. Inside method create() instantiate form class passing request.POST and request.FILES to constructor, instantiate model class, assign image item from form.cleaned_data to model's corresponding attribute and save model.

2010/6/22 Mario Gonzalez <gonzal...@gmail.com>

vernomcrp

unread,
Jul 24, 2010, 4:06:06 AM7/24/10
to django-piston
Quite a COOL solution, will try it , immediately :) Thank Mario

Rock

unread,
Jul 26, 2010, 4:01:12 PM7/26/10
to django-piston
Correct. The only piston specific difference that I have encountered
is that you must avoid piston's validate decorator since it doesn't
know how to do create the form using both request.POST and
request.FILES. I guess adding a validate_upload decorator might be
useful.

On Jun 22, 1:46 am, Alexander Pugachev <alexander.pugac...@gmail.com>
wrote:
> Define model with ImageField, form with ImageField. Inside method create()
> instantiate form class passing request.POST and request.FILES to
> constructor, instantiate model class, assign image item from
> form.cleaned_data to model's corresponding attribute and save model.
>
> 2010/6/22 Mario Gonzalez <gonzalema...@gmail.com>
> > django-pisto...@googlegroups.com<django-piston%2Bunsubscribe@goog legroups.com>
> > .
> > > For more options, visit this group athttp://
> > groups.google.com/group/django-piston?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "django-piston" group.
> > To post to this group, send email to django...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > django-pisto...@googlegroups.com<django-piston%2Bunsubscribe@goog legroups.com>
> > .

Rock

unread,
Jul 27, 2010, 5:09:40 PM7/27/10
to django-piston

Here is that validate_upload decorator:

from piston.decorator import decorator
from piston.utils import FormValidationError

def validate_upload(v_form):
# This is a piston-style decorator specifically
# for the file upload case.
#
@decorator
def wrap(f, self, request, *a, **kwa):
form = v_form(request.DOC, request.FILES)

if form.is_valid():
setattr(request, 'form', form)
return f(self, request, *a, **kwa)
else:
raise FormValidationError(form)
return wrap

You use this one in the same manner as the validate() decorator but
the only required argument is the Form since the operation is hard-
coded to POST in this case.

Alexander created a clever extended version of validate() that can
handle both normal form processing and file uploading, but I prefer
validate_upload as a separate decorator to keep things explicit.

(Thanks for prodding me Alex!)

If I hear no objections, then I will go ahead and make this available
to Jesper for possible inclusion in the next release. Since it is a
brand new decorator, it cannot break any existing code.

Rock

Rock

unread,
Jul 27, 2010, 5:15:48 PM7/27/10
to django-piston
Oops. A typo found its way in there. That request.DOC should be
request.POST instead.
Reply all
Reply to author
Forward
0 new messages