src/ldt/ldt/api/ldt/handlers.py
author verrierj
Mon, 30 Jan 2012 17:21:13 +0100
changeset 485 9ee8cc15e244
parent 482 c802e00c7131
child 560 1cb2a4a573e1
permissions -rw-r--r--
Add setting to metadataplayer for API url
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
412
8d777b1d1d92 Add API to search annotations inside a content between two timecodes
verrierj
parents: 388
diff changeset
     1
from ldt.ldt_utils.models import Project, Content, Segment
8d777b1d1d92 Add API to search annotations inside a content between two timecodes
verrierj
parents: 388
diff changeset
     2
from django.db.models import F, Q
13
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
from piston.handler import BaseHandler
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
from piston.utils import rc, require_extended
196
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 63
diff changeset
     5
from ldt.ldt_utils.utils import LdtAnnotation
340
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents: 336
diff changeset
     6
from ldt.ldt_utils.stat import add_annotation_to_stat
424
a2f72b31811b Add french translations + small bugfixes
verrierj
parents: 420
diff changeset
     7
from ldt.security import protect_models, unprotect_models
412
8d777b1d1d92 Add API to search annotations inside a content between two timecodes
verrierj
parents: 388
diff changeset
     8
from ldt.ldt_utils.segmentserializer import SegmentSerializer
13
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
import logging #@UnresolvedImport
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
196
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 63
diff changeset
    11
13
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
class ProjectHandler(BaseHandler):
63
93325a5d61f0 organize format and import
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
    13
    allowed_methods = ('GET', 'PUT',)
