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