Update api push project with metadatacomposer extra datas. Update project's json serialization with metadatacomposer extra datas. (And remove a lot of has_key because of future python 3)
authorcavaliet
Tue, 28 May 2013 12:38:17 +0200
changeset 1198 62eee5e70b6b
parent 1197 a731809df0fd
child 1199 b4559d14b9ac
Update api push project with metadatacomposer extra datas. Update project's json serialization with metadatacomposer extra datas. (And remove a lot of has_key because of future python 3)
src/ldt/ldt/api/ldt/serializers/cinelabserializer.py
src/ldt/ldt/ldt_utils/projectserializer.py
--- a/src/ldt/ldt/api/ldt/serializers/cinelabserializer.py	Tue May 28 10:45:50 2013 +0200
+++ b/src/ldt/ldt/api/ldt/serializers/cinelabserializer.py	Tue May 28 12:38:17 2013 +0200
@@ -9,7 +9,7 @@
 from tastypie.serializers import Serializer
 import lxml.etree
 import math
-     
+
 
 class CinelabSerializer(Serializer):
     # Thanks to the this serializer, the api will be able to serialize and deserialize a project in cinelab json format
@@ -137,7 +137,7 @@
             contentNode.set('id', iri_id)
             # we search the cinelab lists if they represent a content's list of annotation-type
             for l in cinelab["lists"]:
-                if l["meta"].has_key("id-ref"):
+                if "id-ref" in l["meta"]:
                     if l["meta"]["id-ref"]==iri_id:
                         # We build the ensemble node
                         ensembleNode = lxml.etree.SubElement(contentNode, 'ensemble')
@@ -173,8 +173,8 @@
                                             elementNode.set('date', a["meta"]["dc:created"])
                                             elementNode.set('color', a["color"])
                                             img_src = ""
-                                            if a["content"].has_key("img"):
-                                                if a["content"]["img"].has_key("src"):
+                                            if "img" in a["content"]:
+                                                if "src" in a["content"]["img"]:
                                                     img_src = a["content"]["img"]["src"]
                                             elementNode.set('src', img_src)
                                             titleElm = lxml.etree.SubElement(elementNode, 'title')
@@ -184,14 +184,14 @@
                                             # Audio node, if the dict has the audio key
                                             audioElm = lxml.etree.SubElement(elementNode, 'audio')
                                             audioElm.set('source', "")
-                                            if a["content"].has_key("audio"):
+                                            if "audio" in a["content"]:
                                                 audioElm.set('source', a["content"]["audio"]["src"])
                                                 audioElm.text = a["content"]["audio"]["href"]
                                             # The tags dict has been set before, we just get the labels
                                             tagsNode = lxml.etree.SubElement(elementNode, 'tags')
                                             if a["tags"]:
                                                 for t in a["tags"]:
-                                                    if tag_dict.has_key(t["id-ref"]):
+                                                    if t["id-ref"] in tag_dict:
                                                         tagNode = lxml.etree.SubElement(tagsNode, 'tag')
                                                         tagNode.text = tag_dict[t["id-ref"]]
                                             # Last element's node
@@ -213,12 +213,16 @@
                                                     embedcodeNode.text = lxml.etree.CDATA(a["content"]["embedcode"])
                                                 # Text bonus
                                                 elif a["content"]["mimetype"]=="application/x-ldt-text":
+                                                    typeNode = lxml.etree.SubElement(metaNode, 'type')
+                                                    typeNode.text = "text"
                                                     markupNode = lxml.etree.SubElement(metaNode, 'markup')
                                                     markupNode.text = a["content"]["markup"]
                                                     textNode = lxml.etree.SubElement(metaNode, 'text')
                                                     textNode.text = lxml.etree.CDATA(a["content"]["text"])
                                                 # Links bonus
                                                 elif a["content"]["mimetype"]=="application/x-ldt-links":
+                                                    typeNode = lxml.etree.SubElement(metaNode, 'type')
+                                                    typeNode.text = "links"
                                                     linksNode = lxml.etree.SubElement(metaNode, 'links')
                                                     for link in a["content"]["links"]:
                                                         linkNode = lxml.etree.SubElement(linksNode, 'link')
@@ -228,12 +232,13 @@
                                                         titleNode.text = link["title"]
                                                 # Image slideshow bonus
                                                 elif a["content"]["mimetype"]=="application/x-ldt-slideshow":
