# HG changeset patch # User cavaliet # Date 1352307134 -3600 # Node ID 9f29dfc4c2036dfa13c659b457736836f7a94244 # Parent d96088f3892bef0af1ec1273ce210fb36528cf15 first step of the json cinelab to xml ldt serializer. Eveything works but edits. diff -r d96088f3892b -r 9f29dfc4c203 src/ldt/ldt/api/ldt/serializers/cinelabserializer.py --- a/src/ldt/ldt/api/ldt/serializers/cinelabserializer.py Wed Nov 07 16:25:16 2012 +0100 +++ b/src/ldt/ldt/api/ldt/serializers/cinelabserializer.py Wed Nov 07 17:52:14 2012 +0100 @@ -1,7 +1,8 @@ +from django.conf import settings from django.core.serializers import json from django.core.urlresolvers import reverse from django.utils import simplejson -from ldt.ldt_utils.models import Project +from ldt.ldt_utils.models import Content, Project from ldt.ldt_utils.projectserializer import ProjectJsonSerializer from ldt.ldt_utils.utils import generate_uuid from tastypie.serializers import Serializer @@ -54,21 +55,167 @@ "modification_date": "2012-02-11T01:19:40.203322", "state": 2, "title*": "front project : test DailyMotion" - "contents": ["IRI_ID_1","IRI_ID_2"] + "contents*": ["IRI_ID_1","IRI_ID_2"] + "owner*": "user_id" """ logging.debug("FROM cinelab content = " + content) cinelab = simplejson.loads(content) meta = cinelab["meta"] ldt_id = generate_uuid() + # default state = (1, 'edition') OR (2, 'published') + state = 1 contents = [reverse("api_dispatch_detail", kwargs={"api_name":"1.0", "resource_name":"contents", "iri_id":c["id"]}) for c in cinelab["medias"]] owner_uri = reverse("api_dispatch_detail", kwargs={"api_name":"1.0", "resource_name":"users", "username":meta["dc:creator"]}) - s = {"description": meta["dc:description"], "ldt_id": "000_GEN_BY_TC3", "title": meta["dc:title"], + + ldt = lxml.etree.tostring(self.cinelab_to_ldt(cinelab), pretty_print=True) + + s = {"description": meta["dc:description"], "ldt_id": ldt_id, "title": meta["dc:title"], "created_by": meta["dc:creator"], "changed_by": meta["dc:contributor"], "created_by": meta["dc:creator"], "changed_by": meta["dc:contributor"], - "contents": contents, "owner": owner_uri} + "contents": contents, "owner": owner_uri, "state":state, "ldt":ldt} #s = '{"description": "", "ldt": "", "000ldt_id": "gen_by_tc","title": "aaa GEN BY TC"}' #s = '{"description": "", "ldt": "", "title": "aaaGEN BY TC"}' - return simplejson.loads(simplejson.dumps(s)) - + #return simplejson.loads(simplejson.dumps(s)) + return s + + + def cinelab_to_ldt(self, cinelab, ldt_id=None): + # Start xml + meta = cinelab["meta"] + annotation_types = cinelab["annotation-types"] + annotations = cinelab["annotations"] + # If the ldt_id is not set, we get in the cinelab meta + if not ldt_id: + ldt_id = meta["id"] + + # Before creating the xml, we build a dict for tag {"id":"label"} + # Careful : works with python >= 2.7 + tag_dict = {t["id"]:t["meta"]["dc:title"] for t in cinelab["tags"]} + # We'll also build a annotation-type to media/ensemble dict to simplify the views node building + at_media_dict = {} + + # create a dom + iri = lxml.etree.Element('iri') + + # Node project + projectNode = lxml.etree.SubElement(iri, 'project') + projectNode.set('abstract', meta["dc:description"]) + projectNode.set('title', meta["dc:title"]) + projectNode.set('user', meta["dc:creator"]) + projectNode.set('id', ldt_id) + + # Node medias and node annotations + mediasNode = lxml.etree.SubElement(iri, 'medias') + annotationsNode = lxml.etree.SubElement(iri, 'annotations') + for c in cinelab["medias"]: + # We add the content to the medias node + content = Content.objects.get(iri_id=c["id"]) + iri_id = content.iri_id + mediaNode = lxml.etree.SubElement(mediasNode, 'media') + mediaNode.set('id', iri_id) + mediaNode.set('src', content.iri_url()) + if content.videopath != None : + mediaNode.set('video', content.videopath) + else: + mediaNode.set('video', settings.STREAM_URL) + mediaNode.set('pict', "") + mediaNode.set('extra', "") + # We add the annotations + contentNode = lxml.etree.SubElement(annotationsNode, 'content') + 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 l["meta"]["id-ref"]==iri_id: + # We build the ensemble node + ensembleNode = lxml.etree.SubElement(contentNode, 'ensemble') + ensembleNode.set('id', l["id"]) + ensembleNode.set('idProject', ldt_id) + ensembleNode.set('title', l["meta"]["dc:title"]) + ensembleNode.set('author', l["meta"]["dc:creator"]) + ensembleNode.set('abstract', l["meta"]["dc:description"]) + # We build the decoupage node, equivalent to an annotation-type + for at_ref in l["items"]: + at_id = at_ref["id-ref"] + # We get the annotation-type datas + for at in annotation_types: + if at["id"]==at_id: + at_media_dict[at_id] = (iri_id, l["id"]) + decoupageNode = lxml.etree.SubElement(ensembleNode, 'decoupage') + decoupageNode.set('id', at_id) + decoupageNode.set('author', at["dc:creator"]) + titleDec = lxml.etree.SubElement(decoupageNode, 'title') + titleDec.text = at["dc:title"] + abstractDec = lxml.etree.SubElement(decoupageNode, 'abstract') + abstractDec.text = at["dc:description"] + elementsNode = lxml.etree.SubElement(decoupageNode, 'elements') + # We get all the annotations for this media and this annotation-type + for a in annotations: + if a["media"]==iri_id and a["meta"]["id-ref"]==at_id: + elementNode = lxml.etree.SubElement(elementsNode, 'element') + elementNode.set('id', a["id"]) + elementNode.set('begin', str(a["begin"])) + elementNode.set('dur', str(a["end"]-a["begin"])) + elementNode.set('author', a["meta"]["dc:creator"]) + elementNode.set('date', a["meta"]["dc:modified"]) + elementNode.set('color', a["color"]) + elementNode.set('src', "") + titleElm = lxml.etree.SubElement(elementNode, 'title') + titleElm.text = a["content"]["title"] + abstractElm = lxml.etree.SubElement(elementNode, 'abstract') + abstractElm.text = a["content"]["description"] + # Audio node, if the dict has the audio key + audioElm = lxml.etree.SubElement(elementNode, 'audio') + audioElm.set('source', "") + if a["content"].has_key("audio"): + 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"]): + tagNode = lxml.etree.SubElement(tagsNode, 'tag') + tagNode.text = tag_dict[t["id-ref"]] + # Last element's node + lxml.etree.SubElement(elementNode, 'meta') + + # Now all medias and annotation-types and annotations are the xml + # We can set the views/displays node + displaysNode = lxml.etree.SubElement(iri, 'displays') + id_sel = None + i = 1 + for v in cinelab["views"]: + if "stat" not in v["id"] and v.has_key("annotation_types"): + displayNode = lxml.etree.SubElement(displaysNode, 'display') + displayNode.set('id', v["id"]) + displayNode.set('title', "View " + str(i)) + i += 1 + displayNode.set('tc', "0") + displayNode.set('zoom', "0") + displayNode.set('scroll', "0") + audioDis = lxml.etree.SubElement(displayNode, 'audio') + audioDis.set('source', "") + last_iri_id = "" + last_content_node = None + for at_id in v["annotation_types"]: + iri_id, ens_id = at_media_dict[at_id] + if iri_id != last_iri_id: + last_iri_id = iri_id + last_content_node = lxml.etree.SubElement(displayNode, 'content') + last_content_node.set('id', iri_id) + if last_content_node is not None: + decoupageNode = lxml.etree.SubElement(last_content_node, 'decoupage') + decoupageNode.set('idens', ens_id) + decoupageNode.set('id', at_id) + decoupageNode.set('tagsSelect', "") + if not id_sel: + id_sel = iri_id + if not id_sel: + id_sel = "" + displayNode.set('idsel', id_sel) + + # This is the end + return iri \ No newline at end of file