13
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
    model = Project   
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
    def read(self, request, project_id):
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
        """
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
        returns a single project
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
        """
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
        return Project.objects.get(ldt_id=project_id)
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
    
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
    @require_extended
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
    def update(self, request, project_id):
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
        """
332
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
    25
        This method is called when a PUT request is sent to http://<plateform_location>/api/ldt/projects/<project_id>.<format>. 
196
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 63
diff changeset
    26
            <project_id> is the ldt_id field of the project. If <projet_id> does not match any project on the platform, a 410 ("Gone")
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 63
diff changeset
    27
            error will be returned.
197
6dfdc12e338a Improved API for annotations sent in XML
verrierj
parents: 196
diff changeset
    28
            <format> is the format of the data sent back by the server. It can be 'json', 'yaml', 'xml' or 'pickle'.
200
a38c4759ce8a Changed API handler for XML files back to its original behavior
verrierj
parents: 199
diff changeset
    29
            
a38c4759ce8a Changed API handler for XML files back to its original behavior
verrierj
parents: 199
diff changeset
    30
        If the request contains a content-type header whose value is set to "application/json" and a valid utf-8 encoded JSON file,
a38c4759ce8a Changed API handler for XML files back to its original behavior
verrierj
parents: 199
diff changeset
    31
        the following conditions will be checked before the annotations are added : 
a38c4759ce8a Changed API handler for XML files back to its original behavior
verrierj
parents: 199
diff changeset
    32
            If the submitted file is not valid or refers to a media that is not contained in the project, a 500  ("Bad Request")
a38c4759ce8a Changed API handler for XML files back to its original behavior
verrierj
parents: 199
diff changeset
    33
            error will be returned. If the "type" field of an annotation matches an already existing cutting, it will be added to that 
a38c4759ce8a Changed API handler for XML files back to its original behavior
verrierj
parents: 199
diff changeset
    34
            cutting. Otherwise, a new cutting will be added (as well as a new ensemble if needed). New cuttings are added to the view 
218
3035dd9f1a9d Fix typo in documentation
verrierj
parents: 216
diff changeset
    35
            "View at the last recording" if it exists, or to the view "Init view" else. If none of those views exist, the server will 
200
a38c4759ce8a Changed API handler for XML files back to its original behavior
verrierj
parents: 199
diff changeset
    36
            not add the cutting to a view. Several annotations can be added at the same time if the submitted file contains multiple 
216
3dcd742acc13 API return ID of added annotations
verrierj
parents: 200
diff changeset
    37
            annotations. The server returns the file submitted if all annotations have been added successfully, and adds to this file
218
3035dd9f1a9d Fix typo in documentation
verrierj
parents: 216
diff changeset
    38
            IDs of created annotations to the file with a 200("OK") error code.
196
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 63
diff changeset
    39
        
200
a38c4759ce8a Changed API handler for XML files back to its original behavior
verrierj
parents: 199
diff changeset
    40
        If no content-type header is set, the file submitted must be a valid XML file and will replace entirely the ldt field
a38c4759ce8a Changed API handler for XML files back to its original behavior
verrierj
parents: 199
diff changeset
    41
        of the project without any verifications.  
196
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 63
diff changeset
    42
        
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 63
diff changeset
    43
        Example :
13
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
        
200
a38c4759ce8a Changed API handler for XML files back to its original behavior
verrierj
parents: 199
diff changeset
    45
        Remark : The file below contain the minimum necessary fields and attributes for the handler to work. If one field or attribute is
197
6dfdc12e338a Improved API for annotations sent in XML
verrierj
parents: 196
diff changeset
    46
        missing (e.g. author, or date) during submission, an error will occur.
6dfdc12e338a Improved API for annotations sent in XML
verrierj
parents: 196
diff changeset
    47
        
196
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 63
diff changeset
    48
        A platform is reachable at http://localhost/. It contains a project with ID a0593b58-f258-11df-80e1-00145ea4a2be. This project has
200
a38c4759ce8a Changed API handler for XML files back to its original behavior
verrierj
parents: 199
diff changeset
    49
        a content milosforman_amadeus, which has a cutting Salieri inside the view "View at the last recording". The following JSON file exists in the current directory :
196
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 63
diff changeset
    50
    
332
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
    51
        Example of ajax call with 2 differents annotations :
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
    52
        $('#mon_ajax').click(function(e) {
337
496bd82719e5 Little correction on project API.
cavaliet
parents: 336
diff changeset
    53
            var url = "{% url project_api project_id='c8448f21-272d-11e1-876b-c8bcc896c290' emitter_format='.json' %}"; // Don't forget the "." before "json" !
332
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
    54
            
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
    55
            var monjson = '{\
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
    56
                    "annotations": [\
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
    57
                        {\
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
    58
                            "type": "c_07BA1284-5F24-71A8-1EE2-423EED999B8A",\
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
    59
                            "type_title": "New cutting name if necessary",\
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
    60
                            "media": "briandepalma_scarfacedepalma",\
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
    61
                            "begin": 1600000,\
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
    62
                            "end": 2100000,\
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
    63
                            "content": {\
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
    64
                                "data": "new scar annot"\
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
    65
                            },\
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
    66
                            "tags": [ "json","dude" ]\
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
    67
                        }\
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
    68
                    ],\
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
    69
                    "meta": {\
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
    70
                        "creator": "John Doe",\
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
    71
                        "created": "2011-09-10T09:12:58"\
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
    72
                    }\
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
    73
                }';
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
    74
            var monjson2 = '{\
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
    75
                    "annotations": [\
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
    76
                        {\
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
    77
                            "type": "c_07BA1284-5F24-71A8-1EE2-423EED999B8A",\
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
    78
                            "type_title": "New cutting name if necessary",\
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
    79
                            "media": "briandepalma_scarfacedepalma",\
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
    80
                            "begin": 2400000,\
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
    81
                            "end": 3000000,\
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
    82
                            "content": {\
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
    83
                                "data": "ntm iam 2"\
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
    84
                            },\
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
    85
                            "tags": [ "jak" ]\
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
    86
                        }\
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
    87
                    ],\
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
    88
                    "meta": {\
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
    89
                        "creator": "John Doe",\
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
    90
                        "created": "2011-09-10T09:12:58"\
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
    91
                    }\
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
    92
                }';
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
    93
            
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
    94
            $.ajax({
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
    95
                url: url,
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
    96
                type: 'PUT',
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
    97
                contentType: 'application/json',
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
    98
                data: monjson,
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
    99
                // bug with jquery >= 1.5, "json" adds a callback so we don't specify dataType
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
   100
                //dataType: 'json',
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
   101
                success: function(json, textStatus, XMLHttpRequest) {
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
   102
                    alert("success = " + json);
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
   103
                },
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
   104
                error: function(jqXHR, textStatus, errorThrown) {
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
   105
                    alert("ERROR = " + jqXHR.responseText + ", " + errorThrown);
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
   106
                }
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
   107
            });
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
   108
        });
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
   109
        
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
   110
        If we send a PUT request with curl :    
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
   111
        $curl -X PUT http://localhost/api/ldt/projects/a0593b58-f258-11df-80e1-00145ea4a2be.json -d @example.JSON -H  "content-type:application/json"
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
   112
        A new cutting titled "New cutting name" will be created with the first annotation inside, and the annotation "Annotation about Salieri"
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
   113
        will be added to the Salieri cutting. The returned file is :
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
   114
        
196
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 63
diff changeset
   115
        {
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 63
diff changeset
   116
            "annotations": [
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 63
diff changeset
   117
                {
332
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
   118
                    "id": "6d8baf01-ffb1-11e0-810c-001485352c9a",
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
   119
                    "type": "id_annot_type",
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
   120
                    "type_title": "New cutting name",
196
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 63
diff changeset
   121
                    "media": "milosforman_amadeus",
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 63
diff changeset
   122
                    "begin": 50000,
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 63
diff changeset
   123
                    "end": 900000,
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 63
diff changeset
   124
                    "content": {
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 63
diff changeset
   125
                        "data": "new annotation"
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 63
diff changeset
   126
                    },
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 63
diff changeset
   127
                    "tags": [ "json" ]
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 63
diff changeset
   128
                },
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 63
diff changeset
   129
                {
332
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
   130
                    "id": "6d8baf00-ffb1-11e0-8097-001485352c9b",
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
   131
                    "type": "another_id_annot_type",
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
   132
                    "type_title": "Salieri",
216
3dcd742acc13 API return ID of added annotations
verrierj
parents: 200
diff changeset
   133
                    "media": "milosforman_amadeus",
3dcd742acc13 API return ID of added annotations
verrierj
parents: 200
diff changeset
   134
                    "begin": 700000,
3dcd742acc13 API return ID of added annotations
verrierj
parents: 200
diff changeset
   135
                    "end": 1200000,
3dcd742acc13 API return ID of added annotations
verrierj
parents: 200
diff changeset
   136
                    "content": {
3dcd742acc13 API return ID of added annotations
verrierj
parents: 200
diff changeset
   137
                        "data": "Annotation about Salieri"
3dcd742acc13 API return ID of added annotations
verrierj
parents: 200
diff changeset
   138
                    },
3dcd742acc13 API return ID of added annotations
verrierj
parents: 200
diff changeset
   139
                    "tags": [ "xml", "test", "blop" ]
3dcd742acc13 API return ID of added annotations
verrierj
parents: 200
diff changeset
   140
                }
3dcd742acc13 API return ID of added annotations
verrierj
parents: 200
diff changeset
   141
            ],
3dcd742acc13 API return ID of added annotations
verrierj
parents: 200
diff changeset
   142
            
3dcd742acc13 API return ID of added annotations
verrierj
parents: 200
diff changeset
   143
            "meta": {
3dcd742acc13 API return ID of added annotations
verrierj
parents: 200
diff changeset
   144
                "creator": "John Doe",
3dcd742acc13 API return ID of added annotations
verrierj
parents: 200
diff changeset
   145
                "created": "2011-09-10T09:12:58"
3dcd742acc13 API return ID of added annotations
verrierj
parents: 200
diff changeset
   146
            }
3dcd742acc13 API return ID of added annotations
verrierj
parents: 200
diff changeset
   147
        }
197
6dfdc12e338a Improved API for annotations sent in XML
verrierj
parents: 196
diff changeset
   148
        
332
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
   149
        """
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
   150
        #return rc.ALL_OK
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
   151
        #assert False, " TIBO str(request.data) = " + str(request.data)
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
   152
        if request.content_type.lower().find("application/json") > -1 :