-                                                    slideshowNode = lxml.etree.SubElement(metaNode, 'slideshow')
-                                                    durationNode = lxml.etree.SubElement(slideshowNode, 'slideduration')
+                                                    typeNode = lxml.etree.SubElement(metaNode, 'type')
+                                                    typeNode.text = "slideshow"
+                                                    durationNode = lxml.etree.SubElement(metaNode, 'slideduration')
                                                     durationNode.text = str(a["content"]["slideduration"])
-                                                    autostartNode = lxml.etree.SubElement(slideshowNode, 'autostart')
+                                                    autostartNode = lxml.etree.SubElement(metaNode, 'autostart')
                                                     autostartNode.text = str(a["content"]["autostart"])
-                                                    imagesNode = lxml.etree.SubElement(slideshowNode, 'images')
+                                                    imagesNode = lxml.etree.SubElement(metaNode, 'images')
                                                     for img in a["content"]["images"]:
                                                         imageNode = lxml.etree.SubElement(imagesNode, 'image')
                                                         urlNode = lxml.etree.SubElement(imageNode, 'url')
@@ -249,7 +254,7 @@
         id_sel = None
         i = 1
         for v in cinelab["views"]:
-            if "stat" not in v["id"] and v.has_key("annotation_types"):
+            if "stat" not in v["id"] and "annotation_types" in v:
                 displayNode = lxml.etree.SubElement(displaysNode, 'display')
                 displayNode.set('id', v["id"])
                 displayNode.set('title', "View " + str(i))
@@ -282,7 +287,7 @@
         editsNode = lxml.etree.SubElement(iri, 'edits')
         i = 0
         for l in cinelab["lists"]:
-            if l["meta"].has_key("listtype"):
+            if "listtype" in l["meta"]:
                 if l["meta"]["listtype"]=="mashup":
                     editingNode = lxml.etree.SubElement(editsNode, 'editing')
                     editingNode.set('id', str(i))
@@ -350,103 +355,132 @@
         error_string = ""
         do_break = False
         # Meta node
-        if not cinelab_json.has_key("meta"):
+        if not "meta" in cinelab_json:
             error_string += " The cinelab json needs a 'meta' node."
         else:
             mt = cinelab_json["meta"]
-            if not mt.has_key("dc:modified") or not mt.has_key("dc:title") or not mt.has_key("dc:description") or not mt.has_key("dc:creator") or not mt.has_key("dc:created") or not mt.has_key("dc:contributor"):
+            if not "dc:modified" in mt or not "dc:title" in mt or not "dc:description" in mt or not "dc:creator" in mt or not "dc:created" in mt or not "dc:contributor" in mt:
                 error_string += " Meta node must have 'dc:modified', 'dc:title', 'dc:description', 'dc:creator', 'dc:created' and 'dc:contributor' field."
         # Medias node
-        if not cinelab_json.has_key("medias"):
+        if not "medias" in cinelab_json:
             error_string += " The cinelab json needs a 'medias' node."
         else:
             for m in cinelab_json["medias"]:
-                if not m.has_key("id"):
+                if not "id" in m:
                     error_string += " Each media must have an 'id' field."
                     break
         # Lists node
-        if not cinelab_json.has_key("lists"):
+        if not "lists" in cinelab_json:
             error_string += " The cinelab json needs a 'lists' node."
         else:
             for l in cinelab_json["lists"]:
-                if not l.has_key("id"):
+                if not "id" in l:
                     error_string += " Each list must have an 'id' field."
                     do_break = True
-                if not l.has_key("items"):
+                if not "items" in l:
                     error_string += " Each list must have an 'items' field."
                     do_break = True
-                if not l.has_key("meta"):
+                if not "meta" in l:
                     error_string += " Each list must have a 'meta' field."
                     do_break = True
                 else:
                     # 2 types of lists : mashup (list of annotation ids) or list of annotation-types for one media
-                    if not l["meta"].has_key("listtype"):
-                        if not l["meta"].has_key("dc:creator") or not l["meta"].has_key("id-ref"):
+                    if not "listtype" in l["meta"]:
+                        if not "dc:creator" in l["meta"] or not "id-ref" in l["meta"]:
                             error_string += " Each annotation-types list must have 'meta/dc:creator' and 'meta/id-ref' fields."
                             do_break = True
-                    if not l["meta"].has_key("dc:title") or not l["meta"].has_key("dc:description"):
+                    if not "dc:title" in l["meta"] or not "dc:description" in l["meta"]:
                         error_string += " Each list must have 'meta/dc:title' and 'meta/dc:description' fields."
                         do_break = True
                 if do_break:
                     break
         # Annotation-types node
