Merge test
authorsamuel huron <admin@cybunk.com>
Thu, 17 Jun 2010 15:04:36 +0200
changeset 54 24397932219d
parent 53 1fcf0bd88ba3
child 55 d7c9feca3220
Merge test
sbin/sync/sync_ldt_platform
web/eulalie/__init__.py
web/eulalie/templates/eulalie/partial/embed.html
web/eulalie/templates/eulalie/workspace.html
web/ldt/ldt_utils/fileimport.py
web/ldt/ldt_utils/models.py
web/ldt/ldt_utils/utils.py
--- a/sbin/sync/sync_ldt_platform	Thu Jun 17 11:24:33 2010 +0200
+++ b/sbin/sync/sync_ldt_platform	Thu Jun 17 15:04:36 2010 +0200
@@ -1,29 +1,31 @@
-#!/usr/bin/env bash
-set -e
-if [ -d ~/tmp/eulalie_V$1 ]; then
-    rm -fr ~/tmp/eulalie_V$1;
-fi
-
-hg archive -r V$1 ~/tmp/eulalie_V$1
-
-#text2unix ~/tmp/eulalie_V$1
-
-if [ -d ~/tmp/eulalie_V$1 ]; then
-    cat <<EOT | rsync -Cvrlz --delete --filter=". -" ~/tmp/eulalie_V$1/web/ iri@web.iri.centrepompidou.fr:/iridata/www/web/eulalie/
-+ core
-P .htpasswd
-P .htaccess
-P eulalie/.htaccess
-P eulalie/config.py
-P robots.txt
-P env/***
-P log/***
-P index/***
-P static/media/***
-EOT
-fi
-
-if [ -d ~/tmp/eulalie_V$1 ]; then
-    echo "Deleting temp export"
-    rm -fr ~/tmp/eulalie_V$1;
-fi
+#!/usr/bin/env bash
+set -e
+if [ -d ~/tmp/eulalie_V$1 ]; then
+    rm -fr ~/tmp/eulalie_V$1;
+fi
+
+hg archive -r V$1 ~/tmp/eulalie_V$1
+
+#text2unix ~/tmp/eulalie_V$1
+
+if [ -d ~/tmp/eulalie_V$1 ]; then
+    cat <<EOT | rsync -Cvrlz --delete --filter=". -" ~/tmp/eulalie_V$1/web/ iri@web.iri.centrepompidou.fr:/iridata/www/web/eulalie/
++ core
+P .htpasswd
+P .htaccess
+P eulalie/.htaccess
+P eulalie/config.py
+P robots.txt
+P env/***
+P log/***
+P index/***
+P static/media/***
+EOT
+fi
+
+if [ -d ~/tmp/eulalie_V$1 ]; then
+    echo "Deleting temp export"
+    rm -fr ~/tmp/eulalie_V$1;
+fi
+
+ssh iri@web.iri.centrepompidou.fr sudo apache2ctl restart
--- a/web/eulalie/__init__.py	Thu Jun 17 11:24:33 2010 +0200
+++ b/web/eulalie/__init__.py	Thu Jun 17 15:04:36 2010 +0200
@@ -1,16 +1,16 @@
-VERSION = (0,7)
-
-VERSION_STR = unicode(".".join(map(lambda i:"%02d" % (i,), VERSION)))
-
-
-def get_version():
-    version = '%s.%s' % (VERSION[0], VERSION[1])
-    if VERSION[2]:
-        version = '%s.%s' % (version, VERSION[2])
-    if VERSION[3:] == ('alpha', 0):
-        version = '%s pre-alpha' % version
-    else:
-        if VERSION[3] != 'final':
-            version = '%s %s %s' % (version, VERSION[3], VERSION[4])
-    return version
-
+VERSION = (0,8)
+
+VERSION_STR = unicode(".".join(map(lambda i:"%02d" % (i,), VERSION)))
+
+
+def get_version():
+    version = '%s.%s' % (VERSION[0], VERSION[1])
+    if VERSION[2]:
+        version = '%s.%s' % (version, VERSION[2])
+    if VERSION[3:] == ('alpha', 0):
+        version = '%s pre-alpha' % version
+    else:
+        if VERSION[3] != 'final':
+            version = '%s %s %s' % (version, VERSION[3], VERSION[4])
+    return version
+
--- a/web/eulalie/templates/eulalie/partial/embed.html	Thu Jun 17 11:24:33 2010 +0200
+++ b/web/eulalie/templates/eulalie/partial/embed.html	Thu Jun 17 15:04:36 2010 +0200
@@ -1,5 +1,5 @@
-<div id="{{ player_id }}">
-</div>
-<script type="text/javascript">
-    playerLdt(550,310, "http://web.iri.centrepompidou.fr/eulalie/eulalie/{{ json_url }}","{{ player_id }}","/eulalie/static/swf/player.swf");
+<div id="{{ player_id }}">
+</div>
+<script type="text/javascript">
+    playerLdt(550,310, "{{ json_url }}","{{ player_id }}","{{WEB_URL}}{{MEDIA_URL}}swf/player.swf");
 </script>
\ No newline at end of file
--- a/web/eulalie/templates/eulalie/workspace.html	Thu Jun 17 11:24:33 2010 +0200
+++ b/web/eulalie/templates/eulalie/workspace.html	Thu Jun 17 15:04:36 2010 +0200
@@ -1,106 +1,106 @@
-{% extends "base.html" %}
-{% load i18n %}
-
-{% block js_declaration %}
-{{block.super}}
-<script type="text/javascript">
-$(document).ready(function(){
-
-	$('.ldt_link').nyroModal({
-           	type: 'iframe',
-           	forceType: 'iframe',
-       		height:662, 
-       		width:1022,
-       		bgColor: 'rgb(239, 239, 239)',
-       		padding: 5,
-       		titleFromIframe: false
-          	});
-        	
-     $('.ldt_link_create').nyroModal({
-            type: 'iframe',
-            forceType: 'iframe',
-            height:662, 
-            width:1022,
-            bgColor: 'rgb(239, 239, 239)',
-            padding: 5,
-            titleFromIframe: false,
-            beforeHideContent: function(elts, settings, callback){
-	             try {
-		              var res = $('#ldtInitSwf',$('#nyroModalIframe').contents());
-		              if(res.length > 0)
-		              {
-		              	   res.get(0).forceSave();
-		              }
-	             }
-	             catch(err)
-	             {
-	              // do nothing
-	             }                   
-	             callback();
-            }
-        });
-
- 	 $('.ldt_link_embed').click(function(e) {
- 		e.preventDefault();
- 		link = $(e.target);
- 		json_url = link.attr("href");
- 		player_id = link.attr("id");
- 		/*$.nyroModalSettings({
- 		 	ajax: { data: ({ json_url:json_url, player_id:player_id }) }
-     	});*/
- 	    $.nyroModalManual({
-            type: 'iframe',
-            forceType: 'iframe',
-            titleFromIframe: false,
- 	    	height:662, 
- 	   		width:650,
- 	   		bgColor: 'rgb(239, 239, 239)',
- 	   		padding: 5,
-	 	    url: '{% url eulalie.views.popup_embed %}?json_url='+escape(json_url)+'&player_id='+escape(player_id),
- 	    });
- 	    return false;
-      });    
-});
-</script>
-
-{% endblock %}
-
-{% block css_import %}
-{{block.super}}
-    <link rel="stylesheet" href="{{BASE_URL}}static/css/workspace.css" type="text/css"/>
-{% endblock %}
-
-{% block content %}
-
-<div class="span-24 last" id="contentsdiv">
-<h1>{% trans "content list" %}</h1>
-<table>
-{% for content in contents %}
-    <tr>
-        <td>{{ content.title }}</td>
-        <td><a href="{% url ldt.ldt_utils.views.index content.iri_id %}" class="ldt_link">{% trans "open ldt" %}</a></td>
-        <td><a href="{% url ldt.ldt_utils.views.create_project content.iri_id %}" class="ldt_link_create">{% trans "create project" %}</a></td>
-        {% url ldt.ldt_utils.views.project_json_externalid content.external_id as json_url_externalid %}
-        <td><a href="{{json_url_externalid}}" class="ldt_link_embed" id="player_project_{{content.iri_id}}">{% trans "link json by externalid" %}</a>
-        </td>
-    </tr>
-{% endfor %}
-</table>
-</div>
-
-<div class="span-24 last" id="projectsdiv">
-<h1>{% trans "project list" %}</h1>
-<table>
-{% for project in projects %}
-    <tr>
-        <td>{{ project.title }}</td>
-        <td><a href="{% url ldt.ldt_utils.views.indexProject project.ldt_id %}" class="ldt_link">{% trans "open ldt" %}</a></td>
-        <td><a href="{% url ldt.ldt_utils.views.copy_project project.ldt_id %}" class="ldt_link_create">{% trans "copy project" %}</a></td>
-        {% url ldt.ldt_utils.views.project_json_id project.ldt_id as json_url_id %}
-        <td><a href="{{json_url_id}}" class="ldt_link_embed" id="player_project_{{project.ldt_id}}">{% trans "link json by id" %}</a>
-    </tr>
-{% endfor %}
-</table>
-</div>
-
-{% endblock %}
+{% extends "base.html" %}
+{% load i18n %}
+
+{% block js_declaration %}
+{{block.super}}
+<script type="text/javascript">
+$(document).ready(function(){
+
+	$('.ldt_link').nyroModal({
+           	type: 'iframe',
+           	forceType: 'iframe',
+       		height:662, 
+       		width:1022,
+       		bgColor: 'rgb(239, 239, 239)',
+       		padding: 5,
+       		titleFromIframe: false
+          	});
+        	
+     $('.ldt_link_create').nyroModal({
+            type: 'iframe',
+            forceType: 'iframe',
+            height:662, 
+            width:1022,
+            bgColor: 'rgb(239, 239, 239)',
+            padding: 5,
+            titleFromIframe: false,
+            beforeHideContent: function(elts, settings, callback){
+	             try {
+		              var res = $('#ldtInitSwf',$('#nyroModalIframe').contents());
+		              if(res.length > 0)
+		              {
+		              	   res.get(0).forceSave();
+		              }
+	             }
+	             catch(err)
+	             {
+	              // do nothing
+	             }                   
+	             callback();
+            }
+        });
+
+ 	 $('.ldt_link_embed').click(function(e) {
+ 		e.preventDefault();
+ 		link = $(e.target);
+ 		json_url = link.attr("href");
+ 		player_id = link.attr("id");
+ 		/*$.nyroModalSettings({
+ 		 	ajax: { data: ({ json_url:json_url, player_id:player_id }) }
+     	});*/
+ 	    $.nyroModalManual({
+            type: 'iframe',
+            forceType: 'iframe',
+            titleFromIframe: false,
+ 	    	height:662, 
+ 	   		width:650,
+ 	   		bgColor: 'rgb(239, 239, 239)',
+ 	   		padding: 5,
+	 	    url: '{% url eulalie.views.popup_embed %}?json_url='+escape(json_url)+'&player_id='+escape(player_id),
+ 	    });
+ 	    return false;
+      });    
+});
+</script>
+
+{% endblock %}
+
+{% block css_import %}
+{{block.super}}
+    <link rel="stylesheet" href="{{BASE_URL}}static/css/workspace.css" type="text/css"/>
+{% endblock %}
+
+{% block content %}
+
+<div class="span-24 last" id="contentsdiv">
+<h1>{% trans "content list" %}</h1>
+<table>
+{% for content in contents %}
+    <tr>
+        <td>{{ content.title }}</td>
+        <td><a href="{% url ldt.ldt_utils.views.index content.iri_id %}" class="ldt_link">{% trans "open ldt" %}</a></td>
+        <td><a href="{% url ldt.ldt_utils.views.create_project content.iri_id %}" class="ldt_link_create">{% trans "create project" %}</a></td>
+        {% url ldt.ldt_utils.views.project_json_externalid content.external_id as json_url_externalid %}
+        <td><a href="{{WEB_URL}}{{json_url_externalid}}" class="ldt_link_embed" id="player_project_{{content.iri_id}}">{% trans "link json by externalid" %}</a>
+        </td>
+    </tr>
+{% endfor %}
+</table>
+</div>
+
+<div class="span-24 last" id="projectsdiv">
+<h1>{% trans "project list" %}</h1>
+<table>
+{% for project in projects %}
+    <tr>
+        <td>{{ project.title }}</td>
+        <td><a href="{% url ldt.ldt_utils.views.indexProject project.ldt_id %}" class="ldt_link">{% trans "open ldt" %}</a></td>
+        <td><a href="{% url ldt.ldt_utils.views.copy_project project.ldt_id %}" class="ldt_link_create">{% trans "copy project" %}</a></td>
+        {% url ldt.ldt_utils.views.project_json_id project.ldt_id as json_url_id %}
+        <td><a href="{{WEB_URL}}{{json_url_id}}" class="ldt_link_embed" id="player_project_{{project.ldt_id}}">{% trans "link json by id" %}</a>
+    </tr>
+{% endfor %}
+</table>
+</div>
+
+{% endblock %}
--- a/web/ldt/ldt_utils/fileimport.py	Thu Jun 17 11:24:33 2010 +0200
+++ b/web/ldt/ldt_utils/fileimport.py	Thu Jun 17 15:04:36 2010 +0200
@@ -1,379 +1,376 @@
-import tempfile
-import os.path
-import shutil
-from django.core.exceptions import ObjectDoesNotExist
-from ldt.utils import zipfileext
-from django.conf import settings
-from models import Content
-import xml.dom.minidom
-import xml.dom.ext #@UnresolvedImport
-import xml.xpath #@UnresolvedImport
-import fnmatch
-import uuid
-import urllib
-
-class FileImportError(Exception):
-    def __init__(self, value):
-        self.value = value
-    def __str__(self):
-        return repr(self.value)
-
-
-def Property(func):
-    return property(**func()) 
-
-class IriInfo(object):
-
-    
-    def __init__(self, id, order, titledesc, basepath="", videopath=settings.STREAM_URL, decoupage_blacklist = settings.DECOUPAGE_BLACKLIST, flatten = True):
-        self.id = id
-        self.basepath = basepath
-        self.order = order
-        self.src = ""
-        self.annotations = None
-        self.videopath = videopath
-        self.videourl = ""
-        self.title = None
-        self.desc = None
-        self.duration = None
-        self.created = False
-        self.content = None
-        self.decoupage_blacklist = decoupage_blacklist
-        if self.decoupage_blacklist is None:
-            self.decoupage_blacklist = ()
-        self.flatten = flatten
-        
-        
-    
-    def processIri(self):
-        # for just import a file ldt and get the title for every media
-        if 'http' in self.src:
-            url = urllib.urlopen(self.src)
-            doc = xml.dom.minidom.parse(url)
-            doc = Ft.Xml.Domlette.ConvertDocument(doc)             
-            con = xml.xpath.Context.Context(doc, 1, 1, None)
-            #open .iri and get the title
-            res = xml.xpath.Evaluate("/iri/head/meta[@name='title']/@content", context=con)
-            self.title = res[0].value
-            
-        #for import a zip, get title and copy file .iri in the media directory
-        else:
-            path = os.path.join(self.basepath, self.src)
-            doc = xml.dom.minidom.parse(path)
-            
-            con = xml.xpath.Context.Context(doc, 1, 1, None)
-            
-            res = xml.xpath.Evaluate("/iri/head/meta[@name='title']/@content", context=con)
-            self.title = res[0].value
-            
-        res = xml.xpath.Evaluate("/iri/body/ensembles",context=con)
-        ensemblesnode = res[0]
-
-        ensembleids = []
-
-        for node in ensemblesnode.childNodes:
-            if node.nodeType == xml.dom.Node.ELEMENT_NODE and node.tagName == "ensemble":
-                id = node.getAttributeNS(None,u"id")
-                if id not in ensembleids:
-                    ensembleids.append(id)
-
-        if self.annotations is not None:
-            newEnsemble = None
-            for cnode in self.annotations.childNodes:
-                if cnode.nodeType == xml.dom.Node.ELEMENT_NODE and cnode.tagName == "decoupage":
-                    if newEnsemble is None:
-                        newensemble = doc.createElement('ensemble')
-                        ensembleid = self.id+"_"+str(uuid.uuid1())
-                        newensemble.setAttributeNS(None,'id',ensembleid)
-            
-                        newensemble.setAttributeNS(None,'title', self.annotations.getAttribute('title'))
-                        newensemble.setAttributeNS(None,'author', self.annotations.getAttribute('author'))
-                        newensemble.setAttributeNS(None,'date', self.annotations.getAttribute('date'))
-                        newensemble.setAttributeNS(None,'abstract', self.annotations.getAttribute('abstract'))
-                        ensemblesnode.appendChild(newensemble)
-                        ensembleids.append(ensembleid)
-                    newDecoupageNode = cnode.cloneNode(True)
-                    newensemble.appendChild(newDecoupageNode)
-                elif cnode.nodeType == xml.dom.Node.ELEMENT_NODE and cnode.tagName == "ensemble":
-                    ensembleid = cnode.getAttribute(u"id")
-                    cloneNode = cnode.cloneNode(True)
-                    if ensembleid in ensembleids:
-                        ensembleid = self.id+"_"+str(uuid.uuid1())
-                        cloneNode.setAttribute(u"id", ensembleid)
-                    ensembleids.append(ensembleid)
-                    ensemblesnode.appendChild(cloneNode)
-
-        res = xml.xpath.Evaluate("/iri/body/medias/media[@id='video']/video", context=con)
-        if self.flatten:
-            src_video = res[0].getAttribute('src')
-            self.videourl = os.path.basename(src_video)
-            res[0].setAttributeNS(None,'src', self.videourl)
-        self.duration = res[0].getAttributeNS(None, u'dur')
-            
-        f = open(path, "w")
-        try:
-            xml.dom.ext.Print(doc, stream=f)
-        finally:
-            f.close()
-        
-        
-        destPath = os.path.join(os.path.join(os.path.join(settings.MEDIA_ROOT, "media"), "ldt"), self.id);
-        if not os.path.exists(destPath):
-            os.makedirs(destPath)
-        shutil.move(os.path.join(self.basepath, self.src), os.path.join(destPath, os.path.basename(self.src)))
-        self.src = self.id + u"/" + os.path.basename(self.src)
-
-
-
-    def saveContent(self):
-        #if 'http' in self.src:
-        #    url = self.src
-        #else:
-        #    url = self.id + u"/" + os.path.basename(self.src)
-        content, self.created = Content.objects.get_or_create(iri_id=self.id, defaults = {'src':self.videourl, 'iriurl': self.src, 'title':self.title, 'description':self.desc, 'videopath': self.videopath})
-        if not self.created:
-            content.iriurl = self.src
-            content.title = self.title
-            content.description = self.desc
-            content.save()
-
-        content.iriurl = self.src
-        content.videopath = self.videopath.rstrip("/") + "/"
-
-        content.iri = self.id + u"/" + os.path.basename(self.src)
-        content.title = self.title
-        content.description = self.desc
-        content.duration = int(self.duration)
-        content.save()
-
-        self.content = content
-            
-    def process(self):
-        self.processIri()
-        self.saveContent()
-        
-class BaseFileImport(object):
-
-    def __init__(self, filepath, videopath):
-        self.__filepath = filepath
-        self.__tempdir = ""
-        self.__videopath = videopath
-        self.__author = None
-
-        
-    def filepath(): #@NoSelf
-        doc = """Docstring""" #@UnusedVariable
-       
-        def fget(self):
-            return self.__filepath
-           
-        def fset(self, value):
-            self.__filepath = value
-           
-        def fdel(self):
-            del self.__filepath
-           
-        return locals()
-       
-    filepath = property(**filepath())
-    
-    def videopath(): #@NoSelf
-        doc = """Docstring""" #@UnusedVariable
-       
-        def fget(self):
-            return self.__videopath
-           
-        def fset(self, value):
-            self.__videopath = value
-           
-        def fdel(self):
-            del self.__videopath
-           
-        return locals()
-       
-    videopath = property(**videopath())
-
-    def author(): #@NoSelf
-        doc = """Docstring""" #@UnusedVariable
-       
-        def fget(self):
-            return self.__author
-           
-        def fset(self, value):
-            self.__author = value
-           
-        def fdel(self):
-            del self.__author
-           
-        return locals()
-
-    author = property(**author())
-    
-       
-class FileImport(BaseFileImport):
-
-    def __init__(self, filepath, videopath, flatten):
-        BaseFileImport.__init__(self, filepath, videopath)
-        self.__checkExistingMedia = False
-        self.__flatten = flatten
-        
-    def checkExistingMedia(): #@NoSelf
-        doc = """Docstring""" #@UnusedVariable
-       
-        def fget(self):
-            return self.__checkExistingMedia
-           
-        def fset(self, value):
-            self.__checkExistingMedia = value
-           
-        def fdel(self):
-            del self.__checkExistingMedia
-           
-        return locals()
-
-    checkExistingMedia = property(**checkExistingMedia())    
-
-    def flatten(): #@NoSelf
-        doc = """Docstring""" #@UnusedVariable
-       
-        def fget(self):
-            return self.__flatten
-           
-        def fset(self, value):
-            self.__flatten = value
-           
-        def fdel(self):
-            del self.__flatten
-           
-        return locals()
-    flatten = property(**flatten())
-
-    def processLdt(self, ldtpath=None):
-        
-        # list iri
-        # see if there is some comments
-        # inject comment in iri
-        # copy iri in folder
-        # create or update content
-        contents = {}
-        if ldtpath:
-            doc = xml.dom.minidom.parse(ldtpath)
-        else:
-            doc = xml.dom.minidom.parse(self.filepath)
-        
-        con = xml.xpath.Context.Context(doc, 1, 1, None)
-        
-        #get author from file ldt
-        result = xml.xpath.Evaluate("/iri/project", context=con)
-        for pnode in result:
-            author = pnode.getAttributeNS(None,u"user")
-            if author:
-                self.author = unicode(author)
-                break 
-
-        result = xml.xpath.Evaluate("/iri/medias/media", context=con)
-        
-        for i, medianode in  enumerate(result):
-        # get iri file's id from file ldt
-            id = medianode.attributes['id'].value
-            if self.checkExistingMedia:
-                try:
-                    Content.objects.get(iri_id=id)
-                    do_pass = True
-                except ObjectDoesNotExist: #Content.DoesNotExist
-                    do_pass = False
-            else:
-                    do_pass = False
-            if not do_pass:
-                if not (contents.has_key(id)):
-                    # Create instance iriInfo(id, order, titledesc, basepath="", videopath=settings.STREAM_URL)
-                    if ldtpath:
-                        contents[id] = IriInfo(id, i, "", os.path.dirname(ldtpath), flatten=self.flatten)
-                    else: 
-                        contents[id] = IriInfo(id, i, "", flatten=self.flatten)
-                    # Get iri file's url from ldt. This url can be relative path or absolute path.
-                contents[id].src = medianode.attributes['src'].value
-                if medianode.attributes['video'].value !="":
-                    contents[id].videopath = medianode.attributes['video'].value
-                elif self.videopath !="" or self.videopath:
-                    contents[id].videopath = self.videopath
-                else:
-                    contents[id].videopath =settings.STREAM_URL
-                    
-                
-        #get annotation of file ldt
-        result = xml.xpath.Evaluate("/iri/annotations/content", context=con)
-        
-        for contentnode in result:
-            id = contentnode.attributes['id'].value
-            # pocketfilms.utils.log.debug("ID : " + str(id))
-            if contents.has_key(id):
-                if self.author:
-                    contentnode.setAttributeNS(None,"author", unicode(self.author))
-                contents[id].annotations = contentnode
-        
-        #go throught values
-        for iriinfo in contents.values():
-            
-            iriinfo.process()
-            
-            # if yes update
-            # if no create
-            # move iri file to the proper place
-            #return list of iriInfo
-        
-    def processFile(self):
-        if self.filepath.name.endswith(".ldt"):
-            self.processLdt()
-        elif self.filepath.name.endswith(".zip"):
-            self.processZip()
-        else:
-            raise FileImportError("Bad file type")
-
-
-    def processZip(self):
-    # """ extraire .zip, pass to method processLdt"""
-        # create temp directory
-        self.__tempdir = tempfile.mkdtemp()
-        openfiles = []
-        flvfiles = []
-        processedids = []
-        
-        try:
-            zipfile = zipfileext.ZipFileExt(self.filepath)
-            zipfile.unzip_into_dir(self.__tempdir)
-            #load ldt
-            foldersToProcess = [self.__tempdir]
-            while len(foldersToProcess):
-                # pocketfilms.utils.log.debug("folder stack length : "+ str(len(foldersToProcess)))
-                currentFolder = foldersToProcess.pop()
-                for entry in os.listdir(currentFolder):
-                    if entry in settings.ZIP_BLACKLIST:
-                        continue
-                    entryPath = os.path.join(currentFolder, entry)
-                    if(os.path.isdir(entryPath)):
-                        # pocketfilms.utils.log.debug("Push folder : " + entryPath)
-                        foldersToProcess.append(entryPath)
-                    elif fnmatch.fnmatch(entry, "*.ldt"):
-                        # pocketfilms.utils.log.debug("Process file : " + entryPath)
-                        ldtid = self.processLdt(entryPath)
-                        processedids.append(ldtid)
-                    elif fnmatch.fnmatch(entry, "*.flv"):
-                        flvfiles.append(entryPath)
-             
-            if settings.CONTENT_ROOT and os.path.exists(settings.CONTENT_ROOT): 
-                for entry in flvfiles:
-                    shutil.copy(entry, settings.CONTENT_ROOT)
-                    
-        finally:
-            for f in openfiles:
-                f.close()
-            shutil.rmtree(self.__tempdir)
-        # delete directory
-        return processedids
-        
-    # def processFileLdt(self):
-        # processedids = []
-        # ldtid = self.processLdt(self.filepath)
-        # processedids.append(ldtid)
-        # return processedids
+import tempfile
+import os.path
+import shutil
+from django.core.exceptions import ObjectDoesNotExist
+from ldt.utils import zipfileext
+from django.conf import settings
+from models import Content
+import xml.dom.minidom
+import xml.dom.ext #@UnresolvedImport
+import xml.xpath #@UnresolvedImport
+import fnmatch
+import uuid
+import urllib
+
+class FileImportError(Exception):
+    def __init__(self, value):
+        self.value = value
+    def __str__(self):
+        return repr(self.value)
+
+
+def Property(func):
+    return property(**func()) 
+
+class IriInfo(object):
+
+    
+    def __init__(self, id, order, titledesc, basepath="", videopath=settings.STREAM_URL, decoupage_blacklist = settings.DECOUPAGE_BLACKLIST, flatten = True):
+        self.id = id
+        self.basepath = basepath
+        self.order = order
+        self.src = ""
+        self.annotations = None
+        self.videopath = videopath
+        self.videourl = ""
+        self.title = None
+        self.desc = None
+        self.duration = None
+        self.created = False
+        self.content = None
+        self.decoupage_blacklist = decoupage_blacklist
+        if self.decoupage_blacklist is None:
+            self.decoupage_blacklist = ()
+        self.flatten = flatten
+        
+        
+    
+    def processIri(self):
+        # for just import a file ldt and get the title for every media
+        if 'http' in self.src:
+            url = urllib.urlopen(self.src)
+            doc = xml.dom.minidom.parse(url)
+        #for import a zip, get title and copy file .iri in the media directory
+        else:
+            path = os.path.join(self.basepath, self.src)
+            doc = xml.dom.minidom.parse(path)
+        
+        
+        doc = Ft.Xml.Domlette.ConvertDocument(doc) 
+        con = xml.xpath.Context.Context(doc, 1, 1, None)
+        
+        res = xml.xpath.Evaluate("/iri/head/meta[@name='title']/@content", context=con)
+        self.title = res[0].value
+
+            
+        res = xml.xpath.Evaluate("/iri/body/ensembles",context=con)
+        ensemblesnode = res[0]
+
+        ensembleids = []
+
+        for node in ensemblesnode.childNodes:
+            if node.nodeType == xml.dom.Node.ELEMENT_NODE and node.tagName == "ensemble":
+                id = node.getAttributeNS(None,u"id")
+                if id not in ensembleids:
+                    ensembleids.append(id)
+
+        if self.annotations is not None:
+            newEnsemble = None
+            for cnode in self.annotations.childNodes:
+                if cnode.nodeType == xml.dom.Node.ELEMENT_NODE and cnode.tagName == "decoupage":
+                    if newEnsemble is None:
+                        newensemble = doc.createElement('ensemble')
+                        ensembleid = self.id+"_"+str(uuid.uuid1())
+                        newensemble.setAttributeNS(None,'id',ensembleid)
+            
+                        newensemble.setAttributeNS(None,'title', self.annotations.getAttribute('title'))
+                        newensemble.setAttributeNS(None,'author', self.annotations.getAttribute('author'))
+                        newensemble.setAttributeNS(None,'date', self.annotations.getAttribute('date'))
+                        newensemble.setAttributeNS(None,'abstract', self.annotations.getAttribute('abstract'))
+                        ensemblesnode.appendChild(newensemble)
+                        ensembleids.append(ensembleid)
+                    newDecoupageNode = cnode.cloneNode(True)
+                    newensemble.appendChild(newDecoupageNode)
+                elif cnode.nodeType == xml.dom.Node.ELEMENT_NODE and cnode.tagName == "ensemble":
+                    ensembleid = cnode.getAttribute(u"id")
+                    cloneNode = cnode.cloneNode(True)
+                    if ensembleid in ensembleids:
+                        ensembleid = self.id+"_"+str(uuid.uuid1())
+                        cloneNode.setAttribute(u"id", ensembleid)
+                    ensembleids.append(ensembleid)
+                    ensemblesnode.appendChild(cloneNode)
+
+        res = xml.xpath.Evaluate("/iri/body/medias/media[@id='video']/video", context=con)
+        if self.flatten:
+            src_video = res[0].getAttributeNS(None,'src')
+            self.videourl = os.path.basename(src_video)
+            res[0].setAttributeNS(None,'src', self.videourl)
+        self.duration = res[0].getAttributeNS(None, u'dur')
+            
+        f = open(path, "w")
+        try:
+            xml.dom.ext.Print(doc, stream=f)
+        finally:
+            f.close()
+        
+        
+        destPath = os.path.join(os.path.join(os.path.join(settings.MEDIA_ROOT, "media"), "ldt"), self.id);
+        if not os.path.exists(destPath):
+            os.makedirs(destPath)
+        shutil.move(os.path.join(self.basepath, self.src), os.path.join(destPath, os.path.basename(self.src)))
+        self.src = self.id + u"/" + os.path.basename(self.src)
+
+
+
+    def saveContent(self):
+        #if 'http' in self.src:
+        #    url = self.src
+        #else:
+        #    url = self.id + u"/" + os.path.basename(self.src)
+        content, self.created = Content.objects.get_or_create(iri_id=self.id, defaults = {'src':self.videourl, 'iriurl': self.src, 'title':self.title, 'description':self.desc, 'videopath': self.videopath})
+        if not self.created:
+            content.iriurl = self.src
+            content.title = self.title
+            content.description = self.desc
+            content.save()
+
+        content.iriurl = self.src
+        content.videopath = self.videopath.rstrip("/") + "/"
+
+        content.iri = self.id + u"/" + os.path.basename(self.src)
+        content.title = self.title
+        content.description = self.desc
+        content.duration = int(self.duration)
+        content.save()
+
+        self.content = content
+            
+    def process(self):
+        self.processIri()
+        self.saveContent()
+        
+class BaseFileImport(object):
+
+    def __init__(self, filepath, videopath):
+        self.__filepath = filepath
+        self.__tempdir = ""
+        self.__videopath = videopath
+        self.__author = None
+
+        
+    def filepath(): #@NoSelf
+        doc = """Docstring""" #@UnusedVariable
+       
+        def fget(self):
+            return self.__filepath
+           
+        def fset(self, value):
+            self.__filepath = value
+           
+        def fdel(self):
+            del self.__filepath
+           
+        return locals()
+       
+    filepath = property(**filepath())
+    
+    def videopath(): #@NoSelf
+        doc = """Docstring""" #@UnusedVariable
+       
+        def fget(self):
+            return self.__videopath
+           
+        def fset(self, value):
+            self.__videopath = value
+           
+        def fdel(self):
+            del self.__videopath
+           
+        return locals()
+       
+    videopath = property(**videopath())
+
+    def author(): #@NoSelf
+        doc = """Docstring""" #@UnusedVariable
+       
+        def fget(self):
+            return self.__author
+           
+        def fset(self, value):
+            self.__author = value
+           
+        def fdel(self):
+            del self.__author
+           
+        return locals()
+
+    author = property(**author())
+    
+       
+class FileImport(BaseFileImport):
+
+    def __init__(self, filepath, videopath, flatten):
+        BaseFileImport.__init__(self, filepath, videopath)
+        self.__checkExistingMedia = False
+        self.__flatten = flatten
+        
+    def checkExistingMedia(): #@NoSelf
+        doc = """Docstring""" #@UnusedVariable
+       
+        def fget(self):
+            return self.__checkExistingMedia
+           
+        def fset(self, value):
+            self.__checkExistingMedia = value
+           
+        def fdel(self):
+            del self.__checkExistingMedia
+           
+        return locals()
+
+    checkExistingMedia = property(**checkExistingMedia())    
+
+    def flatten(): #@NoSelf
+        doc = """Docstring""" #@UnusedVariable
+       
+        def fget(self):
+            return self.__flatten
+           
+        def fset(self, value):
+            self.__flatten = value
+           
+        def fdel(self):
+            del self.__flatten
+           
+        return locals()
+    flatten = property(**flatten())
+
+    def processLdt(self, ldtpath=None):
+        
+        # list iri
+        # see if there is some comments
+        # inject comment in iri
+        # copy iri in folder
+        # create or update content
+        contents = {}
+        if ldtpath:
+            doc = xml.dom.minidom.parse(ldtpath)
+        else:
+            doc = xml.dom.minidom.parse(self.filepath)
+        
+        con = xml.xpath.Context.Context(doc, 1, 1, None)
+        
+        #get author from file ldt
+        result = xml.xpath.Evaluate("/iri/project", context=con)
+        for pnode in result:
+            author = pnode.getAttributeNS(None,u"user")
+            if author:
+                self.author = unicode(author)
+                break 
+
+        result = xml.xpath.Evaluate("/iri/medias/media", context=con)
+        
+        for i, medianode in  enumerate(result):
+        # get iri file's id from file ldt
+            id = medianode.attributes['id'].value
+            if self.checkExistingMedia:
+                try:
+                    Content.objects.get(iri_id=id)
+                    do_pass = True
+                except ObjectDoesNotExist: #Content.DoesNotExist
+                    do_pass = False
+            else:
+                    do_pass = False
+            if not do_pass:
+                if not (contents.has_key(id)):
+                    # Create instance iriInfo(id, order, titledesc, basepath="", videopath=settings.STREAM_URL)
+                    if ldtpath:
+                        contents[id] = IriInfo(id, i, "", os.path.dirname(ldtpath), flatten=self.flatten)
+                    else: 
+                        contents[id] = IriInfo(id, i, "", flatten=self.flatten)
+                    # Get iri file's url from ldt. This url can be relative path or absolute path.
+                contents[id].src = medianode.attributes['src'].value
+                if medianode.attributes['video'].value !="":
+                    contents[id].videopath = medianode.attributes['video'].value
+                elif self.videopath !="" or self.videopath:
+                    contents[id].videopath = self.videopath
+                else:
+                    contents[id].videopath =settings.STREAM_URL
+                    
+                
+        #get annotation of file ldt
+        result = xml.xpath.Evaluate("/iri/annotations/content", context=con)
+        
+        for contentnode in result:
+            id = contentnode.attributes['id'].value
+            # pocketfilms.utils.log.debug("ID : " + str(id))
+            if contents.has_key(id):
+                if self.author:
+                    contentnode.setAttributeNS(None,"author", unicode(self.author))
+                contents[id].annotations = contentnode
+        
+        #go throught values
+        for iriinfo in contents.values():
+            
+            iriinfo.process()
+            
+            # if yes update
+            # if no create
+            # move iri file to the proper place
+            #return list of iriInfo
+        
+    def processFile(self):
+        if self.filepath.name.endswith(".ldt"):
+            self.processLdt()
+        elif self.filepath.name.endswith(".zip"):
+            self.processZip()
+        else:
+            raise FileImportError("Bad file type")
+
+
+    def processZip(self):
+    # """ extraire .zip, pass to method processLdt"""
+        # create temp directory
+        self.__tempdir = tempfile.mkdtemp()
+        openfiles = []
+        flvfiles = []
+        processedids = []
+        
+        try:
+            zipfile = zipfileext.ZipFileExt(self.filepath)
+            zipfile.unzip_into_dir(self.__tempdir)
+            #load ldt
+            foldersToProcess = [self.__tempdir]
+            while len(foldersToProcess):
+                # pocketfilms.utils.log.debug("folder stack length : "+ str(len(foldersToProcess)))
+                currentFolder = foldersToProcess.pop()
+                for entry in os.listdir(currentFolder):
+                    if entry in settings.ZIP_BLACKLIST:
+                        continue
+                    entryPath = os.path.join(currentFolder, entry)
+                    if(os.path.isdir(entryPath)):
+                        # pocketfilms.utils.log.debug("Push folder : " + entryPath)
+                        foldersToProcess.append(entryPath)
+                    elif fnmatch.fnmatch(entry, "*.ldt"):
+                        # pocketfilms.utils.log.debug("Process file : " + entryPath)
+                        ldtid = self.processLdt(entryPath)
+                        processedids.append(ldtid)
+                    elif fnmatch.fnmatch(entry, "*.flv"):
+                        flvfiles.append(entryPath)
+             
+            if settings.CONTENT_ROOT and os.path.exists(settings.CONTENT_ROOT): 
+                for entry in flvfiles:
+                    shutil.copy(entry, settings.CONTENT_ROOT)
+                    
+        finally:
+            for f in openfiles:
+                f.close()
+            shutil.rmtree(self.__tempdir)
+        # delete directory
+        return processedids
+        
+    # def processFileLdt(self):
+        # processedids = []
+        # ldtid = self.processLdt(self.filepath)
+        # processedids.append(ldtid)
+        # return processedids
--- a/web/ldt/ldt_utils/models.py	Thu Jun 17 11:24:33 2010 +0200
+++ b/web/ldt/ldt_utils/models.py	Thu Jun 17 15:04:36 2010 +0200
@@ -2,8 +2,7 @@
 from django.conf import settings
 from ldt.core.models import Document, Owner
 from django.utils.translation import ugettext_lazy as _