200
a38c4759ce8a Changed API handler for XML files back to its original behavior
verrierj
parents: 199
diff changeset
   153
            try:
a38c4759ce8a Changed API handler for XML files back to its original behavior
verrierj
parents: 199
diff changeset
   154
                project = Project.objects.get(ldt_id=project_id)
a38c4759ce8a Changed API handler for XML files back to its original behavior
verrierj
parents: 199
diff changeset
   155
            except Project.DoesNotExist:
332
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
   156
                return rc.NOT_HERE
200
a38c4759ce8a Changed API handler for XML files back to its original behavior
verrierj
parents: 199
diff changeset
   157
        
340
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents: 336
diff changeset
   158
            adder = LdtAnnotation(project)
332
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
   159
            logging.debug("request json " + repr(request.data))
424
a2f72b31811b Add french translations + small bugfixes
verrierj
parents: 420
diff changeset
   160
            
a2f72b31811b Add french translations + small bugfixes
verrierj
parents: 420
diff changeset
   161
            unprotect_models() # Allows anonymous user to modify models in this request only 
196
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 63
diff changeset
   162
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 63
diff changeset
   163
            meta = request.data['meta']
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 63
diff changeset
   164
            author = meta['creator']
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 63
diff changeset
   165
            date = meta['created']
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 63
diff changeset
   166
            new_annotations = request.data['annotations']
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 63
diff changeset
   167
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 63
diff changeset
   168
            for a in new_annotations:
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 63
diff changeset
   169
                dur = str(a['end'] - a['begin'])
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 63
diff changeset
   170
                begin = str(a['begin'])