-        if not cinelab_json.has_key("annotation-types"):
+        if not "annotation-types" in cinelab_json:
             error_string += " The cinelab json needs a 'annotation-types' node."
         else:
             for at in cinelab_json["annotation-types"]:
-                if not at.has_key("id") or not at.has_key("dc:title") or not at.has_key("dc:creator") or not at.has_key("dc:description"):
+                if not "id" in at or not "dc:title" in at or not "dc:creator" in at or not "dc:description" in at:
                     error_string += " Each annotation-type must have 'id', 'dc:title', 'dc:creator' and 'dc:description' fields."
                     break
         # Annotations node
-        if not cinelab_json.has_key("annotations"):
+        if not "annotations" in cinelab_json:
             error_string += " The cinelab json needs a 'annotations' node."
         else:
             do_break = False
             for a in cinelab_json["annotations"]:
-                if not a.has_key("id") or not a.has_key("begin") or not a.has_key("end") or not a.has_key("tags") or not a.has_key("color") or not a.has_key("media"):
+                if not "id" in a or not "begin" in a or not "end" in a or not "tags" in a or not "color" in a or not "media" in a:
                     error_string += " Each annotation must have 'id', 'begin', 'end', 'tags' and 'media' fields."
                     do_break = True
-                if not a.has_key("meta"):
+                if not "meta" in a:
                     error_string += " Each annotation must have 'meta' field."
                     do_break = True
                 else:
-                    if not a["meta"].has_key("id-ref") or not a["meta"].has_key("dc:created") or not a["meta"].has_key("dc:creator"):
+                    if not "id-ref" in a["meta"] or not "dc:created" in a["meta"] or not "dc:creator" in a["meta"]:
                         error_string += " Each annotation must have 'meta/id-ref', 'meta/dc:created' and 'meta/dc:creator' fields."
                         do_break = True
-                if not a.has_key("content"):
+                if not "content" in a:
                     error_string += " Each annotation must have 'content' field."
                     do_break = True
                 else:
-                    if not a["content"].has_key("title") or not a["content"].has_key("description"):
+                    if not "title" in a["content"] or not "description" in a["content"]:
                         error_string += " Each annotation must have 'content/title' and 'content/description' fields."
                         do_break = True
+                    # Test for metadatacomposer extra datas, type video, audio, text, links or slideshow
+                    if "mimetype" in a["content"]:
+                        if a["content"]["mimetype"]=="application/x-ldt-video" or a["content"]["mimetype"]=="application/x-ldt-audio":
+                            if not "url" in a["content"] or not "embedcode" in a["content"]:
+                                error_string += " A video or audio annotation must have 'content/url' and 'content/embedcode' fields."
+                                do_break = True
+                        elif a["content"]["mimetype"]=="application/x-ldt-text":
+                            if not "markup" in a["content"] or not "text" in a["content"]:
+                                error_string += " A text annotation must have 'content/markup' and 'content/text' fields."
+                                do_break = True
+                        elif a["content"]["mimetype"]=="application/x-ldt-links":
+                            if not "links" in a["content"]:
+                                error_string += " A links annotation must have 'content/links' field."
+                                do_break = True
+                            else:
+                                for l in a["content"]["links"]:
+                                    if not "url" in l or not "title" in l:
+                                        error_string += " Each link in links annotation must have 'url' and 'title' fields."
+                                        do_break = True
+                        elif a["content"]["mimetype"]=="application/x-ldt-slideshow":
+                            if not "slideduration" in a["content"] or not "autostart" in a["content"] or not "images" in a["content"]:
+                                error_string += " A slideshow annotation must have 'content/slideduration' and 'content/autostart' and 'content/images' fields."
+                                do_break = True
+                            else:
+                                for i in a["content"]["images"]:
+                                    if not "url" in i or not "title" in i or not "description" in i:
+                                        error_string += " Each image in slideshow annotation must have 'url' and 'title' and 'description' fields."
+                                        do_break = True
+                # End test annotations
                 if do_break:
                     break
         # Views node
-        if not cinelab_json.has_key("views"):
+        if not "views" in cinelab_json:
             error_string += " The cinelab json needs a 'views' node."
         else:
             for v in cinelab_json["views"]:
-                if v.has_key("meta") :
-                    if v["meta"].has_key("stat") :
+                if "meta" in v:
+                    if "stat" in v["meta"]:
                         continue
-                if not v.has_key("id") or not v.has_key("contents") or not v.has_key("annotation_types"):
+                if not "id" in v or not "contents" in v or not "annotation_types" in v:
                     error_string += " Each view must have 'id', 'contents', and 'annotation_types' fields."
                     break
         # Tags node
