api update after tastypie update. cinelab serializer update for metadatacomposer (validation not coded)
authorcavaliet
Tue, 28 May 2013 10:45:50 +0200
changeset 1197 a731809df0fd
parent 1196 0d86ca7ccc7b
child 1198 62eee5e70b6b
api update after tastypie update. cinelab serializer update for metadatacomposer (validation not coded)
src/ldt/ldt/api/ldt/resources/project.py
src/ldt/ldt/api/ldt/serializers/cinelabserializer.py
--- a/src/ldt/ldt/api/ldt/resources/project.py	Fri May 24 21:55:08 2013 +0200
+++ b/src/ldt/ldt/api/ldt/resources/project.py	Tue May 28 10:45:50 2013 +0200
@@ -1,6 +1,8 @@
+from django import VERSION as django_version
 from django.conf import settings
 from django.conf.urls import url
 from django.contrib.auth.models import Group
+from django.core.exceptions import ObjectDoesNotExist
 from guardian.shortcuts import assign
 from ldt.api.ldt.authentication import (SessionAuthentication, 
     MultiAuthentication, ApiKeyAuthentication)
@@ -12,7 +14,7 @@
 from ldt.security.permissionchecker import check_object_perm_for_user
 from tastypie import fields, http
 from tastypie.authorization import Authorization
-from tastypie.exceptions import BadRequest
+from tastypie.exceptions import BadRequest, NotFound
 from tastypie.resources import Bundle, ModelResource, ALL
 from tastypie.utils import dict_strip_unicode_keys
 
@@ -87,10 +89,14 @@
         the request returns accepted (202) if the project is well updated
         and Not Found (404) if the id in url or datas were incorrect.
         """
-        deserialized = self.deserialize(request, request.raw_post_data, format=request.META.get('CONTENT_TYPE', 'application/json'))
+        if django_version >= (1, 4):
+            body = request.body
+        else:
+            body = request.raw_post_data
+        deserialized = self.deserialize(request, body, format=request.META.get('CONTENT_TYPE', 'application/json'))
         deserialized = self.alter_deserialized_detail_data(request, deserialized)
         bundle = self.build_bundle(data=dict_strip_unicode_keys(deserialized), request=request)
-        self.is_valid(bundle, request)
+        
         try:
             # We have to check if the user can change the project BEFORE calling obj_update
             proj = Project.objects.get(ldt_id=bundle.data['ldt_id'])
@@ -98,7 +104,15 @@
                 # Even if the user has the guardian right to change the project, 
                 # tastypie's save_m2m will raise an error. So we unprotect just for this saving.
                 unprotect_models()
-                updated_bundle = self.obj_update(bundle, request=request, **self.remove_api_resource_names(kwargs))
+                # Here we hack self.obj_update because it bugs : add request object to data and makes self.obj_get bug.
+                #updated_bundle = self.obj_update(bundle, request=request, **self.remove_api_resource_names(kwargs))
+                try:
+                    bundle.obj = self.obj_get(bundle=bundle, **self.remove_api_resource_names(kwargs))
+                except ObjectDoesNotExist:
+                    raise NotFound("A model instance matching the provided arguments could not be found.")
+                bundle = self.full_hydrate(bundle)
+                updated_bundle = self.save(bundle)
+                # Save is done, we can reactivate guardian rights
                 protect_models()
             else:
                 raise BadRequest("User has no right to change the project.")
--- a/src/ldt/ldt/api/ldt/serializers/cinelabserializer.py	Fri May 24 21:55:08 2013 +0200
+++ b/src/ldt/ldt/api/ldt/serializers/cinelabserializer.py	Tue May 28 10:45:50 2013 +0200
@@ -195,7 +195,53 @@
                                                         tagNode = lxml.etree.SubElement(tagsNode, 'tag')
                                                         tagNode.text = tag_dict[t["id-ref"]]
                                             # Last element's node
-                                            lxml.etree.SubElement(elementNode, 'meta')
+                                            metaNode = lxml.etree.SubElement(elementNode, 'meta')
+                                            # New for metadatacomposer : we add meta informations about extra of different kinds : 
+                                            # related video, audio, links and html code which are not the native annotation datas
+                                            # like title, description, etc.
+                                            if "mimetype" in a["content"]:
+                                                # Video or audio bonus
+                                                if a["content"]["mimetype"]=="application/x-ldt-video" or a["content"]["mimetype"]=="application/x-ldt-audio":
+                                                    typeNode = lxml.etree.SubElement(metaNode, 'type')
+                                                    if a["content"]["mimetype"]=="application/x-ldt-video":
+                                                        typeNode.text = "video"
+                                                    else:
+                                                        typeNode.text = "audio"
+                                                    urlNode = lxml.etree.SubElement(metaNode, 'url')
+                                                    urlNode.text = a["content"]["url"]
+                                                    embedcodeNode = lxml.etree.SubElement(metaNode, 'embedcode')
+                                                    embedcodeNode.text = lxml.etree.CDATA(a["content"]["embedcode"])
+                                                # Text bonus
+                                                elif a["content"]["mimetype"]=="application/x-ldt-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":
+                                                    linksNode = lxml.etree.SubElement(metaNode, 'links')
+                                                    for link in a["content"]["links"]:
+                                                        linkNode = lxml.etree.SubElement(linksNode, 'link')
+                                                        urlNode = lxml.etree.SubElement(linkNode, 'url')
+                                                        urlNode.text = link["url"]
+                                                        titleNode = lxml.etree.SubElement(linkNode, 'title')
+                                                        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')
+                                                    durationNode.text = str(a["content"]["slideduration"])
+                                                    autostartNode = lxml.etree.SubElement(slideshowNode, 'autostart')
+                                                    autostartNode.text = str(a["content"]["autostart"])
+                                                    imagesNode = lxml.etree.SubElement(slideshowNode, 'images')
+                                                    for img in a["content"]["images"]:
+                                                        imageNode = lxml.etree.SubElement(imagesNode, 'image')
+                                                        urlNode = lxml.etree.SubElement(imageNode, 'url')
+                                                        urlNode.text = img["url"]
+                                                        titleNode = lxml.etree.SubElement(imageNode, 'title')
+                                                        titleNode.text = img["title"]
+                                                        descriptionNode = lxml.etree.SubElement(imageNode, 'description')
+                                                        descriptionNode.text = img["description"]
         
         # Now all medias and annotation-types and annotations are the xml
         # We can set the views/displays node