add checking of the display element when generating ldt for segment
authorymh <ymh.work@gmail.com>
Thu, 19 May 2011 15:55:54 +0200
changeset 116 1f64d39734ed
parent 115 d2cbc4c647dc
child 117 c26bfde0c5d3
add checking of the display element when generating ldt for segment
src/ldt/ldt/ldt_utils/views.py
--- a/src/ldt/ldt/ldt_utils/views.py	Thu May 19 15:07:14 2011 +0200
+++ b/src/ldt/ldt/ldt_utils/views.py	Thu May 19 15:55:54 2011 +0200
@@ -4,16 +4,16 @@
 from django.db.models import Q
 from django.forms.models import model_to_dict
 from django.forms.util import ErrorList
-from django.http import (HttpResponse, HttpResponseRedirect, 
+from django.http import (HttpResponse, HttpResponseRedirect,
     HttpResponseForbidden, HttpResponseServerError)
-from django.shortcuts import (render_to_response, get_object_or_404, 
+from django.shortcuts import (render_to_response, get_object_or_404,
     get_list_or_404)
 from django.template import RequestContext
 from django.template.loader import render_to_string
 from django.utils import simplejson
 from django.utils.html import escape
 from django.utils.translation import ugettext as _, ungettext
-from forms import (LdtAddForm, SearchForm, AddProjectForm, CopyProjectForm, 
+from forms import (LdtAddForm, SearchForm, AddProjectForm, CopyProjectForm,
     ContentForm, MediaForm)
 from ldt.core.models import Owner
 from ldt.ldt_utils.models import Content
@@ -33,6 +33,7 @@
 import urllib2
 
 
+
 @login_required
 def workspace(request):
 
@@ -321,22 +322,33 @@
     if project_id and project_id != "_" :
         project = Project.objects.get(ldt_id=project_id) #@UndefinedVariable
         ldtdoc = lxml.etree.fromstring(project.ldt.encode("utf-8"))
-#        display_nodes = ldtdoc.xpath("/iri/annotations/content")
-#TODO check that segment is in didplay
-#  <displays>
-#    <display id="0" title="generated" idsel="traces_2ac93c836fc4a00b36a77a356182210f" tc="0">
-#      <content id="traces_2ac93c836fc4a00b36a77a356182210f">
-#        <decoupage id="dp_1" idens="ensldt_2d8ab9b5975ef7721077034bb987cd43"/>
-#        <decoupage id="dp_2" idens="ensldt_2d8ab9b5975ef7721077034bb987cd43"/>
-#      </content>
-#      <activeSegment>
-#        <id idctt="traces_2ac93c836fc4a00b36a77a356182210f" idens="ensldt_2d8ab9b5975ef7721077034bb987cd43" idcut="dp_1" idseg="dp_1_sp_105"/>
-#      </activeSegment>
-#    </display>
-#  </displays>
-
-
-        resp.write(project.ldt)
+        displays_node = ldtdoc.find("displays")
+        if not displays_node:
+            displays_node = lxml.etree.SubElement(ldtdoc,u"displays")        
+        res = displays_node.xpath("display")
+        if len(res) > 0:
+            display_node = res[0]
+        else:
+            display_node  = lxml.etree.SubElement(displays_node, u"display", attrib={u'id':u'0', u'title': u'generated', u'idsel':unicode(content_id), u'tc':u'0'})
+        
+        res = display_node.xpath("content[@id='%s']/decoupage[(@id='%s') and (@idens='%s')]" % (content_id, cutting_id, ensemble_id))
+        if len(res) == 0:
+            #create node
+            res = display_node.xpath("content[@id='%s']" %(content_id))
+            if len(res) == 0:
+                content_node = lxml.etree.SubElement(display_node,u"content")
+            else:
+                content_node =  res[0]
+            lxml.etree.SubElement(content_node, u"decoupage", attrib={u'id':unicode(cutting_id), u'idens':unicode(ensemble_id)})
+        active_segment_node = displays_node.find(u"activeSegment")
+        if not active_segment_node:
+            active_segment_node = lxml.etree.SubElement(displays_node, u"activeSegment")
+        id_node = active_segment_node.find(u"id")
+        if id_node:
+            active_segment_node.remove(id_node)
+        lxml.etree.SubElement(active_segment_node, u"id", attrib={u"idctt":unicode(content_id), u"idens": unicode(ensemble_id), "idcut":unicode(cutting_id), u"idseg":unicode(segment_id)})
+                                    
+        resp.write(lxml.etree.tostring(ldtdoc, xml_declaration=True, encoding='utf-8', pretty_print=True))
     else:
         # generate ldt from 
         ldtgen = LdtUtils()