add all polemic attributes and polemic attribute rates on contents
authorverrierj
Fri, 27 Jan 2012 14:39:39 +0100
changeset 470 b1dd78b59750
parent 469 8004f0d1a1bf
child 471 c54a7864a22f
add all polemic attributes and polemic attribute rates on contents
src/ldt/ldt/ldt_utils/models.py
src/ldt/ldt/ldt_utils/templates/front/front_all_contents.html
src/ldt/ldt/ldt_utils/templates/front/front_home.html
src/ldt/ldt/ldt_utils/views/workspace.py
--- a/src/ldt/ldt/ldt_utils/models.py	Fri Jan 27 11:26:48 2012 +0100
+++ b/src/ldt/ldt/ldt_utils/models.py	Fri Jan 27 14:39:39 2012 +0100
@@ -141,6 +141,13 @@
         permissions = (
                        ('view_content', 'Can view content'),
                        )
+        
+    def __init__(self, *args, **kwargs):
+        
+        super(Content, self).__init__(*args, **kwargs)
+        
+        if not hasattr(Content, 'pol_positive'):
+            self.__add_polemic_attributes()
     
     def natural_key(self):
         return self.iri_id
@@ -376,68 +383,65 @@
     def get_tags(self):
         return Tag.objects.get_for_object(self)
     
-    indices = {
-           'annotation_volume_begin' : 0,
-           'annotation_volume_end' : settings.DIVISIONS_FOR_STAT_ANNOTATION,
-           'pol_positive' : settings.DIVISIONS_FOR_STAT_ANNOTATION,
-           'pol_negative' : settings.DIVISIONS_FOR_STAT_ANNOTATION + 1,
-           'pol_reference' : settings.DIVISIONS_FOR_STAT_ANNOTATION + 2,
-           'pol_question' : settings.DIVISIONS_FOR_STAT_ANNOTATION + 3,               
-           }
     
+    pol_indices = {
+               'annotation_volume_begin' : 0,
+               'annotation_volume_end' : settings.DIVISIONS_FOR_STAT_ANNOTATION,
+               'pol_positive' : settings.DIVISIONS_FOR_STAT_ANNOTATION,
+               'pol_negative' : settings.DIVISIONS_FOR_STAT_ANNOTATION + 1,
+               'pol_reference' : settings.DIVISIONS_FOR_STAT_ANNOTATION + 2,
+               'pol_question' : settings.DIVISIONS_FOR_STAT_ANNOTATION + 3,               
+               }    
+
+    # add polemic attributes and polemic attribute rates to class Content
+    def __add_polemic_attributes(self):
+        for element in self.pol_indices.keys():
+            if element.startswith('pol_'):                    
+                Content.add_to_class(element, property(self.__make_getter(element), self.__make_setter(element)))
+                Content.add_to_class("%s_rate" % element, property(self.__make_rate(element)))  
+
+    def __make_getter(self, i):
+        def inner_getter(self):
+            l = self.__str2list(self.stat_annotation)
+            return l[Content.pol_indices[i]]
+        return inner_getter
+    
+    def __make_setter(self, i):
+        def inner_setter(self, value):
+            l = self.__str2list(self.stat_annotation)
+            l[Content.pol_indices[i]] = value
+            self.stat_annotation = self.__list2str(l)
+        return inner_setter
+    
+    def __make_rate(self, i):
+        def inner_rate(self):
+            if self.nb_annotation <= 0:
+                return 0
+            return int(getattr(self, i) / float(self.nb_annotation) * 100 )
+        return inner_rate   
+    
+    def __list2str(self, l):
+        return ("%s" % l)[1:-1].replace(' ','')
+        
+    def __str2list(self, s):
+        return [int(x) for x in s.split(',')] 
+        
     def annotation_volume(): #@NoSelf
         
         def fget(self):
-            l = self.str2list(self.stat_annotation)
-            return l[Content.indices['annotation_volume_begin']:Content.indices['annotation_volume_end']]
+            l = self.__str2list(self.stat_annotation)
+            return l[Content.pol_indices['annotation_volume_begin']:Content.pol_indices['annotation_volume_end']]
         
         def fset(self, value): # value is a list
-            l = self.str2list(self.stat_annotation)
-            l[Content.indices['annotation_volume_begin']:Content.indices['annotation_volume_end']] = value
-            self.stat_annotation = self.list2str(l)
+            l = self.__str2list(self.stat_annotation)
+            l[Content.pol_indices['annotation_volume_begin']:Content.pol_indices['annotation_volume_end']] = value
+            self.stat_annotation = self.__list2str(l)
             
         return locals()
     
-    annotation_volume = property(**annotation_volume())
-    
-    def polemic_positive(): #@NoSelf
-        
-        def fget(self):
-            l = self.str2list(self.stat_annotation)
-            return l[Content.indices['pol_positive']]
-        
-        def fset(self, value):
-            l = self.str2list(self.stat_annotation)
-            l[Content.indices['pol_positive']] = value
-            self.stat_annotation = self.list2str(l)
-            
-        return locals()
-    
-    polemic_positive = property(**polemic_positive())
-    
+    annotation_volume = property(**annotation_volume())    
     