388
454bd3bd6ffd Debug api for annotation creation in embed player.
cavaliet
parents: 344
diff changeset
   171
                type_id, new_id = adder.add(a['media'], a['type'], a['type_title'], a['content']['data'], '', a['tags'], begin, dur, author, date)
216
3dcd742acc13 API return ID of added annotations
verrierj
parents: 200
diff changeset
   172
                if not new_id:
424
a2f72b31811b Add french translations + small bugfixes
verrierj
parents: 420
diff changeset
   173
                    protect_models()
196
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 63
diff changeset
   174
                    return rc.BAD_REQUEST
340
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents: 336
diff changeset
   175
                
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents: 336
diff changeset
   176
                                
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents: 336
diff changeset
   177
                content = project.contents.get(iri_id=a['media'])
478
260f3438cb78 Fix bug when annotations are added in the metadataplayer
verrierj
parents: 426
diff changeset
   178
                add_annotation_to_stat(content, a['begin'], a['end'])
388
454bd3bd6ffd Debug api for annotation creation in embed player.
cavaliet
parents: 344
diff changeset
   179
                
454bd3bd6ffd Debug api for annotation creation in embed player.
cavaliet
parents: 344
diff changeset
   180
                # We update the ids
454bd3bd6ffd Debug api for annotation creation in embed player.
cavaliet
parents: 344
diff changeset
   181
                a['type'] = type_id
337
496bd82719e5 Little correction on project API.
cavaliet
parents: 336
diff changeset
   182
                a['id'] = new_id
218
3035dd9f1a9d Fix typo in documentation
verrierj
parents: 216
diff changeset
   183
            
337
496bd82719e5 Little correction on project API.
cavaliet
parents: 336
diff changeset
   184
            # We save if there were added annotation
496bd82719e5 Little correction on project API.
cavaliet
parents: 336
diff changeset
   185
            if len(new_annotations)>0 :
332
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
   186
                adder.save()
218
3035dd9f1a9d Fix typo in documentation
verrierj
parents: 216
diff changeset
   187
            
424
a2f72b31811b Add french translations + small bugfixes
verrierj
parents: 420
diff changeset
   188
            protect_models()
340
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents: 336
diff changeset
   189
            
216
3dcd742acc13 API return ID of added annotations
verrierj
parents: 200
diff changeset
   190
            return request.data
196
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 63
diff changeset
   191
            
200
a38c4759ce8a Changed API handler for XML files back to its original behavior
verrierj
parents: 199
diff changeset
   192
        else:
a38c4759ce8a Changed API handler for XML files back to its original behavior
verrierj
parents: 199
diff changeset
   193
            logging.debug("request " + repr(request))
a38c4759ce8a Changed API handler for XML files back to its original behavior
verrierj
parents: 199
diff changeset
   194
            data = request.data
332
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
   195
            ldt_str = data["ldt"]
196
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 63
diff changeset
   196
            
200
a38c4759ce8a Changed API handler for XML files back to its original behavior
verrierj
parents: 199
diff changeset
   197
            logging.debug("request data" + repr(ldt_str))
196
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 63
diff changeset
   198
            
200
a38c4759ce8a Changed API handler for XML files back to its original behavior
verrierj
parents: 199
diff changeset
   199
            if not ldt_str:
332
c28d4dc49a50 add API interface to add annotation, with an ajax example in the comment.
cavaliet
parents: 218
diff changeset
   200
                return rc.BAD_REQUEST
200
a38c4759ce8a Changed API handler for XML files back to its original behavior
verrierj
parents: 199
diff changeset
   201
            
a38c4759ce8a Changed API handler for XML files back to its original behavior
verrierj
parents: 199
diff changeset
   202
            project = Project.objects.get(ldt_id=project_id)
a38c4759ce8a Changed API handler for XML files back to its original behavior
verrierj
parents: 199
diff changeset
   203
            
a38c4759ce8a Changed API handler for XML files back to its original behavior
verrierj
parents: 199
diff changeset
   204
            project.ldt = ldt_str
a38c4759ce8a Changed API handler for XML files back to its original behavior
verrierj
parents: 199
diff changeset
   205
            
424
a2f72b31811b Add french translations + small bugfixes
verrierj
parents: 420
diff changeset
   206
            unprotect_models()
200
a38c4759ce8a Changed API handler for XML files back to its original behavior
verrierj
parents: 199
diff changeset
   207
            project.save()
424
a2f72b31811b Add french translations + small bugfixes
verrierj
parents: 420
diff changeset
   208
            protect_models()
196
b939a58d13b0 Moved code to add annotation to ldt_utils + added tests
verrierj
parents: 63
diff changeset
   209
        
13
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
            return rc.ALL_OK
340
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents: 336
diff changeset
   211
        
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents: 336
diff changeset
   212
        
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents: 336
diff changeset
   213
class ContentHandler(BaseHandler):
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents: 336
diff changeset
   214
    allowed_methods = ('GET', )
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents: 336
diff changeset
   215
    model = Content
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents: 336
diff changeset
   216
    exclude = (
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents: 336
diff changeset
   217
               ("media_obj"),
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents: 336
diff changeset
   218
               )   
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents: 336
diff changeset
   219
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents: 336
diff changeset
   220
    def read(self, request, iri_id):
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents: 336
diff changeset
   221
        """
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents: 336
diff changeset
   222
        returns a single content
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents: 336
diff changeset
   223
        """
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents: 336
diff changeset
   224
        return Content.objects.get(iri_id=iri_id)
5f919a978f50 Stats for annotations volume can be computed using ./manage.py statannotation [-c content_id] or in the admin pages of module ldt_utils.
verrierj
parents: 336
diff changeset
   225
    
412
8d777b1d1d92 Add API to search annotations inside a content between two timecodes
verrierj
parents: 388
diff changeset
   226