-        if not cinelab_json.has_key("tags"):
+        if not "tags" in cinelab_json:
             error_string += " The cinelab json needs a 'tags' node."
         else:
             do_break = False
             for t in cinelab_json["tags"]:
-                if not t.has_key("id"):
+                if not "id" in t:
                     error_string += " Each tag must have an 'id' field."
                     do_break = True
-                if not t.has_key("meta"):
+                if not "meta" in t:
                     error_string += " Each tag must have 'meta' field."
                     do_break = True
                 else:
-                    if not t["meta"].has_key("dc:title"):
+                    if not "dc:title" in t["meta"]:
                         error_string += " Each tag must have a 'meta/dc:title' field."
                         do_break = True
                 if do_break:
--- a/src/ldt/ldt/ldt_utils/projectserializer.py	Tue May 28 10:45:50 2013 +0200
+++ b/src/ldt/ldt/ldt_utils/projectserializer.py	Tue May 28 12:38:17 2013 +0200
@@ -6,7 +6,6 @@
 from ldt.ldt_utils.models import Content, Project
 from ldt.ldt_utils.stat import get_string_from_buckets
 from ldt.ldt_utils.utils import reduce_text_node
-import logging
 import lxml.etree
 import uuid
 
@@ -14,6 +13,7 @@
 
 DATE_FORMATS = ["%d/%m/%Y", "%Y-%m-%d"]
 
+import logging
 logger = logging.getLogger(__name__)
 
 """
@@ -256,7 +256,7 @@
                         "mimetype": "application/x-ldt-structured",
                         "title": element_title,
                         "description": element_description,
-                        "color": element_color,
+                        #"color": element_color,
                         "img": {
                             "src": element_ldt_src,
                         },
@@ -280,6 +280,40 @@
                 if element_source:
                     new_annotation['meta']['dc:source'] = element_source
                 
+                # Metadatacomposer features. An annotation can have the usual datas (title, description...)
+                # and new kinds of extra metas : video, audio, text, links array, images slideshow
+                # Get type
+                meta_type_node = element_node.xpath("meta/type")
+                if len(meta_type_node) > 0:
+                    meta_type = reduce_text_node(meta_type_node[0], "text()")
+                    # Update mimetype and add datas
+                    if meta_type=="video":
+                        new_annotation["content"]["mimetype"] = "application/x-ldt-video"
+                        new_annotation["content"]["url"] = reduce_text_node(element_node, "meta/url/text()")
+                        new_annotation["content"]["embedcode"] = reduce_text_node(element_node, "meta/embedcode/text()")
+                    elif meta_type=="audio":
+                        new_annotation["content"]["mimetype"] = "application/x-ldt-audio"
+                        new_annotation["content"]["url"] = reduce_text_node(element_node, "meta/url/text()")
+                        new_annotation["content"]["embedcode"] = reduce_text_node(element_node, "meta/embedcode/text()")
+                    elif meta_type=="text":
+                        new_annotation["content"]["mimetype"] = "application/x-ldt-text"
+                        new_annotation["content"]["markup"] = reduce_text_node(element_node, "meta/markup/text()")
+                        new_annotation["content"]["text"] = reduce_text_node(element_node, "meta/text/text()")
+                    elif meta_type=="links":
+                        new_annotation["content"]["mimetype"] = "application/x-ldt-links"
+                        new_annotation["content"]["links"] = []
+                        link_nodes = element_node.xpath("meta/links/link")
+                        for link in link_nodes:
+                            new_annotation["content"]["links"].append({"url": reduce_text_node(link, "url/text()"), "title":reduce_text_node(link, "title/text()")})
+                    elif meta_type=="slideshow":
+                        new_annotation["content"]["mimetype"] = "application/x-ldt-slideshow"
+                        new_annotation["content"]["slideduration"] = reduce_text_node(element_node, "meta/slideduration/text()")
+                        new_annotation["content"]["autostart"] = {'true': True, 'false': False, "0": False, "1": True}.get(reduce_text_node(element_node, "meta/autostart/text()").lower())
+                        new_annotation["content"]["images"] = []
+                        image_nodes = element_node.xpath("meta/images/image")
+                        for image in image_nodes:
+                            new_annotation["content"]["images"].append({"url": reduce_text_node(image, "url/text()"), "title":reduce_text_node(image, "title/text()"), "description":reduce_text_node(image, "description/text()")})
+                                
                 self.annotations_dict[element_id] = new_annotation
                 self.annotations_by_annotation_types[decoupage_id].append(new_annotation)