web/ldt/ldt_utils/projectserializer.py
changeset 10 84e31387a741
parent 8 d0a1c6d22b0e
child 26 d5b6908e4da5
--- a/web/ldt/ldt_utils/projectserializer.py	Thu Jun 10 04:16:26 2010 +0200
+++ b/web/ldt/ldt_utils/projectserializer.py	Thu Jun 10 11:17:09 2010 +0200
@@ -2,6 +2,9 @@
 import Ft.Xml.Domlette
 import xml.xpath
 from datetime import datetime
+from ldt.ldt_utils.models import Content, Project
+
+DATE_FORMATS = ["%d/%m/%Y","%Y-%m-%d"]
 
 """
 Serialize a project object to a cinelab compatible array
@@ -21,11 +24,11 @@
     
     def __parse_ensemble(self, ensemble_node, content):
         
-        ensemble_id = ensemble_node.getAttributeNS("id",None)
-        ensemble_author = ensemble_node.getAttributeNS("author",None)
-        ensemble_title = ensemble_node.getAttributeNS("title",None)
-        ensemble_description = ensemble_node.getAttributeNS("abstract",None)
-        ensemble_created = datetime.utcnow().iso_format()
+        ensemble_id = ensemble_node.getAttributeNS(None,u"id")
+        ensemble_author = ensemble_node.getAttributeNS(None,u"author")
+        ensemble_title = ensemble_node.getAttributeNS(None,u"title")
+        ensemble_description = ensemble_node.getAttributeNS(None,u"abstract")
+        ensemble_created = datetime.utcnow().isoformat()
         ensemble_modified = ensemble_created 
         
         list_items = []
@@ -50,16 +53,22 @@
             if decoupage_node.nodeType != xml.dom.Node.ELEMENT_NODE or decoupage_node.tagName != "decoupage" :
                 continue
             
-            decoupage_id = decoupage_node.getAttributeNS("id",None)
-            decoupage_creator = decoupage_node.getAttributeNS("author",None)
+            decoupage_id = decoupage_node.getAttributeNS(None, u"id")
+            decoupage_creator = decoupage_node.getAttributeNS(None,u"author")
             if not decoupage_creator:
                 decoupage_creator = "IRI"
             decoupage_contributor = decoupage_creator
-            date_str = decoupage_node.getAttributeNS("date",None)
+            date_str = decoupage_node.getAttributeNS(None,u"date")
+            decoupage_created = None
             if date_str :
-                decoupage_created = datetime.strptime(date_str,"%d/%m/%Y").iso_format()
-            else :
-                decoupage_created = datetime.utcnow().iso_format()
+                for date_format in DATE_FORMATS:
+                    try:
+                        decoupage_created = datetime.strptime(date_str,date_format).isoformat()
+                        break
+                    except Exception:
+                        decoupage_created = None
+            if decoupage_created is None:
+                decoupage_created = datetime.utcnow().isoformat()
             decoupage_modified = decoupage_created
             
             decoupage_title = ""
@@ -89,11 +98,11 @@
             res = xml.xpath.Evaluate("elements/element", decoupage_node)
             for element_node in res:
                 
-                element_id = element_node.getAttributeNS("id",None)
-                element_begin = element_node.getAttributeNS("begin",None)
-                element_duration = element_node.getAttributeNS("dur",None)
+                element_id = element_node.getAttributeNS(None,u"id")
+                element_begin = element_node.getAttributeNS(None,u"begin")
+                element_duration = element_node.getAttributeNS(None,u"dur")
                 element_media = content.iri_id
-                element_color = element_node.getAttributeNS("color",None)
+                element_color = element_node.getAttributeNS(None,u"color")
                 
                 element_title = ""
                 for txtRes in xml.xpath.Evaluate("title/text()", element_node): 
@@ -107,15 +116,19 @@
                 element_audio_href = ""
                 res = xml.xpath.Evaluate("audio", element_node)
                 if len(res) > 0:
-                    element_audio_src = res[0].getAttributeNS(None, "source")
-                    element_audio_href = res[0].value
+                    element_audio_src = res[0].getAttributeNS(None, u"source")
+                    ltext = []
+                    for n in res[0].childNodes:
+                        if n.nodeType in (dom.Node.TEXT_NODE, dom.Node.CDATA_SECTION_NODE):
+                            ltext.append(n.data)
+                    element_audio_href = ''.join(ltext)
                 
                 
                 element_tags = []
                 
-                tags = element_node.getAttributeNS("tags",None)
+                tags = element_node.getAttributeNS(None,u"tags")
                 
-                tags_list = map(lambda s:s.trim(),tags.split(","))
+                tags_list = map(lambda s:s.strip(),tags.split(","))
 
                 #tags                                
                 if tags is None or len(tags) == 0:
@@ -130,7 +143,7 @@
                     for tagnode in restagnode:
                         tags_list.append(tagnode.data)
                 
-                tag_date = datetime.utcnow().iso_format()
+                tag_date = datetime.utcnow().isoformat()
                 for tag_id in tags_list:
                     if tag_id not in self.tags:
                         new_tag = {
@@ -177,20 +190,20 @@
     
     def __parse_ldt(self):
         
-        doc = xml.dom.minidom.parseString(self.project.ldt)
+        doc = xml.dom.minidom.parseString(self.project.ldt.encode("utf-8"))
         self.ldt_doc = Ft.Xml.Domlette.ConvertDocument(doc)
         con = xml.xpath.Context.Context(doc, 1, 1, None)
         
         res = xml.xpath.Evaluate("/iri/medias/media", context=con)
         for mediaNode in res:
-            iri_id = mediaNode.getAttributeNS(None,"id")
+            iri_id = mediaNode.getAttributeNS(None,u"id")
             content = Content.objects.get(iri_id=iri_id)
-            self.__parser_content(content)
+            self.__parse_content(content)
             
         res = xml.xpath.Evaluate("/iri/annotations/content",context=con)               
         
         for content_node in res:
-            content_id = content_node.getAttributeNS(None, "id")
+            content_id = content_node.getAttributeNS(None, u"id")
             content = Content.objects.get(iri_id=content_id)
             for ensemble_node in content_node.childNodes:
                 if ensemble_node.nodeType != xml.dom.Node.ELEMENT_NODE or ensemble_node.tagName != "ensemble" :
@@ -239,9 +252,9 @@
              "id" : content.iri_id,
              "url" : content.videopath.rstrip('/') + "/" + content.src,
              "dc:creator" : author,
-             "dc:created" : content.creation_date.iso_format(),
+             "dc:created" : content.creation_date.isoformat(),
              "dc:contributor" : contributor,
-             "dc:modified" : content.update_date.iso_format(),
+             "dc:modified" : content.update_date.isoformat(),
              "dc:creator.contents" : content_author,
              "dc:created.contents" : content_date,
              "dc:title" : content.title,
@@ -263,7 +276,6 @@
         res = {}
         
         self.__parse_ldt()
-        self.__parse_contents()
         
         project_main_media = ""
         if len(self.medias) > 0:
@@ -271,8 +283,8 @@
         
         res['metas'] = {
              'id': self.project.ldt_id,
-             'dc:created':self.project.creation_date.iso_format(),
-             'dc:modified':self.project.modification_date.iso_format(),
+             'dc:created':self.project.creation_date.isoformat(),
+             'dc:modified':self.project.modification_date.isoformat(),
              'dc:contributor':self.project.changed_by,
              'dc:creator':self.project.created_by,
              'dc:title':self.project.title,