add weight to api tag search
authorcavaliet
Wed, 12 Dec 2012 13:12:01 +0100
changeset 1024 f1d6e7acbd52
parent 1023 ea6dd883704d
child 1025 fc3d5e88cd45
add weight to api tag search
src/ldt/ldt/api/ldt/resources/tag.py
--- a/src/ldt/ldt/api/ldt/resources/tag.py	Tue Dec 11 11:15:35 2012 +0100
+++ b/src/ldt/ldt/api/ldt/resources/tag.py	Wed Dec 12 13:12:01 2012 +0100
@@ -26,15 +26,21 @@
         # Search by content ids is priori
         content_ids = request.GET.get('contents', '')
         segment_ids = request.GET.get('segments', '')
+        # We define the number of steps in weight int
+        try:
+            weight_steps = int(request.GET.get('steps', 10))
+        except:
+            weight_steps = 10
         tags_cloud = None
-        if content_ids != "":
+        if content_ids=="all" or segment_ids=="all":
+            tags_cloud = Tag.objects.cloud_for_model(Segment, steps=weight_steps)
+        elif content_ids != "":
             # We get all the segments for these contents
             content_ids = content_ids.split(',')
-            tags_cloud = Tag.objects.cloud_for_model(Segment, filters={"iri_id__in":content_ids})
-        if not all_segments and segment_ids != "":
+            tags_cloud = Tag.objects.cloud_for_model(Segment, filters={"iri_id__in":content_ids}, steps=weight_steps)
+        elif not all_segments and segment_ids != "":
             segment_ids = segment_ids.split(',')
-            tags_cloud = Tag.objects.cloud_for_model(Segment, filters={"element_id__in":segment_ids})
-        
+            tags_cloud = Tag.objects.cloud_for_model(Segment, filters={"element_id__in":segment_ids}, steps=weight_steps)
         
         limit = request.GET.get('limit', getattr(settings, 'API_LIMIT_PER_PAGE', 20))
         if limit == "0":
@@ -52,10 +58,16 @@
             bundle = self.build_bundle(obj=tag, request=request)
             bundle = self.full_dehydrate(bundle)
             objects.append(bundle)
-
+        
         object_list = {
             'objects': objects,
         }
 
         self.log_throttled_access(request)
-        return self.create_response(request, object_list)
\ No newline at end of file
+        return self.create_response(request, object_list)
+    
+    def dehydrate(self, bundle):
+        # This function enable to add the weight of a tag in the bundle's datas, which is not in the tag model
+        if bundle.obj and hasattr(bundle.obj,'font_size'):
+            bundle.data['weight'] = bundle.obj.font_size
+        return bundle
\ No newline at end of file