PUT/POST binary files

3 views
Skip to first unread message

suckerpunch

unread,
Sep 16, 2008, 8:59:43 PM9/16/08
to Django REST interface
Hi guys - good work :)

I'm building an Adobe Lightroom plugin for my website to directly
export images into the DB via the REST API.

It looks like Lightroom doesn't do PUT, but it does do POST. So I want
to send over the file, extract the caption and title from the EXIF for
the Django model, save those to the DB and save the file onto the
filesystem.

Now, should I treat this as a resource not mapped to a model, pre-
process and then create a new model, or is there a more efficient
method?

This is in my urls.py

picture_resource = Collection(
queryset = Picture.objects.all(),
permitted_methods = ('GET', 'POST', 'PUT',),
expose_fields = ('id', 'title', 'story'),
receiver = PictureReceiver(),
responder = XMLResponder(),
authentication = HttpBasicAuthentication()
)

This is the first go at a receiver

class PictureReceiver(Receiver):
def __init__(self):
print "PictureReceiver"
def get_data(self, request, method):
print request['story']
print request['fullsize_filename']
def get_post_data(self, request):
return self.get_data(request, 'POST')
def get_put_data(self, request):
return self.get_data(request, 'PUT')

And this is my model

class Picture(models.Model):
title = models.CharField(blank=True, max_length=765)
caption = models.TextField(blank=True)
is_live = models.BooleanField(blank=True)
fullsize_filename = models.ImageField(upload_to='%Y/%m/%d')
created_at = models.DateTimeField(null=True, blank=True)
updated_at = models.DateTimeField(null=True, blank=True)
def __str__(self):
return self.title

Any suggestions? Many thanks :)

suckerpunch

unread,
Sep 22, 2008, 4:55:42 PM9/22/08
to Django REST interface
Hiya, I'm getting a bit further but repeatedly coming up against 400
bad request messages when I know the packet is well formed. It looks
like the 400 error handling is a little eager in firing.

The traces are working well, but I can't get past these 400 errors.
I'm using cURL to form the requests so that's pretty reliable.

Andreas suggested I email the list.

Cheers :)

Alex Ezell

unread,
Sep 22, 2008, 9:27:46 PM9/22/08
to django-res...@googlegroups.com
On Mon, Sep 22, 2008 at 3:55 PM, suckerpunch <vish.vi...@gmail.com> wrote:

Hiya, I'm getting a bit further but repeatedly coming up against 400
bad request messages when I know the packet is well formed. It looks
like the 400 error handling is a little eager in firing.

Howdy,
Are you using any authentication on the requests? Is cURL POSTing or GETting to the URL? Do you have the correct methods in your permitted_methods for that URL? 

Maybe a little code snippet might help us pinpoint the issue.

/alex

suckerpunch

unread,
Sep 23, 2008, 6:23:24 AM9/23/08
to Django REST interface
Hi Alex,

Yes, there is authentication on the request, but that's working fine.
I tried taking out the auth and there was little difference.

Here's a chunk from urls.py

picture_resource = Collection(
queryset = Picture.objects.all(),
permitted_methods = ('GET', 'POST', 'PUT', 'DELETE'),
expose_fields = ('id', 'title', 'caption', 'single', 'story',
'position', 'is_live', 'keep', 'remove', 'fullsize_filename',
'created_at', 'updated_at'),
receiver = PictureReceiver(),
responder = XMLResponder(paginate_by = 2),
authentication = HttpBasicAuthentication()
)

Here's the receiver:

class PictureReceiver(Receiver):
def __init__(self):
print "PictureReceiver"
def get_data(self, request, method):
print request.REQUEST['title']
print request.REQUEST['caption']
print request.REQUEST['position']
print request.REQUEST['single']
print request.REQUEST['story']
print request.REQUEST['is_live']

def get_post_data(self, request):
print "POST Request"
return self.get_data(request, 'POST')

Just printing to the console for debug.

And here's the cURL POST command:

curl -F "title=title1" -F "caption=caption1" -F position=42 -F
"fullsize_filename=@test-post.jpg;image/jpeg" -F "single=on" -F
"story=1" -F "is_live=on" -F "created_at_0=2008-09-20" -F
"created_at_1=00:46:19" -F "updated_at_0=2008-09-20" -F
"updated_at_1=00:46:21" -F "_save=Save" http://localhost:8000/xml/picture/
> error.html
Reply all
Reply to author
Forward
0 new messages