src/ldt/ldt/ldt_utils/projectserializer.py
author veltr
Fri, 16 Nov 2012 18:08:48 +0100
changeset 956 295c5613698f
parent 934 196f98b4bf98
child 1093 03104d3f6ca1
permissions -rw-r--r--
Another Widget Bugfix
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
366
5bd7937ea1d7 Add view to select first cutting in json
verrierj
parents: 195
diff changeset
     1
from django.conf import settings
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
from datetime import datetime
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
from django.utils.datastructures import SortedDict
19
d5ffd0b5bf0d correct project json export
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
     4
from ldt.ldt_utils.models import Content
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
from ldt.ldt_utils.utils import reduce_text_node
195
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
     6
from ldt.ldt_utils.models import User, Project
479
1cbfcfcb2a47 Objects serializer send correct stats
verrierj
parents: 427
diff changeset
     7
from ldt.ldt_utils.stat import get_string_from_buckets
482
c802e00c7131 Lot of bugfixes
verrierj
parents: 479
diff changeset
     8
from ldt.security.utils import use_forbidden_url
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
import logging
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
import lxml.etree
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
import uuid
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
13
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    13
DATE_FORMATS = ["%d/%m/%Y", "%Y-%m-%d"]
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
"""
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
Serialize a project object to a cinelab compatible array
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
"""
886
3ccedd271e36 add to_cinelab serializer to api
cavaliet
parents: 810
diff changeset
    19
class ProjectJsonSerializer:
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
    
482
c802e00c7131 Lot of bugfixes
verrierj
parents: 479
diff changeset
    21
    def __init__(self, project, from_contents=True, from_display=True, first_cutting=None, only_one_cutting=False):
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
        self.project = project
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
        self.parsed = False
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
        self.ldt_doc = None
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
        self.medias_dict = SortedDict()
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
        self.annotations_dict = SortedDict()
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
        self.annotations_by_annotation_types = {}
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
        self.tags = {}
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
        self.tags_dict = SortedDict()
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
        self.annotation_types_dict = SortedDict()
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
        self.views_dict = SortedDict()
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
        self.lists_dict = SortedDict()
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
        self.serialize_contents = from_contents
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
        self.from_display = from_display
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
        self.display_contents_list = []
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
        self.display_cuttings_list = []
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
        self.display_ensemble_list = []
366
5bd7937ea1d7 Add view to select first cutting in json
verrierj
parents: 195
diff changeset
    38
        self.first_cutting = first_cutting
404
4adc42ab55fd Fix minor bugs
verrierj
parents: 385
diff changeset
    39
        self.only_one_cutting = only_one_cutting
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    40
        
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    41
        
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    42
    def __parse_views(self, display_node_list):
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    43
        for display_node in display_node_list:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    44
            display_id = display_node.get(u"id", None)
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    45
            if not display_id:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    46
                continue
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    47
            content_list = []
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
            cuttings_list = []
366
5bd7937ea1d7 Add view to select first cutting in json
verrierj
parents: 195
diff changeset
    49
            
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
            new_display = {
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
                "id": display_id,
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
                "contents": content_list,
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
                "annotation_types": cuttings_list,
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
            }
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
            
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
            for content_node in display_node.xpath("content"):
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
                content_id = content_node.get("id")
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    58
                if content_id not in content_list:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
                    content_list.append(content_id)                    
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
                if content_id not in self.display_contents_list:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
                    self.display_contents_list.append(content_id)
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
                for cutting_node  in content_node.xpath("decoupage"):
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
                    cutting_id = cutting_node.get("id")
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
                    if cutting_id not in cuttings_list:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
                        cuttings_list.append(cutting_id)
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
                    if cutting_id not in self.display_cuttings_list:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
                        self.display_cuttings_list.append(cutting_id)
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
                    ensemble_id = cutting_node.get("idens")
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
                    if ensemble_id not in self.display_ensemble_list:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
                        self.display_ensemble_list.append(ensemble_id)
366
5bd7937ea1d7 Add view to select first cutting in json
verrierj
parents: 195
diff changeset
    71
            
532
155456f99f3d Projectserializer can add an extra annotation-type and its annotations to the serialization of a view
verrierj
parents: 482
diff changeset
    72
            
366
5bd7937ea1d7 Add view to select first cutting in json
verrierj
parents: 195
diff changeset
    73
            # sets cutting to display in first position for the metadataplayer
5bd7937ea1d7 Add view to select first cutting in json
verrierj
parents: 195
diff changeset
    74
            if self.first_cutting:
5bd7937ea1d7 Add view to select first cutting in json
verrierj
parents: 195
diff changeset
    75
                
5bd7937ea1d7 Add view to select first cutting in json
verrierj
parents: 195
diff changeset
    76
                annotation_types = new_display['annotation_types']
532
155456f99f3d Projectserializer can add an extra annotation-type and its annotations to the serialization of a view
verrierj
parents: 482
diff changeset
    77
                    
155456f99f3d Projectserializer can add an extra annotation-type and its annotations to the serialization of a view
verrierj
parents: 482
diff changeset
    78
                if self.first_cutting not in annotation_types:
155456f99f3d Projectserializer can add an extra annotation-type and its annotations to the serialization of a view
verrierj
parents: 482
diff changeset
    79
                    annotation_types.append(self.first_cutting)
155456f99f3d Projectserializer can add an extra annotation-type and its annotations to the serialization of a view
verrierj
parents: 482
diff changeset
    80
                    