-from utils import create_ldt, copy_ldt, create_empty_iri
-import os
+from utils import create_ldt, copy_ldt, create_empty_iri, update_iri
 import os.path
 import uuid
 import xml
@@ -48,20 +47,28 @@
         writer.deleteDocuments(lucene.Term("iri_id", self.iri_id))
         writer.commit()
         
+    #TODO: better manage the change in .iri name and error scenario (save in temp file + rename
     def save(self):
         # create iri file if needed
+        created = False
         try:
             iri_file_path = self.iri_file_path()
             if not os.path.exists(iri_file_path):
                 dir = os.path.dirname(iri_file_path)
                 if not os.path.exists(dir):
                     os.makedirs(dir)
+                created = True
                 file = open(iri_file_path,"w")
                 create_empty_iri(file, self, "IRI")
+            else:
+                created = False
+                update_iri(iri_file_path, self, "IRI")
+                
         except Exception, e:
-            if os.path.exists(iri_file_path):
-                os.remove(iri_file_path)
-                raise e
+            if created:
+                if os.path.exists(iri_file_path):
+                    os.remove(iri_file_path)
+            raise e
         # update it 
         super(Content, self).save()
     
--- a/web/ldt/ldt_utils/utils.py	Thu Jun 17 11:24:33 2010 +0200
+++ b/web/ldt/ldt_utils/utils.py	Thu Jun 17 15:04:36 2010 +0200
@@ -1,314 +1,354 @@
-import lucene
-from ldt.ldt_utils import STORE
-from ldt.ldt_utils import ANALYZER
-from Ft.Xml import MarkupWriter
-import uuid
-import django.core.urlresolvers
-from django.conf import settings
-import urllib
-import xml.dom
-import xml.dom.minidom
-import xml.dom.ext
-import xml.xpath
-import os
-import os.path
-import datetime
-
-class LdtSearch(object):
-
-    def query(self, field, query):
-        indexSearcher = lucene.IndexSearcher(STORE)
-        queryParser = lucene.QueryParser(lucene.Version.LUCENE_30, field, lucene.FrenchAnalyzer(lucene.Version.LUCENE_30))
-        queryParser.setDefaultOperator(lucene.QueryParser.Operator.AND)
-        queryObj = queryParser.parse(query)
-        hits = indexSearcher.search(queryObj, settings.LDT_MAX_SEARCH_NUMBER)
-	
-        res = []
-        for hit in hits.scoreDocs:
-            doc = indexSearcher.doc(hit.doc)
-            res.append({"iri_id":doc.get("iri_id"),"ensemble_id":doc.get("ensemble_id"),"decoupage_id":doc.get("decoupage_id"), "element_id":doc.get("element_id")})
-        indexSearcher.close()
-        return res
-
-    def queryAll(self, query):		
-        return self.query("all", query)
-
-class LdtUtils(object):
-    
-    def generateLdt(self, contentList, file, title = u"", author=u"IRI Web", web_url=u"", media_url="", startSegment = None, contributions=None):
-
-        writer = MarkupWriter(file, indent = u"yes")
-        writer.startDocument()
-        writer.startElement(u"iri")
-        writer.simpleElement(u"project", attributes={u"id":unicode(str(uuid.uuid1())), u"title":unicode(title) , u"user":author, u"abstract":u""})
-        writer.startElement(u"medias")
-        for content in contentList:
-            videopath = unicode(settings.STREAM_URL)
-            if content.videopath :
-                videopath = unicode(content.videopath)
-            writer.simpleElement(u"media", attributes={u"id":content.iri_id,u"src":content.iri_url(web_url),u"video":videopath,u"pict":u"",u"extra":u""})
-        writer.endElement(u"medias")
-
-        if contributions is None:
-            contributions = []
-        annotations_nodes = {}
-        for contrib in contributions:
-            doc = xml.dom.minidom.parseString(contrib.ldtproject.ldt.encode("utf-8"))
-            con = xml.xpath.Context.Context(doc, 1, 1, None)
-            res = xml.xpath.Evaluate("/iri/annotations/content", context=con)
-            for content in res:
-                contentid = content.getAttribute("id")
-                if annotations_nodes.has_key(contentid):
-                    contentnode = annotations_nodes[contentid]
-                else:
-                    contentnode = {"id":contentid, "ensembles":[]}
-                    annotations_nodes[contentid]=contentnode
-                for ens in content.childNodes:
-                    if ens.nodeType == xml.dom.Node.ELEMENT_NODE and ens.tagName.endswith("ensemble"):
-                        contentnode["ensembles"].append(ens.toprettyxml())
-
-        if len(annotations_nodes) > 0:
-            writer.startElement(u"annotations")
-            for content in contentList:
-                if content.content_base.iri_id in annotations_nodes:
-                    contentnode = annotations_nodes[content.content_base.iri_id]
-                    if contentnode is not None:
-                        if len(contentnode["ensembles"])>0:
-                            writer.startElement(u"content", attributes={"id":contentnode["id"]})
-                            writer.text(u"")
-                            for ens in contentnode["ensembles"]:
-                                writer.xmlFragment(ens.encode("utf-8"))
-                            writer.endElement(u"content")
-                        else:
-                            writer.simpleElement(u"content", attributes={"id":contentnode["id"]})
-            writer.endElement(u"annotations")
-        else:
-            writer.simpleElement(u"annotations")
-
-
-        writer.startElement(u"displays")
-        if len(contentList) > 0:
-            writer.startElement(u"display", attributes={u"id":u"0",u"title":u"generated",u"idsel":contentList[0].iri_id,u"tc":u"0"})
-            for content in contentList:
-                writer.startElement(u"content", attributes={u"id":content.iri_id})
-                filepath = urllib.urlopen(content.iri_url())
-                doc = xml.dom.minidom.parse(filepath)
-                con = xml.xpath.Context.Context(doc, 1, 1, None)
-                res = xml.xpath.Evaluate("/iri/body/ensembles/ensemble/decoupage", context=con)
-                for decoupagenode in res:
-                    decoupage_id = decoupagenode.getAttribute(u"id")
-                    ensemble_id = decoupagenode.parentNode.getAttribute(u"id")
-                    writer.simpleElement(u"decoupage", attributes={u"id":decoupage_id,u"idens":ensemble_id})
-                writer.endElement(u"content")
-            if startSegment is not None:
-                writer.startElement(u"activeSegment")
-                writer.simpleElement(u"id",attributes={u"idctt" : startSegment["idcontent"],u"idens" : startSegment["idgroup"], u"idcut" : startSegment["idcutting"], u"idseg" : startSegment["idsegment"]})
-                writer.endElement(u"activeSegment")
-
-            writer.endElement(u"display")
-        writer.endElement(u"displays")
-        writer.simpleElement(u"edits")
-        writer.endElement(u"iri")
-
-    def generateInit(self, url, method, search=None):
-        
-        import xml.dom
-        import xml.dom.ext
-    
-        impl = xml.dom.getDOMImplementation()
-        doc = impl.createDocument(xml.dom.EMPTY_NAMESPACE, 'iri', None)
-    
-        elementFiles = doc.createElement('files')
-        doc.documentElement.appendChild(elementFiles)
-    
-        elementInit = doc.createElement('init')
-        elementFiles.appendChild(elementInit)
-    
-        elementfile = doc.createElement('file')
-            
-        elementfile.setAttribute('src',settings.WEB_URL + django.core.urlresolvers.reverse(method, args=url))
-        elementfile.setAttribute('display', '1')
-        if(search):
-            elementfile.setAttribute("segsel",settings.WEB_URL + django.core.urlresolvers.reverse(search, args=url))
-    
-    
-        # /*chemin video : tant que le serveur de media n'est pas up, */
-        elementfile.setAttribute('video', settings.STREAM_URL)
-        elementfile.setAttribute('pict', "")
-        elementfile.setAttribute('extra', "")
-    
-        elementInit.appendChild(elementfile);
-    
-        elementRecent = doc.createElement('recent');
-        elementFiles.appendChild(elementRecent);
-    
-    
-        elementLibrary = doc.createElement('library');
-        elementFiles.appendChild(elementLibrary);
-    
-        username = ''
-        id = ''
-        elementUser = doc.createElement('user')
-        elementUser.setAttribute('name', username)
-        elementUser.setAttribute('id', id)
-        doc.documentElement.appendChild(elementUser)
-        
-        return doc
-
-def create_ldt(project, user):
-    
-    contentList=project.contents.all()
-    
-    """create xml"""
-    
-    # create a dom
-    impl = xml.dom.getDOMImplementation()
-    dom = impl.createDocument(xml.dom.EMPTY_NAMESPACE, 'iri', None)
-    #node project
-    elementProject = dom.createElement('project')
-    dom.documentElement.appendChild(elementProject)
-    
-    
-    elementProject.setAttribute('abstract', "")
-    elementProject.setAttribute('title', project.title)
-    elementProject.setAttribute('user', user.username)
-    elementProject.setAttribute('id', project.ldt_id)
-    #node medias
-    elementMedias = dom.createElement('medias')
-    dom.documentElement.appendChild(elementMedias)
-    
-    idsel = None      
-    for content in contentList:
-        if not idsel:
-            idsel = content.iri_id
-        elementMedia = dom.createElement('media')            
-        elementMedia.setAttribute('id', content.iri_id)
-        elementMedia.setAttribute('src', content.iri_url())
-        if content.videopath and content.videopath !="":
-            elementMedia.setAttribute('video', content.videopath)
-        else:
-            elementMedia.setAttribute('video', settings.STREAM_URL)
-        elementMedia.setAttribute('pict', "")
-        elementMedia.setAttribute('extra', "")
-        elementMedias.appendChild(elementMedia)
-    if not idsel:
-        idsel = ""
-
-    #node annotations
-    elementAnnotations = dom.createElement('annotations')
-    dom.documentElement.appendChild(elementAnnotations)
-    #node displays
-    elementDisplays = dom.createElement('displays')
-    elementDisplay = dom.createElement('display')
-    elementDisplay.setAttribute('id', '0')
-    elementDisplay.setAttribute('title', 'Init view')
-    elementDisplay.setAttribute('idsel', idsel)
-    elementDisplay.setAttribute('tc', '0')
-    elementDisplay.setAttribute('zoom', '0')
-    elementDisplay.setAttribute('scroll', '0')
-    elementDisplay.setAttribute('infoBAB', '')
-    #node content
-    for content in contentList:
-        elementContent = dom.createElement('content')
-        elementContent.setAttribute('id', content.iri_id)
-        if not 'http' in content.iriurl:
-        #eg: "iiiielizabethrosse/ENMI08-III_elizabethrosse.iri"
-            url = content.iri_url()
-        else:
-            url =content.iriurl
-        file = urllib.urlopen(url)
-        doc = xml.dom.minidom.parse(file)
-        con = xml.xpath.Context.Context(doc, 1, 1, None)
-        res = xml.xpath.Evaluate("/iri/body/ensembles/ensemble/decoupage", context=con)
-        #node decoupage
-        for decoupagenode in res:
-            decoupage_id = decoupagenode.getAttribute(u"id")
-            ensemble_id = decoupagenode.parentNode.getAttribute(u"id")
-            elementDecoupage = dom.createElement('decoupage')
-            elementDecoupage.setAttribute('idens', ensemble_id)
-            elementDecoupage.setAttribute('id', decoupage_id)
-            elementContent.appendChild(elementDecoupage)     
-        elementDisplay.appendChild(elementContent)   
-    
-    elementDisplays.appendChild(elementDisplay)
-    dom.documentElement.appendChild(elementDisplays)
-    
-    elementEdits = dom.createElement('edits')
-    dom.documentElement.appendChild(elementEdits)
-    # write dom in Project.ldt 
-    project.ldt = dom.documentElement.toprettyxml()
-    #save Project
-    project.save()
-    return project        
-
-
-def copy_ldt(project, new_project, user):
-    new_project.ldt_id = str(uuid.uuid1())
-    new_project.created_by=user.username
-    new_project.changed_by=user.username
-    new_project.state = 1
-    
-    contentList=project.contents.all()
-    
-    """create xml"""
-    
-    # create a dom
-    dom = xml.dom.minidom.parseString(project.ldt.encode("utf-8"))
-    con = xml.xpath.Context.Context(dom, 1, 1, None)
-    res = xml.xpath.Evaluate("iri/project", context=con)
-    for elementProject in res:
-        elementProject.setAttribute('abstract', "")
-        elementProject.setAttribute('title', new_project.title)
-        elementProject.setAttribute('user', user.username)
-        elementProject.setAttribute('id', new_project.ldt_id)
-        
-    new_project.ldt = dom.documentElement.toprettyxml()
-    #save Project
-    new_project.save()
-    return new_project
-
-def create_empty_iri(file, content, username):
-    
-    writer = MarkupWriter(file, indent = u"yes")
-    writer.startDocument()
-    writer.startElement(u"iri")
-    
-    writer.startElement(u"head")
-    
-    writer.simpleElement(u'meta', attributes={u'name':u'id', 'content':unicode(content.iri_id)})
-    writer.simpleElement(u'meta', attributes={u'name':u'title', 'content':unicode(content.title)})
-    writer.simpleElement(u'meta', attributes={u'name':u'author', 'content':unicode(username)})
-    writer.simpleElement(u'meta', attributes={u'name':u'contributor', 'content':unicode(username)})
-    writer.simpleElement(u'meta', attributes={u'name':u'date', 'content':unicode(datetime.date.today().isoformat())})
-    writer.simpleElement(u'meta', attributes={u'name':u'copyright', 'content':u'IRI'})
-    writer.simpleElement(u'meta', attributes={u'name':u'type', 'content':u'video'})    
-    
-    writer.endElement(u"head")
-
-    writer.startElement(u"body")
-    
-    writer.startElement(u"ensembles")
-    writer.endElement(u"ensembles")
-    
-    writer.simpleElement(u'links')
-    writer.startElement(u"medias")
-
-    writer.startElement(u"media", attributes={u'id':u'video'})
-    writer.simpleElement(u'video', attributes={u'src':unicode(content.src),u'id':unicode(uuid.uuid1()),u'dur':unicode(content.duration),u'begin':u'0'})
-    writer.endElement(u"media")
-
-    writer.startElement(u"media", attributes={u'id':u'tool'})
-    writer.simpleElement(u'tool')
-    writer.endElement(u"media")
-
-    writer.endElement(u"medias")
-    
-    #writer.startElement(u'display', attributes={u'id':unicode(uuid.uuid1()), u'title':u"default", u"idsel":unicode(content.iri_id), u"tc":u"0", u"zoom":u"0", u"scroll":u"0", u"infoBAB":u""})
-    writer.simpleElement(u'display')
-    
-    writer.endElement(u"body")
-
-    
-    writer.endElement(u"iri")
-    writer.endDocument()
-    
+import lucene
+from ldt.ldt_utils import STORE
+from ldt.ldt_utils import ANALYZER
+from Ft.Xml import MarkupWriter
+import uuid
+import django.core.urlresolvers
+from django.conf import settings
+import urllib
+import xml.dom
+import xml.dom.minidom
+import xml.dom.ext
+import xml.xpath
+import datetime
+import Ft
+
+class LdtSearch(object):
+
+    def query(self, field, query):
+        indexSearcher = lucene.IndexSearcher(STORE)
+        queryParser = lucene.QueryParser(lucene.Version.LUCENE_30, field, lucene.FrenchAnalyzer(lucene.Version.LUCENE_30))
+        queryParser.setDefaultOperator(lucene.QueryParser.Operator.AND)
+        queryObj = queryParser.parse(query)
+        hits = indexSearcher.search(queryObj, settings.LDT_MAX_SEARCH_NUMBER)
+	
+        res = []
+        for hit in hits.scoreDocs:
+            doc = indexSearcher.doc(hit.doc)
+            res.append({"iri_id":doc.get("iri_id"),"ensemble_id":doc.get("ensemble_id"),"decoupage_id":doc.get("decoupage_id"), "element_id":doc.get("element_id")})
+        indexSearcher.close()
+        return res
+
+    def queryAll(self, query):		
+        return self.query("all", query)
+
+class LdtUtils(object):
+    
+    def generateLdt(self, contentList, file, title = u"", author=u"IRI Web", web_url=u"", media_url="", startSegment = None, contributions=None):
+
+        writer = MarkupWriter(file, indent = u"yes")
+        writer.startDocument()
+        writer.startElement(u"iri")
+        writer.simpleElement(u"project", attributes={u"id":unicode(str(uuid.uuid1())), u"title":unicode(title) , u"user":author, u"abstract":u""})
+        writer.startElement(u"medias")
+        for content in contentList:
+            videopath = unicode(settings.STREAM_URL)
+            if content.videopath :
+                videopath = unicode(content.videopath)
+            writer.simpleElement(u"media", attributes={u"id":content.iri_id,u"src":content.iri_url(web_url),u"video":videopath,u"pict":u"",u"extra":u""})
+        writer.endElement(u"medias")
+
+        if contributions is None:
+            contributions = []
+        annotations_nodes = {}
+        for contrib in contributions:
+            doc = xml.dom.minidom.parseString(contrib.ldtproject.ldt.encode("utf-8"))
+            con = xml.xpath.Context.Context(doc, 1, 1, None)
+            res = xml.xpath.Evaluate("/iri/annotations/content", context=con)
+            for content in res:
+                contentid = content.getAttribute("id")
+                if annotations_nodes.has_key(contentid):
+                    contentnode = annotations_nodes[contentid]
+                else:
+                    contentnode = {"id":contentid, "ensembles":[]}
+                    annotations_nodes[contentid]=contentnode
+                for ens in content.childNodes:
+                    if ens.nodeType == xml.dom.Node.ELEMENT_NODE and ens.tagName.endswith("ensemble"):
+                        contentnode["ensembles"].append(ens.toprettyxml())
+
+        if len(annotations_nodes) > 0:
+            writer.startElement(u"annotations")
+            for content in contentList:
+                if content.content_base.iri_id in annotations_nodes:
+                    contentnode = annotations_nodes[content.content_base.iri_id]
+                    if contentnode is not None:
+                        if len(contentnode["ensembles"])>0:
+                            writer.startElement(u"content", attributes={"id":contentnode["id"]})
+                            writer.text(u"")
+                            for ens in contentnode["ensembles"]:
+                                writer.xmlFragment(ens.encode("utf-8"))
+                            writer.endElement(u"content")
+                        else:
+                            writer.simpleElement(u"content", attributes={"id":contentnode["id"]})
+            writer.endElement(u"annotations")
+        else:
+            writer.simpleElement(u"annotations")
+
+
+        writer.startElement(u"displays")
+        if len(contentList) > 0:
+            writer.startElement(u"display", attributes={u"id":u"0",u"title":u"generated",u"idsel":contentList[0].iri_id,u"tc":u"0"})
+            for content in contentList:
+                writer.startElement(u"content", attributes={u"id":content.iri_id})
+                filepath = urllib.urlopen(content.iri_url())
+                doc = xml.dom.minidom.parse(filepath)
+                con = xml.xpath.Context.Context(doc, 1, 1, None)
+                res = xml.xpath.Evaluate("/iri/body/ensembles/ensemble/decoupage", context=con)
+                for decoupagenode in res:
+                    decoupage_id = decoupagenode.getAttribute(u"id")
+                    ensemble_id = decoupagenode.parentNode.getAttribute(u"id")
+                    writer.simpleElement(u"decoupage", attributes={u"id":decoupage_id,u"idens":ensemble_id})
+                writer.endElement(u"content")
+            if startSegment is not None:
+                writer.startElement(u"activeSegment")
+                writer.simpleElement(u"id",attributes={u"idctt" : startSegment["idcontent"],u"idens" : startSegment["idgroup"], u"idcut" : startSegment["idcutting"], u"idseg" : startSegment["idsegment"]})
+                writer.endElement(u"activeSegment")
+
+            writer.endElement(u"display")
+        writer.endElement(u"displays")
+        writer.simpleElement(u"edits")
+        writer.endElement(u"iri")
+
+    def generateInit(self, url, method, search=None):
+        
+        import xml.dom
+        import xml.dom.ext
+    
+        impl = xml.dom.getDOMImplementation()
+        doc = impl.createDocument(xml.dom.EMPTY_NAMESPACE, 'iri', None)
+    
+        elementFiles = doc.createElement('files')
+        doc.documentElement.appendChild(elementFiles)
+    
+        elementInit = doc.createElement('init')
+        elementFiles.appendChild(elementInit)
+    
+        elementfile = doc.createElement('file')
+            
+        elementfile.setAttribute('src',settings.WEB_URL + django.core.urlresolvers.reverse(method, args=url))
+        elementfile.setAttribute('display', '1')
+        if(search):
+            elementfile.setAttribute("segsel",settings.WEB_URL + django.core.urlresolvers.reverse(search, args=url))
+    
+    
+        # /*chemin video : tant que le serveur de media n'est pas up, */
+        elementfile.setAttribute('video', settings.STREAM_URL)
+        elementfile.setAttribute('pict', "")
+        elementfile.setAttribute('extra', "")
+    
+        elementInit.appendChild(elementfile);
+    
+        elementRecent = doc.createElement('recent');
+        elementFiles.appendChild(elementRecent);
+    
+    
+        elementLibrary = doc.createElement('library');
+        elementFiles.appendChild(elementLibrary);
+    
+        username = ''
+        id = ''
+        elementUser = doc.createElement('user')
+        elementUser.setAttribute('name', username)
+        elementUser.setAttribute('id', id)
+        doc.documentElement.appendChild(elementUser)
+        
+        return doc
+
+def create_ldt(project, user):
+    
+    contentList=project.contents.all()
+    
+    """create xml"""
+    
+    # create a dom
+    impl = xml.dom.getDOMImplementation()
+    dom = impl.createDocument(xml.dom.EMPTY_NAMESPACE, 'iri', None)
+    #node project
+    elementProject = dom.createElement('project')
+    dom.documentElement.appendChild(elementProject)
+    
+    
+    elementProject.setAttribute('abstract', "")
+    elementProject.setAttribute('title', project.title)
+    elementProject.setAttribute('user', user.username)
+    elementProject.setAttribute('id', project.ldt_id)
+    #node medias
+    elementMedias = dom.createElement('medias')
+    dom.documentElement.appendChild(elementMedias)
+    
+    idsel = None      
+    for content in contentList:
+        if not idsel:
+            idsel = content.iri_id
+        elementMedia = dom.createElement('media')            
+        elementMedia.setAttribute('id', content.iri_id)
+        elementMedia.setAttribute('src', content.iri_url())
+        if content.videopath and content.videopath !="":
+            elementMedia.setAttribute('video', content.videopath)
+        else:
+            elementMedia.setAttribute('video', settings.STREAM_URL)
+        elementMedia.setAttribute('pict', "")
+        elementMedia.setAttribute('extra', "")
+        elementMedias.appendChild(elementMedia)
+    if not idsel:
+        idsel = ""
+
+    #node annotations
+    elementAnnotations = dom.createElement('annotations')
+    dom.documentElement.appendChild(elementAnnotations)
+    #node displays
+    elementDisplays = dom.createElement('displays')
+    elementDisplay = dom.createElement('display')
+    elementDisplay.setAttribute('id', '0')
+    elementDisplay.setAttribute('title', 'Init view')
+    elementDisplay.setAttribute('idsel', idsel)
+    elementDisplay.setAttribute('tc', '0')
+    elementDisplay.setAttribute('zoom', '0')
+    elementDisplay.setAttribute('scroll', '0')
+    elementDisplay.setAttribute('infoBAB', '')
+    #node content
+    for content in contentList:
+        elementContent = dom.createElement('content')
+        elementContent.setAttribute('id', content.iri_id)
+        if not 'http' in content.iriurl:
+        #eg: "iiiielizabethrosse/ENMI08-III_elizabethrosse.iri"
+            url = content.iri_url()
+        else:
+            url =content.iriurl
+        file = urllib.urlopen(url)
+        doc = xml.dom.minidom.parse(file)
+        con = xml.xpath.Context.Context(doc, 1, 1, None)
+        res = xml.xpath.Evaluate("/iri/body/ensembles/ensemble/decoupage", context=con)
+        #node decoupage
+        for decoupagenode in res:
+            decoupage_id = decoupagenode.getAttribute(u"id")
+            ensemble_id = decoupagenode.parentNode.getAttribute(u"id")
+            elementDecoupage = dom.createElement('decoupage')
+            elementDecoupage.setAttribute('idens', ensemble_id)
+            elementDecoupage.setAttribute('id', decoupage_id)
+            elementContent.appendChild(elementDecoupage)     
+        elementDisplay.appendChild(elementContent)   
+    
+    elementDisplays.appendChild(elementDisplay)
+    dom.documentElement.appendChild(elementDisplays)
+    
+    elementEdits = dom.createElement('edits')
+    dom.documentElement.appendChild(elementEdits)
+    # write dom in Project.ldt 
+    project.ldt = dom.documentElement.toprettyxml()
+    #save Project
+    project.save()
+    return project        
+
+
+def copy_ldt(project, new_project, user):
+    new_project.ldt_id = str(uuid.uuid1())
+    new_project.created_by=user.username
+    new_project.changed_by=user.username
+    new_project.state = 1
+    
+    contentList=project.contents.all()
+    
+    """create xml"""
+    
+    # create a dom
+    dom = xml.dom.minidom.parseString(project.ldt.encode("utf-8"))
+    con = xml.xpath.Context.Context(dom, 1, 1, None)
+    res = xml.xpath.Evaluate("iri/project", context=con)
+    for elementProject in res:
+        elementProject.setAttribute('abstract', "")
+        elementProject.setAttribute('title', new_project.title)
+        elementProject.setAttribute('user', user.username)
+        elementProject.setAttribute('id', new_project.ldt_id)
+        
+    new_project.ldt = dom.documentElement.toprettyxml()
+    #save Project
+    new_project.save()
+    return new_project
+
+def create_empty_iri(file, content, username):
+    
+    writer = MarkupWriter(file, indent = u"yes")
+    writer.startDocument()
+    writer.startElement(u"iri")
+    
+    writer.startElement(u"head")
+    
+    writer.simpleElement(u'meta', attributes={u'name':u'id', 'content':unicode(content.iri_id)})
+    writer.simpleElement(u'meta', attributes={u'name':u'title', 'content':unicode(content.title)})
+    writer.simpleElement(u'meta', attributes={u'name':u'abstract', 'content':unicode(content.description)})
+    writer.simpleElement(u'meta', attributes={u'name':u'author', 'content':unicode(username)})
+    writer.simpleElement(u'meta', attributes={u'name':u'contributor', 'content':unicode(username)})
+    writer.simpleElement(u'meta', attributes={u'name':u'date', 'content':unicode(datetime.date.today().isoformat())})
+    writer.simpleElement(u'meta', attributes={u'name':u'copyright', 'content':u'IRI'})
+    writer.simpleElement(u'meta', attributes={u'name':u'type', 'content':u'video'})    
+    
+    writer.endElement(u"head")
+
+    writer.startElement(u"body")
+    
+    writer.startElement(u"ensembles")
+    writer.endElement(u"ensembles")
+    
+    writer.simpleElement(u'links')
+    writer.startElement(u"medias")
+
+    writer.startElement(u"media", attributes={u'id':u'video'})
+    writer.simpleElement(u'video', attributes={u'src':unicode(content.src),u'id':unicode(content.iri_id),u'dur':unicode(content.duration),u'begin':u'0'})
+    writer.endElement(u"media")
+
+    writer.startElement(u"media", attributes={u'id':u'tool'})
+    writer.simpleElement(u'tool')
+    writer.endElement(u"media")
+
+    writer.endElement(u"medias")
+    
+    #writer.startElement(u'display', attributes={u'id':unicode(uuid.uuid1()), u'title':u"default", u"idsel":unicode(content.iri_id), u"tc":u"0", u"zoom":u"0", u"scroll":u"0", u"infoBAB":u""})
+    writer.simpleElement(u'display')
+    
+    writer.endElement(u"body")
+
+    
+    writer.endElement(u"iri")
+    writer.endDocument()
+    
+def update_iri(filepath, content, username):
+    
+    # open xml
+    doc = xml.dom.minidom.parse(filepath)
+    doc = Ft.Xml.Domlette.ConvertDocument(doc) 
+    
+    con = xml.xpath.Context.Context(doc, 1, 1, None)
+    res = xml.xpath.Evaluate("/iri/heqd/meta", context=con)
+    # update meta
+    
+    for meta_node in res:
+        meta_name = meta_node.getAttributeNS(None,"name")
+        content_attr = None
+        if meta_name == u'id':
+            content_attr = unicode(content.iri_id)
+        elif meta_name == u'title':
+            content_attr = unicode(content.title)
+        elif meta_name == u'abstract':
+            content_attr = unicode(content.description)
+        elif meta_name == u'contributor':
+            content_attr = unicode(username)
+        elif meta_name == u"date":
+            content_attr = unicode(datetime.date.today().isoformat())
+        if content_attr is not None:
+            meta_node.setAttributeNS(None, u"content", content_attr)
+
+    res = xml.xpath.Evaluate("/iri/body/medias/media[@id='video']/video", context=con)
+    
+    if len(res) > 0:
+        video_node = res[0]
+        video_node.setAttributeNS(None, u'src', unicode(content.src))
+        video_node.setAttributeNS(None, u'dur', unicode(content.duration))
+        video_node.setAttributeNS(None, u'id', unicode(content.iri_id))
+    # update video
+    
+    f = open(filepath, "w")
+    try:
+        xml.dom.ext.Print(doc, stream=f)
+    finally:
+        f.close()