Add polemic properties to contents
authorverrierj
Fri, 27 Jan 2012 10:27:52 +0100
changeset 468 d1ff0694500b
parent 467 a1e9f5e91791
child 469 8004f0d1a1bf
Add polemic properties to contents
src/ldt/ldt/ldt_utils/contentindexer.py
src/ldt/ldt/ldt_utils/models.py
src/ldt/ldt/ldt_utils/segmentserializer.py
src/ldt/ldt/ldt_utils/stat.py
src/ldt/ldt/ldt_utils/templates/front/front_all_contents.html
src/ldt/ldt/ldt_utils/templates/front/front_search_results.html
src/ldt/ldt/ldt_utils/views/lignesdetemps.py
src/ldt/ldt/management/commands/statannotation.py
src/ldt/ldt/templatetags/front_tags.py
--- a/src/ldt/ldt/ldt_utils/contentindexer.py	Thu Jan 26 16:01:12 2012 +0100
+++ b/src/ldt/ldt/ldt_utils/contentindexer.py	Fri Jan 27 10:27:52 2012 +0100
@@ -83,6 +83,7 @@
 
                 title = reduce_text_node(elementNode, "title/text()")                
                 abstract = reduce_text_node(elementNode, "abstract/text()")
+                polemics = elementNode.xpath('meta/polemics/polemic/text()')
                 
                 author = elementNode.get("author", "")
                 start_ts = int(float(elementNode.get("begin", "-1")))
@@ -120,6 +121,7 @@
                               date=date_str,
                               project_obj=project,
                               project_id=ldt_id)
+                seg.polemics = seg.get_polemic(polemics)
                 seg.save()
                 self.writer.addDocument(doc)
 
--- a/src/ldt/ldt/ldt_utils/models.py	Thu Jan 26 16:01:12 2012 +0100
+++ b/src/ldt/ldt/ldt_utils/models.py	Fri Jan 27 10:27:52 2012 +0100
@@ -376,6 +376,68 @@
     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,               
+           }
+    
+    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']]
+        
+        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)
+            
+        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())
+    
+    
+    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(',')] 
 
 
     
@@ -521,8 +583,8 @@
             return True
         else:
             return False
-        
-
+    
+    
 class Segment(SafeModel):
     
     project_obj = models.ForeignKey(Project, null=True)
@@ -541,6 +603,32 @@
     abstract = models.TextField(null=True, blank=True)
     polemics = models.IntegerField(null=True, blank=True, default=0)
     
+    
+    # Mask is 0000, representing OK|KO|Q|REF
+    mask = {
+            'OK': set([8,9,10,11,12,13,14,15]),
+            'KO': set([4,5,6,7,12,13,14,15]),
+            'Q': set([2,3,6,7,10,11,14,15]),
+            'REF': set([1,3,5,7,9,11,13,15]),
+            }
+    
+    def is_polemic(self, polemic_keyword): # OK, KO, Q, REF 
+        if self.polemics in self.mask[polemic_keyword]:
+            return True
+        return False
+    
+    def get_polemic(self, polemic_keywords):
+        value = set(range(16))
+        
+        for keyword in self.mask.keys():
+            if keyword in polemic_keywords:
+                value = value.intersection(self.mask[keyword])
+            else:
+                value.difference_update(self.mask[keyword])
+            
+        return value.pop()
+        
+    
     def __unicode__(self):
         return "/".join((unicode(self.project_id), unicode(self.iri_id), unicode(self.ensemble_id), unicode(self.cutting_id), unicode(self.element_id)))
     
--- a/src/ldt/ldt/ldt_utils/segmentserializer.py	Thu Jan 26 16:01:12 2012 +0100
+++ b/src/ldt/ldt/ldt_utils/segmentserializer.py	Fri Jan 27 10:27:52 2012 +0100
@@ -1,5 +1,4 @@
 from django.conf import settings
-from django.utils.datastructures import SortedDict
 from ldt.ldt_utils.models import Project
 import lxml.etree
 import uuid
--- a/src/ldt/ldt/ldt_utils/stat.py	Thu Jan 26 16:01:12 2012 +0100
+++ b/src/ldt/ldt/ldt_utils/stat.py	Fri Jan 27 10:27:52 2012 +0100
@@ -12,16 +12,26 @@
     size_division = content.duration / nb_division
     limits = [x * size_division for x in range(nb_division+1)]
     nb_annotation = len(segments)
+    
+    polemic_positive = 0
+    polemic_negative = 0
 
     for segment in segments:
         
         begin = segment.start_ts
         end = segment.start_ts + segment.duration
         
+        if segment.is_polemic('OK'):
+            polemic_positive += 1
+        if segment.is_polemic('KO'):
+            polemic_negative += 1
+        
         buckets = find_buckets(buckets, limits, begin, end)
         