155456f99f3d Projectserializer can add an extra annotation-type and its annotations to the serialization of a view
verrierj
parents: 482
diff changeset
    81
                index = -1
155456f99f3d Projectserializer can add an extra annotation-type and its annotations to the serialization of a view
verrierj
parents: 482
diff changeset
    82
                for i, s in enumerate(annotation_types):
155456f99f3d Projectserializer can add an extra annotation-type and its annotations to the serialization of a view
verrierj
parents: 482
diff changeset
    83
                    if s == self.first_cutting: 
155456f99f3d Projectserializer can add an extra annotation-type and its annotations to the serialization of a view
verrierj
parents: 482
diff changeset
    84
                        index = i
155456f99f3d Projectserializer can add an extra annotation-type and its annotations to the serialization of a view
verrierj
parents: 482
diff changeset
    85
                        break
155456f99f3d Projectserializer can add an extra annotation-type and its annotations to the serialization of a view
verrierj
parents: 482
diff changeset
    86
                        
155456f99f3d Projectserializer can add an extra annotation-type and its annotations to the serialization of a view
verrierj
parents: 482
diff changeset
    87
                annotation_types[0], annotation_types[index] = annotation_types[index], annotation_types[0]                        
382
123b89cf599e Search results are clickable
verrierj
parents: 366
diff changeset
    88
            
404
4adc42ab55fd Fix minor bugs
verrierj
parents: 385
diff changeset
    89
            if self.only_one_cutting:
4adc42ab55fd Fix minor bugs
verrierj
parents: 385
diff changeset
    90
                new_display['annotation_types'] = [new_display['annotation_types'][0]] 
532
155456f99f3d Projectserializer can add an extra annotation-type and its annotations to the serialization of a view
verrierj
parents: 482
diff changeset
    91
            
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
            self.views_dict[display_id] = new_display
366
5bd7937ea1d7 Add view to select first cutting in json
verrierj
parents: 195
diff changeset
    93
            
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
    
532
155456f99f3d Projectserializer can add an extra annotation-type and its annotations to the serialization of a view
verrierj
parents: 482
diff changeset
    95
    def __parse_ensemble(self, ensemble_node, content, cutting_only=None):
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
        
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
        ensemble_id = ensemble_node.attrib[u"id"]
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
        ensemble_author = ensemble_node.attrib[u"author"]
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
        ensemble_title = ensemble_node.attrib[u"title"]
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
        ensemble_description = ensemble_node.attrib[u"abstract"]
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
        ensemble_created = datetime.utcnow().isoformat()
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
        ensemble_modified = ensemble_created 
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
        
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
        list_items = []
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
        new_list = {
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
            "id" : ensemble_id,
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
            "items" : list_items,
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
            "meta" : {
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
                "dc:creator":ensemble_author,
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
                "dc:created": ensemble_created,
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
                "dc:contributor":"undefined",
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
                "dc:modified": ensemble_modified,
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
                "dc:title":ensemble_title,
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
                "dc:description": ensemble_description,
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
                "id-ref":content.iri_id,
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
                "editable":"false"
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
            }
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
        }
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
        
532
155456f99f3d Projectserializer can add an extra annotation-type and its annotations to the serialization of a view
verrierj
parents: 482
diff changeset
   120
        if cutting_only:
155456f99f3d Projectserializer can add an extra annotation-type and its annotations to the serialization of a view
verrierj
parents: 482
diff changeset
   121
            cuttings_list = cutting_only
155456f99f3d Projectserializer can add an extra annotation-type and its annotations to the serialization of a view
verrierj
parents: 482
diff changeset
   122
        else:
155456f99f3d Projectserializer can add an extra annotation-type and its annotations to the serialization of a view
verrierj
parents: 482
diff changeset
   123
            cuttings_list = ensemble_node             
155456f99f3d Projectserializer can add an extra annotation-type and its annotations to the serialization of a view
verrierj
parents: 482
diff changeset
   124
                
155456f99f3d Projectserializer can add an extra annotation-type and its annotations to the serialization of a view
verrierj
parents: 482
diff changeset
   125
        for decoupage_node in cuttings_list:            
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
            if decoupage_node.tag != "decoupage" :
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
                continue
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
            
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
            decoupage_id = decoupage_node.attrib[ u"id"]
532
155456f99f3d Projectserializer can add an extra annotation-type and its annotations to the serialization of a view
verrierj
parents: 482
diff changeset
   130
            if not cutting_only and self.from_display and decoupage_id not in self.display_cuttings_list:
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
                continue
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
            decoupage_creator = decoupage_node.attrib[u"author"]
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
            if not decoupage_creator:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
                decoupage_creator = "IRI"
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
            decoupage_contributor = decoupage_creator
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
            date_str = decoupage_node.get(u"date")
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
            decoupage_created = None
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
            if date_str :
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
                for date_format in DATE_FORMATS:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
                    try:
13
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   141
                        decoupage_created = datetime.strptime(date_str, date_format).isoformat()
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
                        break
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
                    except Exception:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
                        decoupage_created = None
532
155456f99f3d Projectserializer can add an extra annotation-type and its annotations to the serialization of a view
verrierj
parents: 482
diff changeset
   145
                                            
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
            if decoupage_created is None:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
                decoupage_created = datetime.utcnow().isoformat()
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
            decoupage_modified = decoupage_created
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
            
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
            decoupage_title = ""
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
            for txtRes in decoupage_node.xpath("title/text()", smart_strings=False): 
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
                    decoupage_title += txtRes
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
            decoupage_description = ""
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
            for txtRes in decoupage_node.xpath("abstract/text()", smart_strings=False): 
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
                    decoupage_description += txtRes
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
            list_items.append({"id-ref":decoupage_id})
532
155456f99f3d Projectserializer can add an extra annotation-type and its annotations to the serialization of a view
verrierj
parents: 482
diff changeset
   159
                        
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
            new_annotation_types = {
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
                "id":decoupage_id,
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
                "dc:creator":decoupage_creator,
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
                "dc:created":decoupage_created,
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
                "dc:contributor":decoupage_contributor,
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
                "dc:modified":decoupage_modified,
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
                "dc:title":decoupage_title,
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
                "dc:description":decoupage_description
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
            }
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
            
532
155456f99f3d Projectserializer can add an extra annotation-type and its annotations to the serialization of a view
verrierj
parents: 482
diff changeset
   170
            self.annotation_types_dict[decoupage_id] = new_annotation_types                
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
            self.annotations_by_annotation_types[decoupage_id] = []
532
155456f99f3d Projectserializer can add an extra annotation-type and its annotations to the serialization of a view
verrierj
parents: 482
diff changeset
   172
                      
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
            res = decoupage_node.xpath("elements/element")
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
            for element_node in res:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
                
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
                element_id = element_node.attrib[u"id"]
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
                element_begin = element_node.attrib[u"begin"]
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
                element_duration = element_node.attrib[u"dur"]
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
                element_media = content.iri_id
382
123b89cf599e Search results are clickable
verrierj
parents: 366
diff changeset
   180
                element_color = element_node.attrib.get(u"color", "")
52
83c5a6907714 update cinelab serialization
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   181
                element_ldt_src = element_node.attrib.get(u"src", "")
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
                
13
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   183
                element_title = reduce_text_node(element_node, "title/text()")        
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
                element_description = reduce_text_node(element_node, "abstract/text()")                
19
d5ffd0b5bf0d correct project json export
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   185
                
d5ffd0b5bf0d correct project json export
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   186
                element_source_node_list = element_node.xpath("meta/source")
d5ffd0b5bf0d correct project json export
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   187
                
d5ffd0b5bf0d correct project json export
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   188
                if len(element_source_node_list) > 0:
d5ffd0b5bf0d correct project json export
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   189
                    element_source_node = element_source_node_list[0]
d5ffd0b5bf0d correct project json export
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   190
                    element_source = {"mimetype" :element_source_node.get(u'mimetype'), "url":element_source_node.get(u'url'), "content":reduce_text_node(element_source_node)}
d5ffd0b5bf0d correct project json export
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   191
                else:
d5ffd0b5bf0d correct project json export
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   192
                    element_source = None
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
                
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
                element_audio_src = ""
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
                element_audio_href = ""
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
                res = element_node.xpath("audio")
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
                if len(res) > 0:
13
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   198
                    element_audio_src = res[0].get(u"source", u"")
810
e7546394653c add audio annotation to segment api and correct reindex command.
cavaliet
parents: 713
diff changeset
   199
                    element_audio_href = res[0].text
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
                
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
                element_tags = []
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
                
13
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   203
                tags = element_node.get(u"tags", u"")
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
                
13
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   205
                tags_list = map(lambda s:s.strip(), tags.split(","))
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
                #tags                                
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
                if tags is None or len(tags) == 0:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
                    tags_list = []
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
                    restagnode = element_node.xpath("tag/text()", smart_strings=False)
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
                    for tagnode in restagnode:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
                        tags_list.append(tagnode)
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
                        
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
                if tags_list is None or len(tags_list) == 0:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
                    tags_list = []
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
                    restagnode = element_node.xpath("tags/tag/text()", smart_strings=False)
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
                    for tagnode in restagnode:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   218
                        tags_list.append(tagnode)
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
                
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
                tag_date = datetime.utcnow().isoformat()
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
                for tag_title in tags_list:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
                    if tag_title not in self.tags:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
                        tag_id = unicode(uuid.uuid1())
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
                        new_tag = {
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
                            "id":tag_id,
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
                            "meta" : {
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   227
                                "dc:creator":"IRI",
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
                                "dc:created": tag_date,
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
                                "dc:contributor":"IRI",
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
                                "dc:modified": tag_date,
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
                                "dc:title":tag_title
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   232
                            }
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
                        }
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
                        self.tags[tag_title] = new_tag
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
                        self.tags_dict[tag_id] = new_tag
426
4328cd4a7533 refactor modal windows + add translations
verrierj
parents: 420
diff changeset
   236
                    else:
4328cd4a7533 refactor modal windows + add translations
verrierj
parents: 420
diff changeset
   237
                        tag_id = self.tags[tag_title]["id"]
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
                    element_tags.append({"id-ref":tag_id})
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
                if not element_tags:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
                    element_tags = None
