change stat_annotation to one to one field
authorymh <ymh.work@gmail.com>
Thu, 09 Feb 2012 22:17:34 +0100
changeset 551 c447d863b6ad
parent 550 2cea1d92b19d
child 552 6fbf608bd2a7
change stat_annotation to one to one field
.settings/org.eclipse.core.resources.prefs
src/ldt/ldt/ldt_utils/migrations/0016_one_to_one_stat_annotation.py
src/ldt/ldt/ldt_utils/models.py
src/ldt/ldt/ldt_utils/stat.py
src/ldt/ldt/management/commands/statannotation.py
--- a/.settings/org.eclipse.core.resources.prefs	Thu Feb 09 15:14:23 2012 +0100
+++ b/.settings/org.eclipse.core.resources.prefs	Thu Feb 09 22:17:34 2012 +0100
@@ -1,4 +1,4 @@
-#Tue Feb 07 18:07:06 CET 2012
+#Thu Feb 09 19:01:11 CET 2012
 eclipse.preferences.version=1
 encoding//src/ldt/ldt/core/migrations/0001_initial.py=utf-8
 encoding//src/ldt/ldt/core/migrations/0002_auto__del_owner.py=utf-8
@@ -12,6 +12,7 @@
 encoding//src/ldt/ldt/ldt_utils/migrations/0010_auto__add_annotationstat.py=utf-8
 encoding//src/ldt/ldt/ldt_utils/migrations/0011_gen_stat_annotation.py=utf-8
 encoding//src/ldt/ldt/ldt_utils/migrations/0015_auto__add_contentstat__del_field_content_last_annotated__del_field_con.py=utf-8
+encoding//src/ldt/ldt/ldt_utils/migrations/0016_one_to_one_stat_annotation.py=utf-8
 encoding//src/ldt/ldt/management/utils.py=utf-8
 encoding//src/ldt/ldt/text/migrations/0001_initial.py=utf-8
 encoding//src/ldt/ldt/user/migrations/0001_initial.py=utf-8
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ldt/ldt/ldt_utils/migrations/0016_one_to_one_stat_annotation.py	Thu Feb 09 22:17:34 2012 +0100
@@ -0,0 +1,145 @@
+# encoding: utf-8
+from south.db import db #@UnresolvedImport
+from south.v2 import SchemaMigration #@UnresolvedImport
+
+class Migration(SchemaMigration):
+
+    def forwards(self, orm):
+        
+        # Changing field 'ContentStat.content'
+        db.alter_column('ldt_utils_contentstat', 'content_id', self.gf('django.db.models.fields.related.OneToOneField')(unique=True, to=orm['ldt_utils.Content']))
+
+
+    def backwards(self, orm):
+        
+        # Changing field 'ContentStat.content'
+        db.alter_column('ldt_utils_contentstat', 'content_id', self.gf('django.db.models.fields.related.ForeignKey')(unique=True, to=orm['ldt_utils.Content']))
+
+
+    models = {
+        'auth.group': {
+            'Meta': {'object_name': 'Group'},
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'name': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '80'}),
+            'permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'})
+        },
+        'auth.permission': {
+            'Meta': {'ordering': "('content_type__app_label', 'content_type__model', 'codename')", 'unique_together': "(('content_type', 'codename'),)", 'object_name': 'Permission'},
+            'codename': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+            'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '50'})
+        },
+        'auth.user': {
+            'Meta': {'object_name': 'User'},
+            'date_joined': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+            'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'blank': 'True'}),
+            'first_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
+            'groups': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Group']", 'symmetrical': 'False', 'blank': 'True'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'is_active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
+            'is_staff': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'is_superuser': ('django.db.models.fields.BooleanField', [], {'default': 'False'}),
+            'last_login': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now'}),
+            'last_name': ('django.db.models.fields.CharField', [], {'max_length': '30', 'blank': 'True'}),
+            'password': ('django.db.models.fields.CharField', [], {'max_length': '128'}),
+            'user_permissions': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['auth.Permission']", 'symmetrical': 'False', 'blank': 'True'}),
+            'username': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '30'})
+        },
+        'contenttypes.contenttype': {
+            'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
+            'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+            'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
+        },
+        'ldt_utils.author': {
+            'Meta': {'object_name': 'Author'},
+            'email': ('django.db.models.fields.EmailField', [], {'max_length': '75', 'null': 'True', 'blank': 'True'}),
+            'firstname': ('django.db.models.fields.CharField', [], {'max_length': '512', 'null': 'True', 'blank': 'True'}),
+            'handle': ('django.db.models.fields.CharField', [], {'max_length': '512', 'unique': 'True', 'null': 'True', 'blank': 'True'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'lastname': ('django.db.models.fields.CharField', [], {'max_length': '512', 'null': 'True', 'blank': 'True'})
+        },
+        'ldt_utils.content': {
+            'Meta': {'ordering': "['title']", 'object_name': 'Content'},
+            'authors': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['ldt_utils.Author']", 'symmetrical': 'False', 'blank': 'True'}),
+            'content_creation_date': ('django.db.models.fields.DateTimeField', [], {'null': 'True', 'blank': 'True'}),
+            'creation_date': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+            'description': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}),
+            'duration': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'blank': 'True'}),
+            'front_project': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['ldt_utils.Project']", 'null': 'True', 'blank': 'True'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'image': ('sorl.thumbnail.fields.ImageField', [], {'default': "'thumbnails/contents/content_default_icon.png'", 'max_length': '200'}),
+            'iri_id': ('django.db.models.fields.CharField', [], {'default': "u'ef91e8c2-5347-11e1-ad5a-58b035f6b93d'", 'unique': 'True', 'max_length': '1024'}),
+            'iriurl': ('django.db.models.fields.CharField', [], {'max_length': '1024'}),
+            'media_obj': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['ldt_utils.Media']", 'null': 'True', 'blank': 'True'}),
+            'tags': ('tagging.fields.TagField', [], {'max_length': '2048', 'null': 'True'}),
+            'title': ('django.db.models.fields.CharField', [], {'max_length': '1024', 'null': 'True', 'blank': 'True'}),
+            'update_date': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'})
+        },
+        'ldt_utils.contentstat': {
+            'Meta': {'object_name': 'ContentStat'},
+            'annotation_volume_str': ('django.db.models.fields.CommaSeparatedIntegerField', [], {'max_length': '1024', 'null': 'True', 'blank': 'True'}),
+            'content': ('django.db.models.fields.related.OneToOneField', [], {'related_name': "'stat_annotation'", 'unique': 'True', 'to': "orm['ldt_utils.Content']"}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'last_annotated': ('django.db.models.fields.DateTimeField', [], {'default': 'datetime.datetime.now', 'null': 'True', 'blank': 'True'}),
+            'nb_annotations': ('django.db.models.fields.IntegerField', [], {'default': '0', 'db_index': 'True'}),
+            'polemics_volume_str': ('django.db.models.fields.CommaSeparatedIntegerField', [], {'max_length': '1024', 'null': 'True', 'blank': 'True'})
+        },
+        'ldt_utils.media': {
+            'Meta': {'object_name': 'Media'},
+            'creation_date': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+            'creator': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']", 'null': 'True', 'blank': 'True'}),
+            'description': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}),
+            'duration': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'blank': 'True'}),
+            'external_id': ('django.db.models.fields.CharField', [], {'max_length': '1024', 'null': 'True', 'blank': 'True'}),
+            'external_permalink': ('django.db.models.fields.URLField', [], {'max_length': '1024', 'null': 'True', 'blank': 'True'}),
+            'external_publication_url': ('django.db.models.fields.URLField', [], {'max_length': '1024', 'null': 'True', 'blank': 'True'}),
+            'external_src_url': ('django.db.models.fields.CharField', [], {'max_length': '1024', 'null': 'True', 'blank': 'True'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'media_creation_date': ('django.db.models.fields.DateTimeField', [], {'null': 'True', 'blank': 'True'}),
+            'mimetype_field': ('django.db.models.fields.CharField', [], {'max_length': '512', 'null': 'True', 'blank': 'True'}),
+            'src': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '1024'}),
+            'title': ('django.db.models.fields.CharField', [], {'max_length': '1024', 'null': 'True', 'blank': 'True'}),
+            'update_date': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
+            'videopath': ('django.db.models.fields.CharField', [], {'max_length': '1024', 'null': 'True', 'blank': 'True'})
+        },
+        'ldt_utils.project': {
+            'Meta': {'ordering': "['title']", 'object_name': 'Project'},
+            'changed_by': ('django.db.models.fields.CharField', [], {'max_length': '70'}),
+            'contents': ('django.db.models.fields.related.ManyToManyField', [], {'to': "orm['ldt_utils.Content']", 'symmetrical': 'False'}),
+            'created_by': ('django.db.models.fields.CharField', [], {'max_length': '70'}),
+            'creation_date': ('django.db.models.fields.DateTimeField', [], {'auto_now_add': 'True', 'blank': 'True'}),
+            'description': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'image': ('sorl.thumbnail.fields.ImageField', [], {'default': "'thumbnails/projects/project_default_icon.png'", 'max_length': '200'}),
+            'ldt': ('django.db.models.fields.TextField', [], {'null': 'True'}),
+            'ldt_id': ('django.db.models.fields.CharField', [], {'unique': 'True', 'max_length': '1024'}),
+            'modification_date': ('django.db.models.fields.DateTimeField', [], {'auto_now': 'True', 'blank': 'True'}),
+            'owner': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['auth.User']", 'null': 'True', 'blank': 'True'}),
+            'state': ('django.db.models.fields.IntegerField', [], {'default': '1'}),
+            'title': ('django.db.models.fields.CharField', [], {'max_length': '1024'})
+        },
+        'ldt_utils.segment': {
+            'Meta': {'unique_together': "(('project_id', 'iri_id', 'ensemble_id', 'cutting_id', 'element_id'),)", 'object_name': 'Segment'},
+            'abstract': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}),
+            'author': ('django.db.models.fields.CharField', [], {'max_length': '1024', 'null': 'True', 'blank': 'True'}),
+            'content': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['ldt_utils.Content']"}),
+            'cutting_id': ('django.db.models.fields.CharField', [], {'max_length': '1024'}),
+            'date': ('django.db.models.fields.CharField', [], {'max_length': '128', 'null': 'True', 'blank': 'True'}),
+            'duration': ('django.db.models.fields.IntegerField', [], {'null': 'True'}),
+            'element_id': ('django.db.models.fields.CharField', [], {'max_length': '1024'}),
+            'ensemble_id': ('django.db.models.fields.CharField', [], {'max_length': '1024'}),
+            'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+            'iri_id': ('django.db.models.fields.CharField', [], {'max_length': '1024'}),
+            'polemics': ('django.db.models.fields.IntegerField', [], {'default': '0', 'null': 'True', 'blank': 'True'}),
+            'project_id': ('django.db.models.fields.CharField', [], {'max_length': '1024', 'null': 'True', 'blank': 'True'}),
+            'project_obj': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['ldt_utils.Project']", 'null': 'True'}),
+            'start_ts': ('django.db.models.fields.IntegerField', [], {'null': 'True'}),
+            'tags': ('tagging.fields.TagField', [], {'max_length': '2048', 'null': 'True'}),
+            'title': ('django.db.models.fields.CharField', [], {'max_length': '2048', 'null': 'True', 'blank': 'True'})
+        }
+    }
+
+    complete_apps = ['ldt_utils']
--- a/src/ldt/ldt/ldt_utils/models.py	Thu Feb 09 15:14:23 2012 +0100
+++ b/src/ldt/ldt/ldt_utils/models.py	Thu Feb 09 22:17:34 2012 +0100
@@ -387,30 +387,27 @@
 
     def __make_getter(self, i):
         def inner_getter(self):