8d777b1d1d92 Add API to search annotations inside a content between two timecodes
verrierj
parents: 388
diff changeset
   227
class SegmentHandler(BaseHandler):
8d777b1d1d92 Add API to search annotations inside a content between two timecodes
verrierj
parents: 388
diff changeset
   228
    allowed_methods = ('GET', )
8d777b1d1d92 Add API to search annotations inside a content between two timecodes
verrierj
parents: 388
diff changeset
   229
    model = Segment
8d777b1d1d92 Add API to search annotations inside a content between two timecodes
verrierj
parents: 388
diff changeset
   230
    exclude = (
8d777b1d1d92 Add API to search annotations inside a content between two timecodes
verrierj
parents: 388
diff changeset
   231
               ("project_obj"),
8d777b1d1d92 Add API to search annotations inside a content between two timecodes
verrierj
parents: 388
diff changeset
   232
               ("content"),
8d777b1d1d92 Add API to search annotations inside a content between two timecodes
verrierj
parents: 388
diff changeset
   233
               )
8d777b1d1d92 Add API to search annotations inside a content between two timecodes
verrierj
parents: 388
diff changeset
   234
    
8d777b1d1d92 Add API to search annotations inside a content between two timecodes
verrierj
parents: 388
diff changeset
   235
    def read(self, request, iri_id, begin, end):
8d777b1d1d92 Add API to search annotations inside a content between two timecodes
verrierj
parents: 388
diff changeset
   236
        """
426
4328cd4a7533 refactor modal windows + add translations
verrierj
parents: 424
diff changeset
   237
        returns segments about content iri_id between timecodes begin and end
412
8d777b1d1d92 Add API to search annotations inside a content between two timecodes
verrierj
parents: 388
diff changeset
   238
        """
485
9ee8cc15e244 Add setting to metadataplayer for API url
verrierj
parents: 482
diff changeset
   239
        begin = int(begin)
9ee8cc15e244 Add setting to metadataplayer for API url
verrierj
parents: 482
diff changeset
   240
        end = int(end)
412
8d777b1d1d92 Add API to search annotations inside a content between two timecodes
verrierj
parents: 388
diff changeset
   241
        
8d777b1d1d92 Add API to search annotations inside a content between two timecodes
verrierj
parents: 388
diff changeset
   242
        content = Content.objects.get(iri_id=iri_id)
8d777b1d1d92 Add API to search annotations inside a content between two timecodes
verrierj
parents: 388
diff changeset
   243
        segments = Segment.objects.filter(content=content).filter(
426
4328cd4a7533 refactor modal windows + add translations
verrierj
parents: 424
diff changeset
   244
                    Q(start_ts__gt=begin, start_ts__lt=end) |                            # segment starts between begin and end
412
8d777b1d1d92 Add API to search annotations inside a content between two timecodes
verrierj
parents: 388
diff changeset
   245
                    Q(start_ts__gt=begin-F('duration'), start_ts__lt=end-F('duration')) |# segment ends between begin and end
8d777b1d1d92 Add API to search annotations inside a content between two timecodes
verrierj
parents: 388
diff changeset
   246
                    Q(start_ts__lt=begin, start_ts__gt=end-F('duration'))                # period [begin:end] is included in the segment
8d777b1d1d92 Add API to search annotations inside a content between two timecodes
verrierj
parents: 388
diff changeset
   247
                    )
8d777b1d1d92 Add API to search annotations inside a content between two timecodes
verrierj
parents: 388
diff changeset
   248
        
482
c802e00c7131 Lot of bugfixes
verrierj
parents: 478
diff changeset
   249
        a = SegmentSerializer(content, segments)
412
8d777b1d1d92 Add API to search annotations inside a content between two timecodes
verrierj
parents: 388
diff changeset
   250
        return a.serialize_to_cinelab()
8d777b1d1d92 Add API to search annotations inside a content between two timecodes
verrierj
parents: 388
diff changeset
   251