934
196f98b4bf98 little serializer debug.
cavaliet
parents: 886
diff changeset
   242
                
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
                new_annotation = {
934
196f98b4bf98 little serializer debug.
cavaliet
parents: 886
diff changeset
   244
                    "begin": int(float(element_begin)),
196f98b4bf98 little serializer debug.
cavaliet
parents: 886
diff changeset
   245
                    "end": int(float(element_begin)) + int(float(element_duration)),
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
                    "id": element_id,
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
                    "media": element_media,
52
83c5a6907714 update cinelab serialization
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   248
                    "color": element_color,
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
                    "content": {
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   250
                        "mimetype": "application/x-ldt-structured",
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
                        "title": element_title,
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   252
                        "description": element_description,
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
                        "color": element_color,
52
83c5a6907714 update cinelab serialization
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   254
                        "img": {
63
93325a5d61f0 organize format and import
ymh <ymh.work@gmail.com>
parents: 52
diff changeset
   255
                            "src": element_ldt_src,
52
83c5a6907714 update cinelab serialization
ymh <ymh.work@gmail.com>
parents: 19
diff changeset
   256
                        },
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
                        "audio": {
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   258
                            "src" : element_audio_src,
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   259
                            "mimetype": "audio/mp3",
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
                            "href": element_audio_href
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
                        },
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   262
                        "polemics" :[pol_elem.text for pol_elem in element_node.xpath("meta/polemics/polemic")],
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
                    },
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
                    "tags": element_tags,
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
                    "meta": {
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   266
                        "id-ref": decoupage_id,
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
                        "dc:creator": decoupage_creator,
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
                        "dc:contributor": decoupage_contributor,
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   269
                        "dc:created": decoupage_created,
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   270
                        "dc:modified": decoupage_modified,
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   271
                    }
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   272
                }
532
155456f99f3d Projectserializer can add an extra annotation-type and its annotations to the serialization of a view
verrierj
parents: 482
diff changeset
   273
                               
19
d5ffd0b5bf0d correct project json export
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   274
                if element_source:
d5ffd0b5bf0d correct project json export
ymh <ymh.work@gmail.com>
parents: 13
diff changeset
   275
                    new_annotation['meta']['dc:source'] = element_source
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   276
                
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   277
                self.annotations_dict[element_id] = new_annotation
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   278
                self.annotations_by_annotation_types[decoupage_id].append(new_annotation)
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   279
        
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   280
        if not list_items:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   281
            new_list["items"] = None
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   282
        self.lists_dict[ensemble_id] = new_list
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   283
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   284
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   285
    def __parse_ldt(self):
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   286
        
560
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 532
diff changeset
   287
        self.ldt_doc = lxml.etree.fromstring(self.project.ldt_encoded)
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   288
        
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   289
        if self.from_display:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   290
            xpath_str = "/iri/displays/display[position()=1]"
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   291
            if isinstance(self.from_display, basestring):
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   292
                xpath_str = "/iri/displays/display[@id='%s']" % self.from_display
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   293
            
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   294
            self.__parse_views(self.ldt_doc.xpath(xpath_str))
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   295
        
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   296
        res = self.ldt_doc.xpath("/iri/medias/media")
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   297
        for mediaNode in res:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   298
            iri_id = mediaNode.attrib[u"id"]
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   299
            if self.from_display and iri_id not in self.display_contents_list:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   300
                continue
63
93325a5d61f0 organize format and import
ymh <ymh.work@gmail.com>
parents: 52
diff changeset
   301
            content = Content.objects.get(iri_id=iri_id) #@UndefinedVariable
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   302
            self.__parse_content(content)
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   303
            
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   304
        res = self.ldt_doc.xpath("/iri/annotations/content")
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   305
        for content_node in res:
713
202fb1255d8b first step for mashup json.
cavaliet
parents: 691
diff changeset
   306
            content_id = content_node.attrib[u"id"]
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   307
            if self.from_display and content_id not in self.display_contents_list:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   308
                continue
63
93325a5d61f0 organize format and import
ymh <ymh.work@gmail.com>
parents: 52
diff changeset
   309
            content = Content.objects.get(iri_id=content_id) #@UndefinedVariable
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   310
            for ensemble_node in content_node:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   311
                if ensemble_node.tag != "ensemble" :
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   312
                    continue
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   313
                ensemble_id = ensemble_node.get("id")
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   314
                if self.from_display and ensemble_id not in self.display_ensemble_list:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   315
                    continue
532
155456f99f3d Projectserializer can add an extra annotation-type and its annotations to the serialization of a view
verrierj
parents: 482
diff changeset
   316
                self.__parse_ensemble(ensemble_node, content)
155456f99f3d Projectserializer can add an extra annotation-type and its annotations to the serialization of a view
verrierj
parents: 482
diff changeset
   317
                
155456f99f3d Projectserializer can add an extra annotation-type and its annotations to the serialization of a view
verrierj
parents: 482
diff changeset
   318
        if self.first_cutting and self.first_cutting not in self.display_cuttings_list:
155456f99f3d Projectserializer can add an extra annotation-type and its annotations to the serialization of a view
verrierj
parents: 482
diff changeset
   319
            cutting_node= self.ldt_doc.xpath('/iri/annotations/content/ensemble/decoupage[@id=\'%s\']' % self.first_cutting)[0]
155456f99f3d Projectserializer can add an extra annotation-type and its annotations to the serialization of a view
verrierj
parents: 482
diff changeset
   320
            ensemble_node = cutting_node.xpath('..')[0]
713
202fb1255d8b first step for mashup json.
cavaliet
parents: 691
diff changeset
   321
            content_node = ensemble_node.xpath('..')[0]
532
155456f99f3d Projectserializer can add an extra annotation-type and its annotations to the serialization of a view
verrierj
parents: 482
diff changeset
   322
            iri_id = content_node.get("id")
934
196f98b4bf98 little serializer debug.
cavaliet
parents: 886
diff changeset
   323
            content = Content.objects.get(iri_id=iri_id)
532
155456f99f3d Projectserializer can add an extra annotation-type and its annotations to the serialization of a view
verrierj
parents: 482
diff changeset
   324
            self.__parse_ensemble(ensemble_node, content, cutting_only=[cutting_node])
155456f99f3d Projectserializer can add an extra annotation-type and its annotations to the serialization of a view
verrierj
parents: 482
diff changeset
   325
            
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   326
        
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   327
        #reorder annotations and annotation type from view
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   328
        if self.from_display and len(self.views_dict) > 0:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   329
            new_annotation_types_dict = SortedDict()
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   330
            new_annotations_dict = SortedDict()
532
155456f99f3d Projectserializer can add an extra annotation-type and its annotations to the serialization of a view
verrierj
parents: 482
diff changeset
   331
            for annotation_type in self.display_cuttings_list + [self.first_cutting]:
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   332
                if annotation_type in self.annotation_types_dict:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   333
                    new_annotation_types_dict[annotation_type] = self.annotation_types_dict[annotation_type]
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   334
                    for annot in self.annotations_by_annotation_types[annotation_type]:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   335
                        new_annotations_dict[annot['id']] = annot
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   336
                    
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   337
            self.annotations_dict = new_annotations_dict
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   338
            self.annotation_types_dict = new_annotation_types_dict
691
ff0b66633807 update project serializer for it to serialize the first bout à bout of a ldt project.
cavaliet
parents: 572
diff changeset
   339
            
ff0b66633807 update project serializer for it to serialize the first bout à bout of a ldt project.
cavaliet
parents: 572
diff changeset
   340
        # We add the first "bout a bout". It is an "edit" in ldt format and list/listtype="mashup" in json format
ff0b66633807 update project serializer for it to serialize the first bout à bout of a ldt project.
cavaliet
parents: 572
diff changeset
   341
        self.__parse_edits()
532
155456f99f3d Projectserializer can add an extra annotation-type and its annotations to the serialization of a view
verrierj
parents: 482
diff changeset
   342
                               
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   343
        self.parsed = True
691
ff0b66633807 update project serializer for it to serialize the first bout à bout of a ldt project.
cavaliet
parents: 572
diff changeset
   344
        
ff0b66633807 update project serializer for it to serialize the first bout à bout of a ldt project.
cavaliet
parents: 572
diff changeset
   345
    
ff0b66633807 update project serializer for it to serialize the first bout à bout of a ldt project.
cavaliet
parents: 572
diff changeset
   346
    def __parse_edits(self):
ff0b66633807 update project serializer for it to serialize the first bout à bout of a ldt project.
cavaliet
parents: 572
diff changeset
   347
        editings = self.ldt_doc.xpath("/iri/edits/editing")
ff0b66633807 update project serializer for it to serialize the first bout à bout of a ldt project.
cavaliet
parents: 572
diff changeset
   348
        if not editings:
ff0b66633807 update project serializer for it to serialize the first bout à bout of a ldt project.
cavaliet
parents: 572
diff changeset
   349
            return False
ff0b66633807 update project serializer for it to serialize the first bout à bout of a ldt project.
cavaliet
parents: 572
diff changeset
   350
        editing = self.ldt_doc.xpath("/iri/edits/editing[position()=1]")[0]
ff0b66633807 update project serializer for it to serialize the first bout à bout of a ldt project.
cavaliet
parents: 572
diff changeset
   351
        e_id = editing.get("id");
ff0b66633807 update project serializer for it to serialize the first bout à bout of a ldt project.
cavaliet
parents: 572
diff changeset
   352
        eList = editing.xpath("edit[position()=1]/eList")[0]
ff0b66633807 update project serializer for it to serialize the first bout à bout of a ldt project.
cavaliet
parents: 572
diff changeset
   353
        d = datetime.utcnow().isoformat()
ff0b66633807 update project serializer for it to serialize the first bout à bout of a ldt project.
cavaliet
parents: 572
diff changeset
   354
        e_title = ""
ff0b66633807 update project serializer for it to serialize the first bout à bout of a ldt project.
cavaliet
parents: 572
diff changeset
   355
        for txtRes in editing.xpath("title/text()", smart_strings=False): 
ff0b66633807 update project serializer for it to serialize the first bout à bout of a ldt project.
cavaliet
parents: 572
diff changeset
   356
            e_title += txtRes
ff0b66633807 update project serializer for it to serialize the first bout à bout of a ldt project.
cavaliet
parents: 572
diff changeset
   357
        e_description = ""
ff0b66633807 update project serializer for it to serialize the first bout à bout of a ldt project.
cavaliet
parents: 572
diff changeset
   358
        for txtRes in editing.xpath("abstract/text()", smart_strings=False): 
ff0b66633807 update project serializer for it to serialize the first bout à bout of a ldt project.
cavaliet
parents: 572
diff changeset
   359
            e_description += txtRes
ff0b66633807 update project serializer for it to serialize the first bout à bout of a ldt project.
cavaliet
parents: 572
diff changeset
   360
        list_items = []
ff0b66633807 update project serializer for it to serialize the first bout à bout of a ldt project.
cavaliet
parents: 572
diff changeset
   361
        for item in eList:
