misc correction on the the migration and command
authorymh <ymh.work@gmail.com>
Wed, 23 Jan 2013 18:14:24 +0100
changeset 1082 28f591b96612
parent 1081 2e086097cb5d
child 1084 179ab492a798
misc correction on the the migration and command
src/ldt/ldt/ldt_utils/migrations/0026_set_relative_ldtproject.py
src/ldt/ldt/management/commands/set_projectldtiri.py
src/ldt/ldt/management/commands/setprojectldtiri.py
src/ldt/ldt/utils/projectldt_parser.py
--- a/src/ldt/ldt/ldt_utils/migrations/0026_set_relative_ldtproject.py	Wed Jan 23 17:19:56 2013 +0100
+++ b/src/ldt/ldt/ldt_utils/migrations/0026_set_relative_ldtproject.py	Wed Jan 23 18:14:24 2013 +0100
@@ -1,17 +1,12 @@
 # -*- coding: utf-8 -*-
-import datetime
-from south.db import db
+from django.core.management import call_command
 from south.v2 import DataMigration
-from django.db import models
-import lxml.etree
-from ldt.management.commands import set_projectldtiri
-from django.core.management import call_command
 
 
 class Migration(DataMigration):
 
     def forwards(self, orm):
-        call_command('set_projectldtiri')
+        call_command('setprojectldtiri')
 
     def backwards(self, orm):
         #do nothing
--- a/src/ldt/ldt/management/commands/set_projectldtiri.py	Wed Jan 23 17:19:56 2013 +0100
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,26 +0,0 @@
-from django.core.management.base import BaseCommand
-from ldt.ldt_utils.models import Content, Project
-from ldt.utils.projectldt_parser import relative_src_xml
-import lxml.etree
-
-class Command(BaseCommand):
-    help = 'set the .iri src in relative url'
-    
-    def handle(self, *args, **options):
-        project_list = Project.objects.all()
-        for p in project_list: #we check all the project in the database
-            no_more_media = False
-            modif = False
-            try : 
-                iri = lxml.etree.fromstring(p.ldt)
-            except :
-                iri = None
-                p.delete()
-            if iri is not None :
-                new_ldt, modif, no_more_media = relative_src_xml(iri)
-                if modif and not no_more_media:
-                    new_ldt = lxml.etree.tostring(new_ldt, pretty_print=True)
-                    p.ldt= new_ldt #we write the the new xml 
-                    p.save()
-                if no_more_media :
-                    p.delete()
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ldt/ldt/management/commands/setprojectldtiri.py	Wed Jan 23 18:14:24 2013 +0100
@@ -0,0 +1,33 @@
+from django.core.management.base import BaseCommand
+from ldt.ldt_utils.models import Project
+from ldt.utils.projectldt_parser import relative_src_xml
+import lxml.etree
+from ..utils import show_progress
+
+class Command(BaseCommand):
+    help = 'set the .iri src in relative url'
+    
+    def handle(self, *args, **options):
+        
+        writer = None
+        
+        project_list = list(Project.objects.all())
+        
+        for i,p in enumerate(project_list): #we check all the project in the database
+            
+            writer = show_progress(i+1, len(project_list), "Processing project %s" % p.title, 80, writer)
+            no_more_media = False
+            modif = False
+            try : 
+                iri = lxml.etree.fromstring(p.ldt)
+            except :
+                iri = None
+                p.delete()
+            if iri is not None :
+                new_ldt, modif, no_more_media = relative_src_xml(iri)
+                if modif and not no_more_media:
+                    new_ldt = lxml.etree.tostring(new_ldt, pretty_print=True)
+                    p.ldt= new_ldt #we write the the new xml 
+                    p.save(must_reindex=False)
+                if no_more_media :
+                    p.delete()
\ No newline at end of file
--- a/src/ldt/ldt/utils/projectldt_parser.py	Wed Jan 23 17:19:56 2013 +0100
+++ b/src/ldt/ldt/utils/projectldt_parser.py	Wed Jan 23 18:14:24 2013 +0100
@@ -1,6 +1,5 @@
 from ldt.utils.url import absolute_media_url, is_absolute
-from ldt.ldt_utils.models import Content, Project
-import lxml.etree
+from ldt.ldt_utils.models import Content
 
 def absolute_src_xml(doc):
     media_list = doc.xpath("/iri/medias/media")
@@ -21,16 +20,17 @@
         for element in media_list:
             src = element.get("src")
             id_content=element.get("id")
-            content = None
-            try :
-                content = Content.objects.get(iri_id=id_content)
-            except :
+            content_list = Content.objects.filter(iri_id=id_content)
+            if len(content_list) == 0:
                 element.getparent().remove(element)
                 modif = True
+                for content_elem in ldt.xpath("/iri/annotations/content[@id=\"%s\"]" % id_content):
+                    content_elem.getparent().remove(content_elem)
                 media_list = ldt.xpath("/iri/medias/media")
                 if len(media_list) == 0 :
                     no_more_media =  True
-            if content is not None :
+            else:
+                content = content_list[0]
                 right_src = content.relative_iri_url()
                 if not src == right_src: #we will modify only the project that have an absolute url
                     modif = True