Created
November 17, 2011 04:51
-
-
Save solenoid/1372388 to your computer and use it in GitHub Desktop.
simple rest implementation in django
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if 'GET' == request.method: | |
resources = [] | |
for resource in mongo_collection.find(): | |
resource['id'] = str(resource['_id']) | |
del resource['_id'] | |
resources.append(resource) | |
content = json.dumps(resources) | |
return HttpResponse(content=content, mimetype='application/javascript') | |
if 'POST' == request.method: | |
raw_data = StringIO.StringIO(request.raw_post_data) | |
resource = json.load(raw_data) | |
resource['_id'] = ObjectId(resource['id']) | |
del resource['id'] | |
mongo_collection.save(resource, safe=True) | |
return HttpResponse(status=200) | |
if 'PUT' == request.method: | |
raw_data = StringIO.StringIO(request.raw_post_data) | |
resource = json.load(raw_data) | |
resource['_id'] = ObjectId(resource['id']) | |
del resource['id'] | |
mongo_collection.update({'_id': resource['_id']}, resource, safe=True) | |
return HttpResponse(status=200) | |
if 'DELETE' == request.method: | |
resource = {'_id': ObjectId(resource_id)} | |
mongo_collection.remove(resource, safe=True) | |
return HttpResponse(status=200) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment