src/ldt/ldt/ldt_utils/models.py
changeset 542 54dfa397baa3
parent 521 20e7bb9d2d14
child 551 c447d863b6ad
--- a/src/ldt/ldt/ldt_utils/models.py	Wed Feb 08 11:46:49 2012 +0100
+++ b/src/ldt/ldt/ldt_utils/models.py	Wed Feb 08 14:00:15 2012 +0100
@@ -131,9 +131,6 @@
     tags = tagging.fields.TagField(max_length=2048, null=True, blank=True)
     media_obj = models.ForeignKey('Media', blank=True, null=True)
     image = ImageField(upload_to=settings.MEDIA_ROOT+"thumbnails/contents/", default=settings.DEFAULT_CONTENT_ICON, max_length=200)
-    stat_annotation = models.CommaSeparatedIntegerField(max_length=1024, null=True, blank=True, verbose_name=_("content.stat_annotation")) 
-    nb_annotation = models.IntegerField(null=True, blank=True, verbose_name=_('content.nb_annotation'))
-    last_annotated = models.DateTimeField(default=datetime.datetime.now, verbose_name=_('content.last_annotated'), blank=True, null=True)
     front_project = models.ForeignKey('Project', null=True, blank=True)
         
     class Meta:
@@ -222,12 +219,7 @@
         
         if not self.pk:
             create_front_project = True
-            
-            if not self.nb_annotation:
-                self.nb_annotation = 0  
-            if not self.stat_annotation:
-                self.__get_empty_stat()
-  
+              
 
         super(Content, self).save(*args, **kwargs)
         
@@ -386,76 +378,115 @@
         return Tag.objects.get_for_object(self)
     
     
-    __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():
+        for element in 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(element, property(self.__make_getter(element)))
                 Content.add_to_class("%s_rate" % element, property(self.__make_rate(element)))  
 
     def __make_getter(self, i):
         def inner_getter(self):
-            if not self.stat_annotation:
-                self.__get_empty_stat()
-            l = self.__str2list(self.stat_annotation)
-            return l[Content.__pol_indices[i]]
+            if self.stat_annotation.count() == 0:
+                return 0;
+            else:                
+                contentstate = self.stat_annotation.all()[0]
+                l = contentstate.polemics_volume
+                return l[POL_INDICES[i]]
         return inner_getter
-    
-    def __make_setter(self, i):
-        def inner_setter(self, value):
-            if not self.stat_annotation:
-                self.__get_empty_stat()
-            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:
+            if self.stat_annotation.count() == 0 or self.stat_annotation.all()[0].nb_annotation <= 0:
                 return 0
-            return int(getattr(self, i) / float(self.nb_annotation) * 100 )
+            return int(getattr(self, i) / float(self.stat_annotation.all()[0].nb_annotation) * 100 )
         return inner_rate   
     
+                    
+    def annotation_volume(): #@NoSelf
+        
+        def fget(self):
+            if self.stat_annotation.count() == 0:
+                contentstate, created = ContentStat.objects.get_or_create(content=self) #@UnusedVariable
+            else:
+                contentstate = self.stat_annotation.all()[0]
+            return contentstate.annotation_volume
+                    
+        return locals()
+    
+    annotation_volume = property(**annotation_volume())
+    
+    
+    def nb_annotations(): #@NoSelf
+        def fget(self):
+            if self.stat_annotation.count() == 0:
+                return 0
+            else:
+                return self.stat_annotation.all()[0].nb_annotations
+            
+        return locals()
+    
+    nb_annotations = property(**nb_annotations())
+
+POL_INDICES = {
+    'pol_positive' : 0,
+    'pol_negative' : 1,
+    'pol_reference' : 2,
+    'pol_question' : 3,               
+}    
+
+
+class ContentStat(models.Model):
+    
+    def __init__(self, *args, **kwargs):        
+        super(ContentStat, self).__init__(*args, **kwargs)
+        self.__init_empty_stat()
+    
+    content = models.ForeignKey(Content, blank=False, null=False, related_name='stat_annotation', verbose_name=_("content_stat.content"), unique=True)
+    annotation_volume_str = models.CommaSeparatedIntegerField(max_length=1024, null=True, blank=True, verbose_name=_("content_stat.annotations_volume"))
+    polemics_volume_str = models.CommaSeparatedIntegerField(max_length=1024, null=True, blank=True, verbose_name=_("content_stat.polemics_volume"))
+    nb_annotations = models.IntegerField(null=False, blank=False, verbose_name=_('content.nb_annotation'), default=0, db_index=True)
+    last_annotated = models.DateTimeField(default=datetime.datetime.now, verbose_name=_('content.last_annotated'), blank=True, null=True) #@UndefinedVariable
+    
+    def __init_empty_stat(self):
+        self.annotation_volume_str = ','.join(['0']*settings.DIVISIONS_FOR_STAT_ANNOTATION)
+        self.polemics_volume_str = ','.join(['0']*len(settings.SYNTAX.keys()))
+        self.nb_annotations = 0
+        self.last_annotated = None
+        
     def __list2str(self, l):
-        return ("%s" % l)[1:-1].replace(' ','')
+        return ','.join([str(c) for c in l])
         
     def __str2list(self, s):
         return [int(x) for x in s.split(',')] 
-    
-    def __get_empty_stat(self):
-        self.stat_annotation =  self.__list2str([0] * (settings.DIVISIONS_FOR_STAT_ANNOTATION + len(settings.SYNTAX.keys())))
-        
+
     def annotation_volume(): #@NoSelf
         
         def fget(self):
-            if not self.stat_annotation:
-                self.__get_empty_stat()
-            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
-            if not self.stat_annotation:
-                self.__get_empty_stat()
-            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 self.__str2list(self.annotation_volume_str)
+
+        def fset(self, value):
+            self.annotation_volume_str = self.__list2str(value)
             
         return locals()
     
     annotation_volume = property(**annotation_volume())
+    
+    def polemics_volume(): #@NoSelf
+        
+        def fget(self):
+            return self.__str2list(self.polemics_volume_str)
+
+        def fset(self, value):
+            self.polemics_volume_str = self.__list2str(value)
+            
+        return locals()
+    
+    polemics_volume = property(**polemics_volume())
 
 
     
+    
 class Project(Document, SafeModel):  
     STATE_CHOICES = (
     (1, 'edition'),