-            if self.stat_annotation.count() == 0:
+            if self.stat_annotation is None:
                 return 0;
             else:                
-                contentstate = self.stat_annotation.all()[0]
-                l = contentstate.polemics_volume
+                l = self.stat_annotation.polemics_volume
                 return l[POL_INDICES[i]]
         return inner_getter
         
     def __make_rate(self, i):
         def inner_rate(self):
-            if self.stat_annotation.count() == 0 or self.stat_annotation.all()[0].nb_annotation <= 0:
+            if self.stat_annotation is None or self.stat_annotation.nb_annotations <= 0:
                 return 0
-            return int(getattr(self, i) / float(self.stat_annotation.all()[0].nb_annotation) * 100 )
+            return int(getattr(self, i) / float(self.stat_annotation.nb_annotations) * 100 )
         return inner_rate   
     
                     
-    def annotation_volume(): #@NoSelf
-        
+    def annotation_volume(): #@NoSelf        
         def fget(self):
-            if self.stat_annotation.count() == 0:
-                contentstate, created = ContentStat.objects.get_or_create(content=self) #@UnusedVariable
+            if self.stat_annotation is None:
+                return [0]*settings.DIVISIONS_FOR_STAT_ANNOTATION
             else:
-                contentstate = self.stat_annotation.all()[0]
-            return contentstate.annotation_volume
+                return self.stat_annotation.annotation_volume
                     
         return locals()
     