ff0b66633807 update project serializer for it to serialize the first bout à bout of a ldt project.
cavaliet
parents: 572
diff changeset
   362
            # ref is structured with contenated ids : id_content|;|id_ensemble|;|id_annot-type|;||;||;|id_SEG
ff0b66633807 update project serializer for it to serialize the first bout à bout of a ldt project.
cavaliet
parents: 572
diff changeset
   363
            ids = item.get("ref").split('|;|')
ff0b66633807 update project serializer for it to serialize the first bout à bout of a ldt project.
cavaliet
parents: 572
diff changeset
   364
            list_items.append(ids[-1])
ff0b66633807 update project serializer for it to serialize the first bout à bout of a ldt project.
cavaliet
parents: 572
diff changeset
   365
        # We build the jsonable object if necessary
ff0b66633807 update project serializer for it to serialize the first bout à bout of a ldt project.
cavaliet
parents: 572
diff changeset
   366
        if len(list_items)>0:
ff0b66633807 update project serializer for it to serialize the first bout à bout of a ldt project.
cavaliet
parents: 572
diff changeset
   367
            new_list = {
ff0b66633807 update project serializer for it to serialize the first bout à bout of a ldt project.
cavaliet
parents: 572
diff changeset
   368
                "id" : e_id,
ff0b66633807 update project serializer for it to serialize the first bout à bout of a ldt project.
cavaliet
parents: 572
diff changeset
   369
                "items" : list_items,
ff0b66633807 update project serializer for it to serialize the first bout à bout of a ldt project.
cavaliet
parents: 572
diff changeset
   370
                "meta" : {
ff0b66633807 update project serializer for it to serialize the first bout à bout of a ldt project.
cavaliet
parents: 572
diff changeset
   371
                    "dc:creator":"IRI",
ff0b66633807 update project serializer for it to serialize the first bout à bout of a ldt project.
cavaliet
parents: 572
diff changeset
   372
                    "dc:created": d,
ff0b66633807 update project serializer for it to serialize the first bout à bout of a ldt project.
cavaliet
parents: 572
diff changeset
   373
                    "dc:contributor":"IRI",
ff0b66633807 update project serializer for it to serialize the first bout à bout of a ldt project.
cavaliet
parents: 572
diff changeset
   374
                    "dc:modified": d,
ff0b66633807 update project serializer for it to serialize the first bout à bout of a ldt project.
cavaliet
parents: 572
diff changeset
   375
                    "dc:title": e_title,
ff0b66633807 update project serializer for it to serialize the first bout à bout of a ldt project.
cavaliet
parents: 572
diff changeset
   376
                    "dc:description": e_description,
ff0b66633807 update project serializer for it to serialize the first bout à bout of a ldt project.
cavaliet
parents: 572
diff changeset
   377
                    "listtype":"mashup"
ff0b66633807 update project serializer for it to serialize the first bout à bout of a ldt project.
cavaliet
parents: 572
diff changeset
   378
                }
ff0b66633807 update project serializer for it to serialize the first bout à bout of a ldt project.
cavaliet
parents: 572
diff changeset
   379
            }
ff0b66633807 update project serializer for it to serialize the first bout à bout of a ldt project.
cavaliet
parents: 572
diff changeset
   380
            self.lists_dict[e_id] = new_list
ff0b66633807 update project serializer for it to serialize the first bout à bout of a ldt project.
cavaliet
parents: 572
diff changeset
   381
        
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   382
    
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   383
    def __parse_content(self, content):
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   384
        
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   385
        doc = lxml.etree.parse(content.iri_file_path())
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   386
        
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   387
        authors = content.authors.all()
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   388
        
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   389
        if len(authors) > 0 :
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   390
            author = authors[0].handle
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   391
        else :
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   392
            author = "IRI"
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   393
        
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   394
        if len(authors) > 1 :
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   395
            contributor = authors[1].handle
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   396
        else :
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   397
            contributor = author
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   398
        
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   399
        content_author = ""
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   400
        
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   401
        res = doc.xpath("/iri/head/meta[@name='author']/@content")
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   402
        if len(res) > 0:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   403
            content_author = res[0]
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   404
        
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   405
        
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   406
        content_date = ""
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   407
        
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   408
        res = doc.xpath("/iri/head/meta[@name='date']/@content")
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   409
        if len(res) > 0:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   410
            content_date = res[0]
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   411
572
10c26c22b76a Replace href by url in JSON
verrierj
parents: 560
diff changeset
   412
        url = ""
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   413
        meta_item_value = ""
482
c802e00c7131 Lot of bugfixes
verrierj
parents: 479
diff changeset
   414
        
c802e00c7131 Lot of bugfixes
verrierj
parents: 479
diff changeset
   415
        if use_forbidden_url(content):   
572
10c26c22b76a Replace href by url in JSON
verrierj
parents: 560
diff changeset
   416
            url = settings.FORBIDDEN_STREAM_URL
366
5bd7937ea1d7 Add view to select first cutting in json
verrierj
parents: 195
diff changeset
   417
        elif content.videopath:
572
10c26c22b76a Replace href by url in JSON
verrierj
parents: 560
diff changeset
   418
            url = content.videopath.rstrip('/') + "/" + content.src
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   419
            meta_item_value = content.videopath.rstrip('/') + "/"
377
a1f9f7583925 The embed player can now play a youtube video.
cavaliet
parents: 366
diff changeset
   420
        else:
