add url arguments to uniquely identify annotations in api
authorymh <ymh.work@gmail.com>
Fri, 18 Sep 2015 12:45:31 +0200
changeset 1442 eaca389b4df8
parent 1441 88f6b795bf95
child 1443 d59952947289
add url arguments to uniquely identify annotations in api
src/ldt/ldt/api/ldt/resources/annotation.py
src/ldt/ldt/api/ldt/resources/content.py
src/ldt/ldt/api/ldt/tests/tests_annotation.py
--- a/src/ldt/ldt/api/ldt/resources/annotation.py	Thu Sep 17 19:45:35 2015 +0200
+++ b/src/ldt/ldt/api/ldt/resources/annotation.py	Fri Sep 18 12:45:31 2015 +0200
@@ -3,10 +3,12 @@
 from ldt.ldt_utils.utils import LdtAnnotation
 from ldt.security import protect_models, unprotect_models
 
+from django.conf.urls import url
 from tastypie import fields
 from tastypie.authorization import Authorization
 from tastypie.exceptions import NotFound, BadRequest
 from tastypie.resources import Resource
+from tastypie.utils import trailing_slash
 
 
 class AnnotationObject(object):
@@ -45,6 +47,11 @@
         always_return_data = True
         include_resource_uri = False
 
+    def prepend_urls(self):
+        return [
+            url(r"^(?P<resource_name>%s)/(?P<element_id>[^/]*?)(?:/(?P<project_id>[^/]*?)(?:/(?P<iri_id>[^/]*?)(?:/(?P<grouping_id>[^/]*?)(?:/(?P<cutting_id>[^/]*?))?)?)?)?%s$" % (self._meta.resource_name, trailing_slash()), self.wrap_view('dispatch_detail'), name="api_dispatch_detail"),
+        ]
+
     def extract_annotation_data(self, data):
         # This method extracts and validates the data we receive from the user for adding or editing an annotation
 
@@ -198,7 +205,7 @@
     def obj_update(self, bundle, **kwargs):
         data_dict = self.extract_annotation_data(bundle.data)
         if not data_dict['ann_id']:
-            data_dict['ann_id'] = kwargs["pk"]
+            data_dict['ann_id'] = kwargs["element_id"]
 
         adder = LdtAnnotation(data_dict["project_obj"])
         try:
@@ -272,11 +279,20 @@
         return bundle
 
     def obj_delete(self, bundle, **kwargs):
-        ann_id = kwargs.get("pk", "")
+        query_args = {
+            'project_id' : kwargs.get("project_id", None),
+            'iri_id' : kwargs.get("iri_id", None),
+            'ensemble_id' : kwargs.get("grouping_id", None),
+            'cutting_id' : kwargs.get("cutting_id", None),
+            'element_id' : kwargs.get("element_id", None)
+        }
+        query_args = { k: v for k,v in query_args.items() if v }
         try:
-            seg_to_delete = Segment.objects.get(element_id=ann_id)
+            seg_to_delete = Segment.objects.get(**query_args)
         except Segment.DoesNotExist:
-            raise NotFound("Segment not found. element_id = " + ann_id)
+            raise NotFound("Segment not found. element_id = " + query_args['element_id'])
+        except Segment.MultipleObjectsReturned:
+            raise NotFound("Multiple segments found. element_id = " + query_args['element_id'])
         project_id = seg_to_delete.project_id
         try:
             project = Project.objects.get(ldt_id=project_id)
@@ -288,10 +304,10 @@
         adder = LdtAnnotation(project)
         try:
             unprotect_models()
-            deleted = adder.delete(ann_id, iri_id, ann_type_id)
+            deleted = adder.delete(query_args['element_id'], iri_id, ann_type_id)
             if not deleted:
                 raise BadRequest
-            delete_segment(project, project_id, iri_id, ensemble_id, ann_type_id, ann_id)
+            delete_segment(project, project_id, iri_id, ensemble_id, ann_type_id, query_args['element_id'])
             adder.save()
         finally:
             protect_models()
--- a/src/ldt/ldt/api/ldt/resources/content.py	Thu Sep 17 19:45:35 2015 +0200
+++ b/src/ldt/ldt/api/ldt/resources/content.py	Fri Sep 18 12:45:31 2015 +0200
@@ -55,7 +55,7 @@
         return [
             url(r"^(?P<resource_name>%s)/recommended/$" % self._meta.resource_name, self.wrap_view('get_recommended'), name="api_contents_recommended"),
             url(r"^(?P<resource_name>%s)/all/(?P<iri_id>[\w\d_.-]+)/$" % self._meta.resource_name, self.wrap_view('get_all_projects'), name="api_content_all_projects"),
-            url(r"^(?P<resource_name>%s)/(?P<iri_id>[\w\d_.-]+)/$" % self._meta.resource_name, self.wrap_view('dispatch_detail'), name="api_dispatch_detail"),            
+            url(r"^(?P<resource_name>%s)/(?P<iri_id>[\w\d_.-]+)/$" % self._meta.resource_name, self.wrap_view('dispatch_detail'), name="api_dispatch_detail"),
         ]
 
     def get_resource_uri(self, bundle_or_obj=None, url_name='api_dispatch_list'):
--- a/src/ldt/ldt/api/ldt/tests/tests_annotation.py	Thu Sep 17 19:45:35 2015 +0200
+++ b/src/ldt/ldt/api/ldt/tests/tests_annotation.py	Fri Sep 18 12:45:31 2015 +0200
@@ -77,7 +77,7 @@
         editing_data["id"] = ann_id
         editing_data["type"] = type_id
         
-        put_resp = self.api_client.client.put(reverse('api_dispatch_detail', kwargs={'api_name':'1.0', 'resource_name':'annotations', 'pk': ann_id}), content_type='application/json', data=json.dumps(editing_data))
+        put_resp = self.api_client.client.put(reverse('api_dispatch_detail', kwargs={'api_name':'1.0', 'resource_name':'annotations', 'element_id': ann_id}), content_type='application/json', data=json.dumps(editing_data))
         edited_ann_data = json.loads(put_resp.content)
         edited_annotation = Segment.objects.get(project_id=edited_ann_data["project"], iri_id=edited_ann_data["media"], cutting_id=edited_ann_data["type"], element_id=edited_ann_data["id"])
         
@@ -95,7 +95,8 @@
         post_resp = self.api_client.client.post(reverse('api_dispatch_list', kwargs={'api_name':'1.0', 'resource_name':'annotations'}), content_type='application/json', data=self.test_annotation_json)       
         ann_id = json.loads(post_resp.content)["id"]
         
-        delete_resp = self.api_client.client.delete(reverse('api_dispatch_detail', kwargs={'api_name':'1.0', 'resource_name':'annotations', 'pk': ann_id}))
+        self.api_client.client.delete(reverse('api_dispatch_detail', kwargs={'api_name':'1.0', 'resource_name':'annotations', 'element_id': ann_id}))
+        
         
         deleted_annotation = Segment.objects.filter(element_id = ann_id)
         self.assertFalse(deleted_annotation.exists())