correct POST on projects without image field
authorymh <ymh.work@gmail.com>
Fri, 30 Jan 2015 15:13:23 +0100
changeset 1336 9ee589aaa464
parent 1335 5c52df59b348
child 1337 a2e245fa521e
correct POST on projects without image field
src/ldt/ldt/api/ldt/resources/project.py
--- a/src/ldt/ldt/api/ldt/resources/project.py	Thu Nov 13 15:49:19 2014 +0100
+++ b/src/ldt/ldt/api/ldt/resources/project.py	Fri Jan 30 15:13:23 2015 +0100
@@ -1,10 +1,18 @@
+import logging
+
 from django import VERSION as django_version
 from django.conf import settings
 from django.conf.urls import url
 from django.contrib.auth.models import Group
 from django.core.exceptions import ObjectDoesNotExist
 from guardian.shortcuts import assign
-from ldt.api.ldt.authentication import (SessionAuthentication, 
+from tastypie import fields, http
+from tastypie.authorization import Authorization
+from tastypie.exceptions import BadRequest
+from tastypie.resources import Bundle, ModelResource, ALL
+from tastypie.utils import dict_strip_unicode_keys
+
+from ldt.api.ldt.authentication import (SessionAuthentication,
     MultiAuthentication, ApiKeyAuthentication)
 from ldt.api.ldt.resources import ContentResource
 from ldt.api.ldt.resources.user import UserResource
@@ -12,12 +20,9 @@
 from ldt.ldt_utils.models import Project
 from ldt.security import protect_models, unprotect_models
 from ldt.security.permissionchecker import check_object_perm_for_user
-from tastypie import fields, http
-from tastypie.authorization import Authorization
-from tastypie.exceptions import BadRequest, NotFound
-from tastypie.resources import Bundle, ModelResource, ALL
-from tastypie.utils import dict_strip_unicode_keys
+
 
+logger = logging.getLogger(__name__)
 
 class ProjectResource(ModelResource):
     contents = fields.ManyToManyField(ContentResource, 'contents')
@@ -82,8 +87,9 @@
     
     
     def hydrate_image(self, bundle):
-        if bundle.data['image'] and bundle.data['image'].startswith(settings.MEDIA_URL):
-            bundle.data['image'] = bundle.data['image'][len(settings.MEDIA_URL):]
+        image_data = bundle.data.get('image', None)
+        if image_data and image_data.startswith(settings.MEDIA_URL):
+            bundle.data['image'] = image_data[len(settings.MEDIA_URL):]
         return bundle
     
     # Updates an existing project. Used with post_detail and with a ldt_id in the url
@@ -114,7 +120,7 @@
                 try:
                     bundle.obj = self.obj_get(bundle=bundle, **self.remove_api_resource_names(kwargs))
                 except ObjectDoesNotExist:
-                    raise NotFound("A model instance matching the provided arguments could not be found.")
+                    return http.HttpNotFound("A model instance matching the provided arguments could not be found.")
                 bundle = self.full_hydrate(bundle)
                 updated_bundle = self.save(bundle)
                 # Save is done, we can reactivate guardian rights
@@ -129,7 +135,7 @@
                 updated_bundle = self.alter_detail_data_to_serialize(request, updated_bundle)
                 return self.create_response(request, updated_bundle, response_class=http.HttpAccepted)
         except Exception as e:
-            return http.HttpNotFound("Object does not exist or Datas are not correct.\nError = " + str(e) + "\n")
-        return http.HttpResponse("fuuuuu")
+            return http.HttpBadRequest("Datas are not correct.\nError = " + str(e) + "\n")
+        return http.HttpResponse("Done")
     
     
\ No newline at end of file