572
10c26c22b76a Replace href by url in JSON
verrierj
parents: 560
diff changeset
   421
            url = content.src
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   422
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   423
        new_media = {
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   424
             "http://advene.liris.cnrs.fr/ns/frame_of_reference/ms" : "o=0",
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   425
             "id" : content.iri_id,
572
10c26c22b76a Replace href by url in JSON
verrierj
parents: 560
diff changeset
   426
             "url" : url,
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   427
             "unit" : "ms",
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   428
             "origin" : "0",
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   429
             "meta": {
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   430
                 "dc:creator" : author,
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   431
                 "dc:created" : content.creation_date.isoformat(),
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   432
                 "dc:contributor" : contributor,
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   433
                 "dc:modified" : content.update_date.isoformat(),
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   434
                 "dc:creator.contents" : content_author,
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   435
                 "dc:created.contents" : content_date,
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   436
                 "dc:title" : content.title,
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   437
                 "dc:description" : content.description,
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   438
                 "dc:duration" : content.get_duration(),
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   439
                 "item": {
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   440
                     "name" : "streamer",
13
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   441
                     "value": meta_item_value,
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   442
                 },
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   443
             }
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   444
        }
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   445
        
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   446
        self.medias_dict[content.iri_id] = new_media
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   447
        
420
38e46b1d0bd3 Small bugfixes
verrierj
parents: 404
diff changeset
   448
        new_display = {
38e46b1d0bd3 Small bugfixes
verrierj
parents: 404
diff changeset
   449
            "id": "stat",
38e46b1d0bd3 Small bugfixes
verrierj
parents: 404
diff changeset
   450
            "contents": [content.iri_id],
38e46b1d0bd3 Small bugfixes
verrierj
parents: 404
diff changeset
   451
            "meta": {
479
1cbfcfcb2a47 Objects serializer send correct stats
verrierj
parents: 427
diff changeset
   452
                     "stat": get_string_from_buckets(content.annotation_volume),
420
38e46b1d0bd3 Small bugfixes
verrierj
parents: 404
diff changeset
   453
                     }
38e46b1d0bd3 Small bugfixes
verrierj
parents: 404
diff changeset
   454
        }
38e46b1d0bd3 Small bugfixes
verrierj
parents: 404
diff changeset
   455
        
38e46b1d0bd3 Small bugfixes
verrierj
parents: 404
diff changeset
   456
        self.views_dict['test'] = new_display
38e46b1d0bd3 Small bugfixes
verrierj
parents: 404
diff changeset
   457
        
934
196f98b4bf98 little serializer debug.
cavaliet
parents: 886
diff changeset
   458
        if self.serialize_contents:
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   459
            res = doc.xpath("/iri/body/ensembles/ensemble")
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   460
            for ensemble_node in res:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   461
                self.__parse_ensemble(ensemble_node, content)
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   462
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   463
    
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   464
    def serialize_to_cinelab(self):
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   465
    
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   466
        res = {}
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   467
        
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   468
        if not self.parsed:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   469
            self.__parse_ldt()    
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   470
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   471
        
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   472
        project_main_media = ""
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   473
        if len(self.medias_dict) > 0:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   474
            project_main_media = self.medias_dict.value_for_index(0)["id"]
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   475
        
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   476
        res['meta'] = {
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   477
             'id': self.project.ldt_id,
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   478
             'dc:created':self.project.creation_date.isoformat(),
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   479
             'dc:modified':self.project.modification_date.isoformat(),
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   480
             'dc:contributor':self.project.changed_by,
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   481
             'dc:creator':self.project.created_by,
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   482
             'dc:title':self.project.title,
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   483
             'dc:description':self.project.get_description(self.ldt_doc), # get from doc, parse ldt
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   484
             'main_media': {"id-ref":project_main_media}
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   485
            }
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   486
                
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   487
                    
13
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   488
        res['medias'] = self.medias_dict.values() if len(self.medias_dict) > 0 else None
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   489
        res['lists'] = self.lists_dict.values() if len(self.lists_dict) > 0 else None
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   490
        res['tags'] = self.tags.values() if len(self.tags) > 0 else None
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   491
        res['views'] = self.views_dict.values() if len(self.views_dict) > 0 else None
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   492
        
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   493
        res['annotation-types'] = self.annotation_types_dict.values() if len(self.annotation_types_dict) > 0 else None
382
123b89cf599e Search results are clickable
verrierj
parents: 366
diff changeset
   494
        res['annotations'] = self.annotations_dict.values() if len(self.annotations_dict) > 0 else None
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   495
        
382
123b89cf599e Search results are clickable
verrierj
parents: 366
diff changeset
   496
        return res 
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   497
    
167
fe00e7302efe Change class and functions names to follow PEP8 formatting standards
verrierj
parents: 132
diff changeset
   498
    def get_annotations(self, first_cutting=True):
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   499
        
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   500
        if not self.parsed:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   501
            self.__parse_ldt()
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   502
        
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   503
        annotations = []
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   504
        
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   505
        current_cutting = None
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   506
        uri = None
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   507
        for annot in self.annotations_dict.values():
63
93325a5d61f0 organize format and import
ymh <ymh.work@gmail.com>
parents: 52
diff changeset
   508
            logging.debug("current cutting" + repr(current_cutting) + " : annot " + annot['meta']['id-ref']) #@UndefinedVariable
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   509
            if first_cutting and current_cutting and current_cutting != annot['meta']['id-ref'] :
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   510
                break
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   511
            current_cutting = annot['meta']['id-ref']
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   512
            content_id = annot['media']