-    def polemic_negative(): #@NoSelf
-        
-        def fget(self):
-            l = self.str2list(self.stat_annotation)
-            return l[Content.indices['pol_negative']]
-        
-        def fset(self, value):
-            l = self.str2list(self.stat_annotation)
-            l[Content.indices['pol_negative']] = value
-            self.stat_annotation = self.list2str(l)
-            
-        return locals()
-    
-    polemic_negative = property(**polemic_negative())
-    
-    
-    
-    def list2str(self, l):
-        return ("%s" % l)[1:-1].replace(' ','')
-        
-    def str2list(self, s):
-        return [int(x) for x in s.split(',')] 
+ 
 
 
     
--- a/src/ldt/ldt/ldt_utils/templates/front/front_all_contents.html	Fri Jan 27 11:26:48 2012 +0100
+++ b/src/ldt/ldt/ldt_utils/templates/front/front_all_contents.html	Fri Jan 27 14:39:39 2012 +0100
@@ -32,8 +32,8 @@
             {% thumbnail content.image "294x165" format="PNG" crop="center" as im %}<img src="{{ im.url }}" class="img_media" width="{{ im.width }}" height="{{ im.height }}" alt="{% trans 'open this media' %}" title="{% trans 'open this media' %}">{% endthumbnail %}
             <div class="overlay">
                 <ul class="polemics">
-                    <li class="pol-negative" style="width:0%">0%</li>
-                    <li class="pol-positive" style="width:0%">0%</li>
+                    <li class="pol-negative" style="width:{{content.pol_positive_rate }}%">{{content.pol_positive_rate }}%</li>
+                    <li class="pol-positive" style="width:{{content.pol_negative_rate}}%">{{content.pol_negative_rate}}%</li>
                     <li class="pol-reference" style="width:0%">0%</li>
                     <li class="pol-question" style="width:0%">0%</li>
                 </ul>
--- a/src/ldt/ldt/ldt_utils/templates/front/front_home.html	Fri Jan 27 11:26:48 2012 +0100
+++ b/src/ldt/ldt/ldt_utils/templates/front/front_home.html	Fri Jan 27 14:39:39 2012 +0100
@@ -36,12 +36,12 @@
         <a class="img_and_overlay" href="{% url ldt.ldt_utils.views.front.annot_content content.iri_id %}">{% thumbnail content.image "294x165" format="PNG" crop="center" as im %}<img src="{{ im.url }}" class="img_media" width="{{ im.width }}" height="{{ im.height }}" alt="{% trans 'open this media' %}" title="{% trans 'open this media' %}">{% endthumbnail %}
             <div class="overlay">
                 <ul class="polemics">
-                    <li class="pol-negative" style="width:0%">0%</li>
-                    <li class="pol-positive" style="width:0%">0%</li>
+                    <li class="pol-negative" style="width:{{content.pol_positive_rate }}%">{{content.pol_positive_rate }}%</li>
+                    <li class="pol-positive" style="width:{{content.pol_negative_rate}}%">{{content.pol_negative_rate}}%</li>
                     <li class="pol-reference" style="width:0%">0%</li>
                     <li class="pol-question" style="width:0%">0%</li>
-                </ul>
-                <span class="graph_annotation" id="sp_{{ content.iri_id }}">{{ content.stat_annotation }}</span>
+                </ul>                
+                <span class="graph_annotation" id="sp_{{ content.iri_id }}">{{ content.annotation_volume|list2str }}</span>
             </div>
         </a>
         <div class="bulle_annot" title="{% blocktrans count nb=content.nb_annotation %}{{nb}} annotation on this media{% plural %}{{nb}} annotations on this media{% endblocktrans %}">{{ content.nb_annotation }}</div>
@@ -67,8 +67,8 @@
         {% thumbnail content.image "134x75" format="PNG" crop="center" as im %}<img src="{{ im.url }}" class="img_media" width="{{ im.width }}" height="{{ im.height }}" alt="{% trans 'open this media' %}" title="{% trans 'open this media' %}">{% endthumbnail %}
             <div class="overlay">
                 <ul class="polemics">
-                    <li class="pol-negative" style="width:0%">0%</li>
-                    <li class="pol-positive" style="width:0%">0%</li>
+                    <li class="pol-negative" style="width:{{content.pol_positive_rate }}%">{{content.pol_positive_rate }}%</li>
+                    <li class="pol-positive" style="width:{{content.pol_negative_rate}}%">{{content.pol_negative_rate}}%</li>
                     <li class="pol-reference" style="width:0%">0%</li>
                     <li class="pol-question" style="width:0%">0%</li>
                 </ul>
--- a/src/ldt/ldt/ldt_utils/views/workspace.py	Fri Jan 27 11:26:48 2012 +0100
+++ b/src/ldt/ldt/ldt_utils/views/workspace.py	Fri Jan 27 14:39:39 2012 +0100
@@ -167,7 +167,7 @@
         else:
             results = get_results_with_context(field, search) 
             
-            Segment.safe_objects.check_perm = False
+            Segment.safe_objects.check_perm = False # no permission policy for segments
             all_segments = Segment.safe_objects.filter(element_id__in=[e['element_id'] for e in results])
             all_projects = Project.safe_objects.filter(ldt_id__in=[e['project_id'] for e in results], state=2)
             all_contents = Content.safe_objects.filter(iri_id__in=[e['iri_id'] for e in results])