Content PUT api endpoint to add an annotation to a content without giving a project id. #35
authorcavaliet
Thu, 09 Aug 2012 18:06:36 +0200
changeset 741 45d814511958
parent 740 d03908cf3c73
child 743 45ddd94e1302
child 744 960e4d8d8acd
Content PUT api endpoint to add an annotation to a content without giving a project id. #35
src/ldt/ldt/api/ldt/handlers.py
src/ldt/ldt/ldt_utils/models.py
src/ldt/ldt/ldt_utils/views/front.py
--- a/src/ldt/ldt/api/ldt/handlers.py	Wed Aug 08 17:20:37 2012 +0200
+++ b/src/ldt/ldt/api/ldt/handlers.py	Thu Aug 09 18:06:36 2012 +0200
@@ -231,7 +231,7 @@
 
         
 class ContentHandler(BaseHandler):
-    allowed_methods = ('GET', )
+    allowed_methods = ('GET', 'PUT')
     model = Content
     exclude = (
                ("media_obj"),
@@ -241,7 +241,23 @@
         """
         returns a single content
         """
-        return Content.objects.get(iri_id=iri_id)
+        return Content.objects.get(iri_id=iri_id)   
+
+    @require_extended
+    def update(self, request, iri_id):
+        """
+        Receives a json exactly like AnnotationHandler, but without any project indicated.
+        We get or set the current content front project, and add the annotation
+        """
+        try:
+            content = Content.objects.get(iri_id=iri_id)
+        except Content.DoesNotExist:
+            return rc.NOT_HERE
+        proj = content.get_or_create_front_project()
+        ah = AnnotationHandler()
+        updated_data = ah.update(request, proj.ldt_id)
+        
+        return updated_data
     
 
 class SegmentHandler(BaseHandler):
--- a/src/ldt/ldt/ldt_utils/models.py	Wed Aug 08 17:20:37 2012 +0200
+++ b/src/ldt/ldt/ldt_utils/models.py	Thu Aug 09 18:06:36 2012 +0200
@@ -366,6 +366,21 @@
         self.front_project.publish(allow_write=True)            
         self.save()
         set_current_user(old_user)
+    
+    
+    def get_or_create_front_project(self):
+        front_proj = self.front_project
+        if front_proj:
+            proj = front_proj
+        else:
+            # The main project for the content
+            proj = Project.safe_objects.filter(contents__in=[self], state=2)
+            if not proj:
+                self.create_front_project()
+                proj = self.front_project
+            else:
+                proj = proj[0]
+        return proj
 
     
     # Tag management
--- a/src/ldt/ldt/ldt_utils/views/front.py	Wed Aug 08 17:20:37 2012 +0200
+++ b/src/ldt/ldt/ldt_utils/views/front.py	Thu Aug 09 18:06:36 2012 +0200
@@ -101,17 +101,7 @@
         
     # If project id is not set, we get the default project for the content
     if project_id is None or project_id == "_":
-        front_proj = content.front_project
-        if front_proj:
-            proj = front_proj
-        else:
-            # The main project for the content
-            proj = Project.safe_objects.filter(contents__in=[content], state=2)
-            if not proj:
-                content.create_front_project()
-                proj = content.front_project
-            else:
-                proj = proj[0]           
+        proj = content.get_or_create_front_project()
     else:
         proj = Project.safe_objects.get(ldt_id=project_id)