63
93325a5d61f0 organize format and import
ymh <ymh.work@gmail.com>
parents: 52
diff changeset
   513
            content = Content.objects.get(iri_id=content_id) #@UndefinedVariable
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   514
            if annot['tags']:
13
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   515
                tags_list = map(lambda tag_entry: self.tags_dict[tag_entry['id-ref']]['meta']['dc:title'], annot['tags'])
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   516
            else:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   517
                tags_list = []
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   518
            begin = int(annot['begin'])
13
97ab7b3191cf add api to update project, uses psiton
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   519
            duration = int(annot['end']) - begin
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   520
            if content.media_obj and content.media_obj.external_publication_url:
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   521
                uri = "%s#t=%d" % (content.media_obj.external_publication_url, begin)
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   522
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   523
        
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   524
            annotations.append({
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   525
                'begin': begin,
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   526
                'duration':duration,
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   527
                'title':annot['content']['title'],
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   528
                'desc':annot['content']['description'],
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   529
                'tags': tags_list,
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   530
                'id':annot['id'],
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   531
                'uri':uri
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   532
            })
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   533
            
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   534
        return annotations
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   535
195
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   536
"""
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   537
Quick and dirty converter from cinelab JSON to ldt format.
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   538
Does not support imports, mutliple medias, or media creation
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   539
"""   
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   540
class JsonCinelab2Ldt:
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   541
    
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   542
    def create_json(self, json):
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   543
            
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   544
        medias = json['medias']
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   545
        contentList = [] 
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   546
        for media in medias:
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   547
            c = Content.objects.get(iri_id=media['id'])
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   548
            if c != None:
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   549
                contentList.append(c)
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   550
            
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   551
        meta = json['meta']            
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   552
        creator = meta['creator']
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   553
        contributor = meta['contributor']
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   554
            
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   555
        user = User.objects.get(username=creator)
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   556
        project = Project.create_project(user, creator + '_' + contributor, contentList)
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   557
        project.changed_by = contributor
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   558
            
560
1cb2a4a573e1 correct annoations api handler + ldt encoding
ymh <ymh.work@gmail.com>
parents: 532
diff changeset
   559
        ldtdoc = lxml.etree.fromstring(project.ldt_encoded)
195
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   560
        element = ldtdoc.xpath('/iri/annotations')
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   561
            
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   562
        for media in contentList:
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   563
            content = lxml.etree.Element('content')
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   564
            content.set('id', media.iri_id)              
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   565
                      
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   566
        annotation_types = json['annotation_types']
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   567
        cuttings = {}
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   568
        if len(annotation_types) > 0:
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   569
            media = lxml.etree.SubElement(element[0], 'content')
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   570
            media.set('id', medias[0]['id'])
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   571
                
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   572
            ens = lxml.etree.SubElement(media, 'ensemble')
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   573
            ens.set('title', 'Decoupages personnels') 
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   574
            ens.set('idProject', project.ldt_id)
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   575
            ens.set('abstract', '')
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   576
            ens.set('id', 'g_' + str(uuid.uuid1()))
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   577
            
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   578
            for i in annotation_types:
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   579
                cutting_infos = {'desc' : i['meta']['description']}
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   580
                
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   581
                dec = lxml.etree.SubElement(ens, 'decoupage') 
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   582
                dec.set('author', contributor)
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   583
                dec.set('id', 'c_' + str(uuid.uuid1()))
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   584
                elements_list = lxml.etree.SubElement(dec, 'elements')
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   585
                    
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   586
                title = lxml.etree.SubElement(dec, 'title')
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   587
                title.text = i['id']
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   588
                
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   589
                abstract = lxml.etree.SubElement(dec, 'abstract')
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   590
                abstract.text = i['meta']['description']
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   591
                
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   592
                cutting_infos['xml_node'] = elements_list
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   593
                cuttings[i['id']] = cutting_infos 
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   594
                   
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   595
                 
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   596
        annotations = json['annotations']            
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   597
        for i in annotations:
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   598
            cutting_infos = cuttings[i['type']]
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   599
            elements_node = cutting_infos['xml_node']
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   600
            element = lxml.etree.SubElement(elements_node, 'element')
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   601
            
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   602
            element.set('begin', str(i['begin']))
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   603
            element.set('dur', str(i['end'] - i['begin']))
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   604
            element.set('id', 's_' + str(uuid.uuid1()))
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   605
            
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   606
            title = lxml.etree.SubElement(element, 'title')
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   607
            audio = lxml.etree.SubElement(element, 'audio')
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   608
            audio.set('source', 'undefined')
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   609
            abstract = lxml.etree.SubElement(element, 'abstract')
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   610
            abstract.text = i['content']['data']
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   611
            tags = lxml.etree.SubElement(element, 'tags')
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   612
            for tag in i['tags']:
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   613
                tag_xml = lxml.etree.SubElement(tags, 'tag')
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   614
                tag_xml.text = tag
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   615
                    
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   616
            
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   617
        project.ldt = lxml.etree.tostring(ldtdoc, pretty_print=True)
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   618
        project.save()
0
bdf22b140727 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   619
        
195
6580f5804006 Added converter from json cinelab to ldt
verrierj
parents: 167
diff changeset
   620
        return project.ldt