@@ -419,10 +416,10 @@
     
     def nb_annotations(): #@NoSelf
         def fget(self):
-            if self.stat_annotation.count() == 0:
+            if self.stat_annotation is None:
                 return 0
             else:
-                return self.stat_annotation.all()[0].nb_annotations
+                return self.stat_annotation.nb_annotations
             
         return locals()
     
@@ -438,11 +435,12 @@
 
 class ContentStat(models.Model):
     
-    def __init__(self, *args, **kwargs):        
+    def __init__(self, *args, **kwargs):                
         super(ContentStat, self).__init__(*args, **kwargs)
-        self.__init_empty_stat()
+        if self.annotation_volume_str is None and self.polemics_volume_str is None:
+            self.__init_empty_stat()
     
-    content = models.ForeignKey(Content, blank=False, null=False, related_name='stat_annotation', verbose_name=_("content_stat.content"), unique=True)
+    content = models.OneToOneField(Content, blank=False, null=False, related_name='stat_annotation', verbose_name=_("content_stat.content"), unique=True, db_index=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)
--- a/src/ldt/ldt/ldt_utils/stat.py	Thu Feb 09 15:14:23 2012 +0100
+++ b/src/ldt/ldt/ldt_utils/stat.py	Thu Feb 09 22:17:34 2012 +0100
@@ -6,19 +6,19 @@
     
     nb_division = settings.DIVISIONS_FOR_STAT_ANNOTATION
     
-    segments = Segment.objects.filter(content=content).select_related(depth=1) #@UndefinedVariable
+    segments = Segment.objects.filter(content=content).select_related('project_obj').only('start_ts','duration','polemics','project_obj__modification_date') #@UndefinedVariable
     buckets = [0] * nb_division 
     
     size_division = content.duration / nb_division
     limits = [x * size_division for x in range(nb_division+1)]
-    nb_annotation = len(segments)
+    nb_annotation = segments.count()
     
-    contentstat, created = ContentStat.objects.get_or_create(content=content)
+    contentstat, created = ContentStat.objects.get_or_create(content=content) #@UnusedVariable @UndefinedVariable
 
     pol_stats = [0]*len(POL_INDICES)
     
-    min_update_ts = datetime.datetime.min
-
+    min_update_ts = datetime.datetime.min #@UndefinedVariable
+    
     for segment in segments:
         
         begin = segment.start_ts
@@ -34,13 +34,13 @@
             pol_stats[POL_INDICES['pol_reference']] += 1
         
         buckets = find_buckets(buckets, limits, begin, end)
-        if segment.project_obj.modification_date > min_update_ts:
+        if segment.project_obj is not None and segment.project_obj.modification_date > min_update_ts:
             min_update_ts = segment.project_obj.modification_date
-    
+            
     contentstat.polemics_volume = pol_stats
     contentstat.annotation_volume = buckets
     contentstat.nb_annotations = nb_annotation
-    contentstat.last_annotated = min_update_ts
+    contentstat.last_annotated = min_update_ts if min_update_ts > datetime.datetime.min else content.update_date #@UndefinedVariable
     
     contentstat.save()
  
--- a/src/ldt/ldt/management/commands/statannotation.py	Thu Feb 09 15:14:23 2012 +0100
+++ b/src/ldt/ldt/management/commands/statannotation.py	Thu Feb 09 22:17:34 2012 +0100
@@ -12,7 +12,7 @@
         total = Content.objects.count() #@UndefinedVariable
        
         for i,c in enumerate(Content.objects.select_related('stat_annotation').all()): #@UndefinedVariable
-            writer = show_progress(i+1, total, "content %s" % c.title, 50, writer)            
+            writer = show_progress(i+1, total, "content %d : %s" % (c.id,c.title), 50, writer)
             update_stat_content(c)
             
         return None