-    content.stat_annotation = get_string_from_buckets(buckets)
+    content.annotation_volume = buckets
     content.nb_annotation = nb_annotation
+    content.polemic_positive = polemic_positive
+    content.polemic_negative = polemic_negative
     
     content.save()     
  
@@ -48,7 +58,7 @@
         content.stat_annotation = get_empty_stat_vector()
     
     buckets = find_buckets(content.stat_annotation, begin, end)
-    content.stat_annotation = get_string_from_buckets(buckets)
+    content.annotation_volume = buckets
     content.last_annotated = datetime.datetime.now()
     
     content.save()        
@@ -64,7 +74,7 @@
     return [int(x) for x in string.split(',')]
 
 def get_empty_stat_vector():
-    return [0] * (settings.DIVISIONS_FOR_STAT_ANNOTATION + 4)
+    return [0] * (settings.DIVISIONS_FOR_STAT_ANNOTATION + 4) # + 4 for polemic syntax
 
 
 def find_buckets(buckets, limits, begin, end):
--- a/src/ldt/ldt/ldt_utils/templates/front/front_all_contents.html	Thu Jan 26 16:01:12 2012 +0100
+++ b/src/ldt/ldt/ldt_utils/templates/front/front_all_contents.html	Fri Jan 27 10:27:52 2012 +0100
@@ -37,7 +37,7 @@
                     <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>
+                <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>
--- a/src/ldt/ldt/ldt_utils/templates/front/front_search_results.html	Thu Jan 26 16:01:12 2012 +0100
+++ b/src/ldt/ldt/ldt_utils/templates/front/front_search_results.html	Fri Jan 27 10:27:52 2012 +0100
@@ -30,7 +30,7 @@
                     "duration" : {{segment.duration}},
                 },{% endfor %}
             ],
-            "stats" : [{{ res.content.stat_annotation }}],
+            "stats" : {{ res.content.annotation_volume }},
         },{% endfor %}
     ];
     
--- a/src/ldt/ldt/ldt_utils/views/lignesdetemps.py	Thu Jan 26 16:01:12 2012 +0100
+++ b/src/ldt/ldt/ldt_utils/views/lignesdetemps.py	Fri Jan 27 10:27:52 2012 +0100
@@ -285,29 +285,23 @@
             new_contents.append(id)
     
         check_icon_project = False
-        contents_remove = []
         
         #find contents to remove
         for c in ldtproject.contents.all():
             if not c.iri_id in new_contents:
-                contents_remove.append(c)
+                ldtproject.contents.remove(c)
                 if ldtproject.image == c.image:
                     check_icon_project = True
         
-        #remove contents
-        AnnotationStat.objects.filter(content__in=contents_remove, project=ldtproject).delete()
-                
         #add new contents
         contents_id = [c.iri_id for c in ldtproject.contents.all()]
         
         for c in new_contents:
             if c not in contents_id:
                 content = Content.objects.get(iri_id=c)
-                rel = AnnotationStat(project=ldtproject, content=content, stat='')
-                rel.save()
+                ldtproject.contents.add(content)
          
-        #update stats for all contents in this project and contents removed     
-        update_stat_project(ldtproject, ldtproject.contents.all(), update_contents=contents_remove)
+        update_stat_project(ldtproject)
         
         #remove html tags added by flash
         description = ldtproject.get_description(doc)
--- a/src/ldt/ldt/management/commands/statannotation.py	Thu Jan 26 16:01:12 2012 +0100
+++ b/src/ldt/ldt/management/commands/statannotation.py	Fri Jan 27 10:27:52 2012 +0100
@@ -1,4 +1,3 @@
-from django.conf import settings
 from django.core.management.base import BaseCommand
 from ldt.ldt_utils.models import Content
 from ldt.ldt_utils.stat import update_stat_content, get_empty_stat_vector
@@ -9,14 +8,16 @@
     
     def handle(self, *args, **options):             
        
-        size_stat = len(get_empty_stat_vector())
+        empty_stat_vector = get_empty_stat_vector()
+        size_stat = len(empty_stat_vector)
        
         for c in Content.objects.all():
-            if len( c.stat_annotation.split(',')) != size_stat:
-                c.stat_annotation = [0] * size_stat
+            if not c.stat_annotation or len( c.stat_annotation.split(',')) != size_stat:
+                c.stat_annotation = c.list2str(empty_stat_vector)
                 c.save()
             
             update_stat_content(c)
-                  
+            
+        print "Done"                  
         return None
     
\ No newline at end of file
--- a/src/ldt/ldt/templatetags/front_tags.py	Thu Jan 26 16:01:12 2012 +0100
+++ b/src/ldt/ldt/templatetags/front_tags.py	Fri Jan 27 10:27:52 2012 +0100
@@ -48,3 +48,7 @@
 @register.filter
 def get_range(value):
     return range(value)
+
+@register.filter
+def list2str(value):
+    return ("%s"%value)[1:-1]