cinelab serializer validator.
authorcavaliet
Thu, 15 Nov 2012 15:15:37 +0100
changeset 948 e5b886bccd83
parent 947 9f0bf23cec0b
child 949 091da391cbec
child 951 bec241ea6aea
cinelab serializer validator.
src/ldt/ldt/api/ldt/serializers/cinelabserializer.py
--- a/src/ldt/ldt/api/ldt/serializers/cinelabserializer.py	Thu Nov 15 00:23:46 2012 +0100
+++ b/src/ldt/ldt/api/ldt/serializers/cinelabserializer.py	Thu Nov 15 15:15:37 2012 +0100
@@ -6,10 +6,11 @@
 from ldt.ldt_utils.projectserializer import ProjectJsonSerializer
 from ldt.ldt_utils.utils import generate_uuid
 from tastypie.serializers import Serializer
-from tastypie.exceptions import NotFound
+from tastypie.exceptions import NotFound, BadRequest
 import math
 import lxml.etree
 import logging
+import tastypie
 
 
 class CinelabSerializer(Serializer):
@@ -82,6 +83,8 @@
     
     
     def cinelab_to_ldt(self, cinelab, ldt_id=None):
+        # We validate the json
+        self.validate_cinelab_json(cinelab)
         # Start xml
         meta = cinelab["meta"]
         annotation_types = cinelab["annotation-types"]
@@ -293,4 +296,117 @@
         
         # This is the end
         return iri
-        
\ No newline at end of file
+    
+    
+    # This function checks every node and tests if each element has the required fields (i.e. "id", "dc:creator"....)
+    def validate_cinelab_json(self, cinelab_json):
+        error_string = ""
+        do_break = False
+        # Meta node
+        if not cinelab_json.has_key("meta"):
+            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"):
+                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"):
+            error_string += " The cinelab json needs a 'medias' node."
+        else:
+            for m in cinelab_json["medias"]:
+                if not m.has_key("id"):
+                    error_string += " Each media must have an 'id' field."
+                    break
+        # Lists node
+        if not cinelab_json.has_key("lists"):
+            error_string += " The cinelab json needs a 'lists' node."
+        else:
+            for l in cinelab_json["lists"]:
+                if not l.has_key("id"):
+                    error_string += " Each list must have an 'id' field."
+                    do_break = True
+                if not l.has_key("items"):
+                    error_string += " Each list must have an 'items' field."
+                    do_break = True
+                if not l.has_key("meta"):
+                    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"):
+                            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"):
+                        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"):
+            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"):
+                    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"):
+            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"):
+                    error_string += " Each annotation must have 'id', 'begin', 'end', 'tags' and 'media' fields."
+                    do_break = True
+                if not a.has_key("meta"):
+                    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"):
+                        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"):
+                    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"):
+                        error_string += " Each annotation must have 'content/title' and 'content/description' fields."
+                        do_break = True
+                if do_break:
+                    break
+        # Views node
+        if not cinelab_json.has_key("views"):
+            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") :
+                        continue
+                if not v.has_key("id") or not v.has_key("contents") or not v.has_key("annotation_types"):
+                    error_string += " Each view must have 'id', 'contents', and 'annotation_types' fields."
+                    break
+        # Tags node
+        if not cinelab_json.has_key("tags"):
+            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"):
+                    error_string += " Each tag must have an 'id' field."
+                    do_break = True
+                if not t.has_key("meta"):
+                    error_string += " Each tag must have 'meta' field."
+                    do_break = True
+                else:
+                    if not t["meta"].has_key("dc:title"):
+                        error_string += " Each tag must have a 'meta/dc:title' field."
+                        do_break = True
+                if do_break:
+                    break
+        
+        # Finally
+        if error_string!="":
+            raise BadRequest(error_string)
+        
+        return True
\ No newline at end of file