update annotation push with post request (not put anymore)
authorcavaliet
Mon, 05 Nov 2012 18:46:29 +0100
changeset 889 b1f9b838937a
parent 887 6ac668113c40
child 890 c15f3985e242
update annotation push with post request (not put anymore)
src/ldt/ldt/api/ldt/resources/annotation.py
src/ldt/ldt/api/ldt/resources/project.py
--- a/src/ldt/ldt/api/ldt/resources/annotation.py	Mon Nov 05 15:20:11 2012 +0100
+++ b/src/ldt/ldt/api/ldt/resources/annotation.py	Mon Nov 05 18:46:29 2012 +0100
@@ -10,17 +10,17 @@
 
 
 class AnnotationObject(object):
-    # For the moment, these attributes are useless. We just need to define AnnotationObject
-    id = ""
-    project = ""
-    type = ""
-    type_title = ""
-    media = ""
-    begin = ""
-    end = "" 
-    content = {"title":"", "description":""}
-    tags = []
-    meta = {"creator":"","created":""}
+    def __init__(self, id="", project = "", type = "", type_title = "", media = "", begin = 0, end = 0, content = {"title":"", "description":""}, tags = [], meta = {"creator":"","created":""}):
+        self.id = id
+        self.project = project
+        self.type = type
+        self.type_title = type_title
+        self.media = media
+        self.begin = begin
+        self.end = end
+        self.content = content
+        self.tags = tags
+        self.meta = meta
     
 class AnnotationResource(Resource):
     # For the moment, these attributes are useless. We just prepare relations to AnnotationObject
@@ -36,23 +36,24 @@
     meta = fields.DictField(attribute = 'meta')
     
     class Meta:
-        allowed_methods = ['put']
+        allowed_methods = ['post']
         resource_name = 'annotations'
         object_class = AnnotationObject
         authorization = Authorization()
         # always_return_data = True because we want the api returns the data with the updated ids
         always_return_data = True
+        include_resource_uri = False
     
     def obj_delete_list(self, request=None, **kwargs):
         return True
     
     def obj_create(self, bundle, request=None, **kwargs):
-        """
-        """
-        logging.debug("ICI 0-1 bundle.data = " + repr(bundle.data))
+        #logging.debug("ICI 0-1 bundle.data = " + repr(bundle.data))
+        # Here the a has the datas for only one annotation. Tastypie's post allows only one resource addition
+        a = bundle.data
         project_id = ""
-        if bundle.data.has_key('project') :
-            project_id = bundle.data["project"]
+        if a.has_key('project') :
+            project_id = a["project"]
         if project_id and project_id != "" :
             try:
                 project = Project.objects.get(ldt_id=project_id)
@@ -60,18 +61,17 @@
                 raise NotFound("Project not found. project_id = " + project_id)
         else :
             # If the project's is not defined, we get or create the content's front project.
-            iri_id = bundle.data["media"]
+            iri_id = a["media"]
             try:
                 content = Content.objects.get(iri_id=iri_id)
             except Content.DoesNotExist:
                 raise NotFound("Content not found. iri_id = " + iri_id)
             project = content.get_or_create_front_project()
-            bundle.data["project"] = project.ldt_id
+            a[u"project"] = project.ldt_id
         
         adder = LdtAnnotation(project)
         unprotect_models() # Allows anonymous user to modify models in this request only
-        # Here the bundle.data has the datas for only one annotation. The others api's functions parse the "objects" from the request's json.
-        a = bundle.data
+        
         dur = str(a['end'] - a['begin'])
         begin = str(a['begin'])
         # We test if the annotation has audio node
@@ -95,16 +95,19 @@
         add_annotation_to_stat(content, a['begin'], a['end'])
         
         # We update the ids
-        bundle.data['type'] = type_id
-        bundle.data['id'] = new_id
-        if not bundle.data['content'].has_key('audio') :
-            bundle.data['content']['audio'] = {'src':audio_src, 'href':audio_href}
-        # We reinject the datas in the bundle's response
-        bundle = self.full_hydrate(bundle)
+        a['type'] = type_id
+        a['id'] = new_id
+        if not a['content'].has_key('audio') :
+            a['content']['audio'] = {'src':audio_src, 'href':audio_href}
             
         # We save the added annotation and reprotect the contents and projects
         adder.save()
         protect_models()
+        # We update the AnnotationObject for the returned datas to be correct.
+        bundle.obj = AnnotationObject(id = a["id"], project = a["project"], type = a["type"], type_title = a["type_title"], media = a["media"], begin = a["begin"], end = a["end"], content = a['content'], tags = a['tags'], meta = a['meta'])
         return bundle
+    
+    def get_resource_uri(self, bundle_or_obj):
+        return ''
 
     
\ No newline at end of file
--- a/src/ldt/ldt/api/ldt/resources/project.py	Mon Nov 05 15:20:11 2012 +0100
+++ b/src/ldt/ldt/api/ldt/resources/project.py	Mon Nov 05 18:46:29 2012 +0100
@@ -2,19 +2,18 @@
 from ldt.ldt_utils.models import Project
 from ldt.api.ldt.serializers.cinelabserializer import CinelabSerializer
 from tastypie.authorization import Authorization
-from tastypie.exceptions import BadRequest, ImmediateHttpResponse
-from tastypie.http import HttpAccepted, HttpNoContent, HttpUnauthorized
 from tastypie.resources import Bundle, ModelResource
-from tastypie.utils import dict_strip_unicode_keys
 
 
 class ProjectResource(ModelResource):
     class Meta:
         allowed_methods = ['get', 'post']
-        #authorization= Authorization() # BE CAREFUL WITH THAT, it's unsecure
+        authorization= Authorization() # BE CAREFUL WITH THAT, it's unsecure
         resource_name = 'projects'
         queryset = Project.objects.all()
         serializer = CinelabSerializer()
+        # In the future version :
+        # detail_uri_name = 'ldt_id'
     
 #    # WARNING : this project API will only accepts and returns json format, no matter format get parameter.
 #    def determine_format(self, request):