--- a/src/ldt/ldt/api/ldt/resources/content.py Mon Oct 29 12:04:20 2012 +0100
+++ b/src/ldt/ldt/api/ldt/resources/content.py Mon Nov 05 15:19:24 2012 +0100
@@ -1,6 +1,6 @@
from django.conf.urls.defaults import url
from ldt.ldt_utils.models import Content
-from tastypie.resources import ModelResource
+from tastypie.resources import Bundle, ModelResource
class ContentResource(ModelResource):
@@ -15,4 +15,14 @@
return [
url(r"^(?P<resource_name>%s)/(?P<iri_id>[\w\d_.-]+)/$" % self._meta.resource_name, self.wrap_view('dispatch_detail'), name="api_dispatch_detail"),
]
+ def get_resource_uri(self, bundle_or_obj):
+ kwargs = {
+ 'resource_name': self._meta.resource_name,
+ 'api_name': self._meta.api_name
+ }
+ if isinstance(bundle_or_obj, Bundle):
+ kwargs['iri_id'] = bundle_or_obj.obj.iri_id
+ else:
+ kwargs['iri_id'] = bundle_or_obj.iri_id
+ return self._build_reverse_url("api_dispatch_detail", kwargs=kwargs)
\ No newline at end of file
--- a/src/ldt/ldt/api/ldt/resources/project.py Mon Oct 29 12:04:20 2012 +0100
+++ b/src/ldt/ldt/api/ldt/resources/project.py Mon Nov 05 15:19:24 2012 +0100
@@ -1,15 +1,20 @@
from django.conf.urls.defaults import url
from ldt.ldt_utils.models import Project
+from ldt.api.ldt.serializers.cinelabserializer import CinelabSerializer
from tastypie.authorization import Authorization
-from tastypie.resources import ModelResource
+from tastypie.exceptions import BadRequest, ImmediateHttpResponse
+from tastypie.http import HttpAccepted, HttpNoContent, HttpUnauthorized
+from tastypie.resources import Bundle, ModelResource
+from tastypie.utils import dict_strip_unicode_keys
class ProjectResource(ModelResource):
class Meta:
- allowed_methods = ['get', 'put']
- authorization= Authorization() # BE CAREFUL WITH THAT, it's unsecure
+ allowed_methods = ['get', 'post']
+ #authorization= Authorization() # BE CAREFUL WITH THAT, it's unsecure
resource_name = 'projects'
queryset = Project.objects.all()
+ serializer = CinelabSerializer()
# # WARNING : this project API will only accepts and returns json format, no matter format get parameter.
# def determine_format(self, request):
@@ -20,4 +25,15 @@
return [
url(r"^(?P<resource_name>%s)/(?P<ldt_id>[\w\d_.-]+)/$" % self._meta.resource_name, self.wrap_view('dispatch_detail'), name="api_dispatch_detail"),
]
+
+ def get_resource_uri(self, bundle_or_obj):
+ kwargs = {
+ 'resource_name': self._meta.resource_name,
+ 'api_name': self._meta.api_name
+ }
+ if isinstance(bundle_or_obj, Bundle):
+ kwargs['ldt_id'] = bundle_or_obj.obj.ldt_id
+ else:
+ kwargs['ldt_id'] = bundle_or_obj.ldt_id
+ return self._build_reverse_url("api_dispatch_detail", kwargs=kwargs)
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ldt/ldt/api/ldt/serializers/__init__.py Mon Nov 05 15:19:24 2012 +0100
@@ -0,0 +1,1 @@
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ldt/ldt/api/ldt/serializers/cinelabserializer.py Mon Nov 05 15:19:24 2012 +0100
@@ -0,0 +1,46 @@
+from django.core.serializers import json
+from django.utils import simplejson
+from ldt.ldt_utils.models import Project
+from ldt.ldt_utils.projectserializer import ProjectJsonSerializer
+from tastypie.serializers import Serializer
+from tastypie.exceptions import NotFound
+import logging
+
+
+class CinelabSerializer(Serializer):
+ # Thanks to the this serializer, the api will be able to serialize and deserialize a project in cinelab json format
+ # See http://liris.cnrs.fr/advene/cinelab/ for more information
+ formats = ['json', 'jsonp', 'xml', 'yaml', 'html', 'plist', 'cinelab']
+ content_types = {
+ 'cinelab': 'application/cinelab',
+ 'json': 'application/json',
+ 'jsonp': 'text/javascript',
+ 'xml': 'application/xml',
+ 'yaml': 'text/yaml',
+ 'html': 'text/html',
+ 'plist': 'application/x-plist',
+ }
+ json_indent = 2
+
+ def to_cinelab(self, data, options=None):
+ """
+ Given some Python data, produces JSON output.
+ """
+ logging.debug("TO cinelab data " + repr(data))
+ options = options or {}
+ if hasattr(data, 'obj') :
+ if isinstance(data.obj, Project) :
+ ps = ProjectJsonSerializer(data.obj)
+ data = ps.serialize_to_cinelab()
+ return simplejson.dumps(data, cls=json.DjangoJSONEncoder, sort_keys=True, indent=self.json_indent)
+ raise NotFound("Project not found")
+
+
+ def from_cinelab(self, content):
+ """
+ This function takes cinelab/json and transform it into a regular REST API json project data.
+ """
+ logging.debug("FROM cinelab content = " + content)
+ #a = simplejson.loads(content)
+ logging.debug("msg")
+ return simplejson.loads(content)
\ No newline at end of file
--- a/src/ldt/ldt/ldt_utils/projectserializer.py Mon Oct 29 12:04:20 2012 +0100
+++ b/src/ldt/ldt/ldt_utils/projectserializer.py Mon Nov 05 15:19:24 2012 +0100
@@ -16,7 +16,7 @@
"""
Serialize a project object to a cinelab compatible array
"""
-class ProjectSerializer:
+class ProjectJsonSerializer:
def __init__(self, project, from_contents=True, from_display=True, first_cutting=None, only_one_cutting=False):
self.project = project
--- a/src/ldt/ldt/ldt_utils/views/json.py Mon Oct 29 12:04:20 2012 +0100
+++ b/src/ldt/ldt/ldt_utils/views/json.py Mon Nov 05 15:19:24 2012 +0100
@@ -5,9 +5,8 @@
from django.utils import simplejson
from django.utils.html import escape
from django.utils.translation import ugettext as _
-from ldt.indexation import get_results_with_context
from ldt.ldt_utils.models import Project
-from ldt.ldt_utils.projectserializer import ProjectSerializer
+from ldt.ldt_utils.projectserializer import ProjectJsonSerializer
from ldt.ldt_utils.searchutils import search_generate_ldt
from datetime import datetime
import ldt.auth as ldt_auth
@@ -63,7 +62,7 @@
escape_bool = {'true': True, 'false': False, "0": False, "1": True}.get(escape_str.lower())
- ps = ProjectSerializer(project, serialize_contents, first_cutting=first_cutting)
+ ps = ProjectJsonSerializer(project, serialize_contents, first_cutting=first_cutting)
project_dict = ps.serialize_to_cinelab()
json_str = simplejson.dumps(project_dict, ensure_ascii=False, indent=indent)
@@ -104,7 +103,7 @@
now = datetime.now()
project.modification_date = project.creation_date = now
#return HttpResponse(lxml.etree.tostring(project_xml, pretty_print=True), mimetype="text/xml;charset=utf-8")
- ps = ProjectSerializer(project, from_contents=False)
+ ps = ProjectJsonSerializer(project, from_contents=False)
mashup_dict = ps.serialize_to_cinelab()
# Now we build the mashup with the good segments (the ones between in and out)
if results:
@@ -133,8 +132,8 @@
cur_in = float(res["start_ts"])
cur_out = cur_in + float(res["duration"])
if tc_in<=cur_in and cur_out<=tc_out:
- #filtered_results.append(res)
- mashup_list["items"].append(res["element_id"])
+ #filtered_results.append(res)
+ mashup_list["items"].append(res["element_id"])
mashup_dict["lists"].append(mashup_list)
#mashup_dict["escape_bool"] = escape_bool
--- a/src/ldt/ldt/ldt_utils/views/rdf.py Mon Oct 29 12:04:20 2012 +0100
+++ b/src/ldt/ldt/ldt_utils/views/rdf.py Mon Nov 05 15:19:24 2012 +0100
@@ -1,7 +1,7 @@
from django.http import HttpResponse, HttpResponseForbidden
from django.utils.translation import ugettext as _
from ldt.ldt_utils.models import Project
-from ldt.ldt_utils.projectserializer import ProjectSerializer
+from ldt.ldt_utils.projectserializer import ProjectJsonSerializer
import ldt.auth as ldt_auth
import logging
import lxml.etree
@@ -25,7 +25,7 @@
resp['Cache-Control'] = 'no-cache, must-revalidate'
resp['Pragma'] = 'no-cache'
- ps = ProjectSerializer(project, from_contents=False, from_display=True)
+ ps = ProjectJsonSerializer(project, from_contents=False, from_display=True)
annotations = ps.get_annotations(first_cutting=True)
rdf_ns = u"http://www.w3.org/1999/02/22-rdf-syntax-ns#"
--- a/src/ldt/ldt/ldt_utils/views/workspace.py Mon Oct 29 12:04:20 2012 +0100
+++ b/src/ldt/ldt/ldt_utils/views/workspace.py Mon Nov 05 15:19:24 2012 +0100
@@ -13,7 +13,7 @@
from ldt.ldt_utils.forms import SearchForm
from ldt.ldt_utils.models import Content, Project, Segment
from ldt.ldt_utils.utils import boolean_convert
-from ldt.ldt_utils.projectserializer import ProjectSerializer
+from ldt.ldt_utils.projectserializer import ProjectJsonSerializer
from ldt.ldt_utils.views.content import get_contents_page, get_content_tags
from ldt.ldt_utils.views.project import get_projects_page, get_published_projects_page
from ldt.security.utils import add_change_attr, get_userlist
@@ -170,7 +170,7 @@
if not ldt_auth.check_access(request.user, project):
return HttpResponseForbidden(_("You can not access this project"))
- ps = ProjectSerializer(project, from_contents=True, from_display=True)
+ ps = ProjectJsonSerializer(project, from_contents=True, from_display=True)
annotations = ps.get_annotations(first_cutting=True)
rend_dict = {'json_url':json_url, 'player_id':player_id, 'annotations':annotations, 'ldt_id': ldt_id, 'stream_mode': stream_mode,
'player_width': player_width, 'player_height': player_height, 'external_url': external_url,