web/ldt/ldt_utils/projectserializer.py
author ymh <ymh.work@gmail.com>
Tue, 19 Oct 2010 15:21:07 +0200
changeset 99 0fb4b009c6eb
parent 98 c9460033138f
parent 95 9bae869b2146
child 100 c5514a2bfdcd
permissions -rw-r--r--
Merge with a8eeb7253e58a28783abbf4362d9bed42a3271f4
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
from datetime import datetime
97
66f6aff5c382 corrections on project serialize and update project
ymh <ymh.work@gmail.com>
parents: 89
diff changeset
     2
from django.utils.datastructures import SortedDict
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
from ldt.ldt_utils.models import Content, Project
98
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
     4
import logging
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
     5
import lxml.etree
24
1296cc9b23d5 correct id generation
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
     6
import uuid
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
DATE_FORMATS = ["%d/%m/%Y","%Y-%m-%d"]
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
"""
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
Serialize a project object to a cinelab compatible array
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
"""
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
class ProjectSerializer:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
    
88
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    15
    def __init__(self, project, from_contents=True, from_display=True):
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
        self.project = project
88
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    17
        self.parsed = False
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
        self.ldt_doc = None
97
66f6aff5c382 corrections on project serialize and update project
ymh <ymh.work@gmail.com>
parents: 89
diff changeset
    19
        self.medias_dict = SortedDict()
66f6aff5c382 corrections on project serialize and update project
ymh <ymh.work@gmail.com>
parents: 89
diff changeset
    20
        self.annotations_dict = SortedDict()
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
        self.tags = {}
98
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    22
        self.tags_dict = SortedDict()
97
66f6aff5c382 corrections on project serialize and update project
ymh <ymh.work@gmail.com>
parents: 89
diff changeset
    23
        self.annotation_types_dict = SortedDict()
98
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    24
        self.views_dict = SortedDict()
97
66f6aff5c382 corrections on project serialize and update project
ymh <ymh.work@gmail.com>
parents: 89
diff changeset
    25
        self.lists_dict = SortedDict()
88
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    26
        self.serialize_contents = from_contents
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    27
        self.from_display = from_display
98
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    28
        self.display_contents_list = []
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    29
        self.display_cuttings_list = []
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    30
        self.display_ensemble_list = []
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    31
        
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    32
        
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    33
    def __parse_views(self, display_node_list):
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    34
        for display_node in display_node_list:
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    35
            display_id = display_node.get(u"id", None)
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    36
            if not display_id:
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    37
                continue
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    38
            content_list = []
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    39
            cuttings_list = []
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    40
            new_display = {
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    41
                "id": display_id,
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    42
                "contents": content_list,
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    43
                "annotation_types": cuttings_list,
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    44
            }
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    45
            
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    46
            for content_node in display_node.xpath("content"):
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    47
                content_id = content_node.get("id")
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    48
                if content_id not in content_list:
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    49
                    content_list.append(content_id)                    
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    50
                if content_id not in self.display_contents_list:
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    51
                    self.display_contents_list.append(content_id)
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    52
                for cutting_node  in content_node.xpath("decoupage"):
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    53
                    cutting_id = cutting_node.get("id")
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    54
                    if cutting_id not in cuttings_list:
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    55
                        cuttings_list.append(cutting_id)
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    56
                    if cutting_id not in self.display_cuttings_list:
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    57
                        self.display_cuttings_list.append(cutting_id)
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    58
                    ensemble_id = cutting_node.get("idens")
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    59
                    if ensemble_id not in self.display_ensemble_list:
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    60
                        self.display_ensemble_list.append(ensemble_id)
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    61
            self.views_dict[display_id] = new_display
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    62
                    
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
    def __parse_ensemble(self, ensemble_node, content):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
        
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
    67
        ensemble_id = ensemble_node.attrib[u"id"]
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
    68
        ensemble_author = ensemble_node.attrib[u"author"]
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
    69
        ensemble_title = ensemble_node.attrib[u"title"]
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
    70
        ensemble_description = ensemble_node.attrib[u"abstract"]
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
        ensemble_created = datetime.utcnow().isoformat()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
        ensemble_modified = ensemble_created 
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    73
        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
        list_items = []
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
        new_list = {
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
            "id" : ensemble_id,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
            "items" : list_items,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
            "meta" : {
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
                "dc:creator":ensemble_author,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
                "dc:created": ensemble_created,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
                "dc:contributor":"undefined",
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
                "dc:modified": ensemble_modified,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
                "dc:title":ensemble_title,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
                "dc:description": ensemble_description,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
                "id-ref":content.iri_id,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
                "editable":"false"
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
            }
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
        }
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
        
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
    91
        for decoupage_node in ensemble_node:
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
    92
            if decoupage_node.tag != "decoupage" :
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
                continue
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
            
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
    95
            decoupage_id = decoupage_node.attrib[ u"id"]
98
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    96
            if self.from_display and decoupage_id not in self.display_cuttings_list:
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    97
                continue
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
    98
            decoupage_creator = decoupage_node.attrib[u"author"]
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
            if not decoupage_creator:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
                decoupage_creator = "IRI"
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
            decoupage_contributor = decoupage_creator
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   102
            date_str = decoupage_node.get(u"date")
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
            decoupage_created = None
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
            if date_str :
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
                for date_format in DATE_FORMATS:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
                    try:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
                        decoupage_created = datetime.strptime(date_str,date_format).isoformat()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
                        break
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
                    except Exception:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
                        decoupage_created = None
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
            if decoupage_created is None:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
                decoupage_created = datetime.utcnow().isoformat()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
            decoupage_modified = decoupage_created
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
            
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
            decoupage_title = ""
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   116
            for txtRes in decoupage_node.xpath("title/text()", smart_strings=False): 
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   117
                    decoupage_title += txtRes
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
            decoupage_description = ""
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   120
            for txtRes in decoupage_node.xpath("abstract/text()", smart_strings=False): 
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   121
                    decoupage_description += txtRes
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
            
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
            list_items.append({"id-ref":decoupage_id})
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
            
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
            new_annotation_types = {
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
                "id":decoupage_id,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
                "dc:creator":decoupage_creator,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
                "dc:created":decoupage_created,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
                "dc:contributor":decoupage_contributor,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
                "dc:modified":decoupage_modified,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
                "dc:title":decoupage_title,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
                "dc:description":decoupage_description
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
            }
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
            
98
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   136
            self.annotation_types_dict[decoupage_id] = new_annotation_types
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
                        
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   138
            res = decoupage_node.xpath("elements/element")
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
            for element_node in res:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
                
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   141
                element_id = element_node.attrib[u"id"]
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   142
                element_begin = element_node.attrib[u"begin"]
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   143
                element_duration = element_node.attrib[u"dur"]
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
                element_media = content.iri_id
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   145
                element_color = element_node.attrib[u"color"]
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
                
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
                element_title = ""
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   148
                for txtRes in element_node.xpath("title/text()", smart_strings=False): 
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   149
                    element_title += txtRes
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
                element_description = ""
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   152
                for txtRes in element_node.xpath("abstract/text()", smart_strings=False): 
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   153
                    element_description += txtRes
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
                
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
                element_audio_src = ""
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
                element_audio_href = ""
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   157
                res = element_node.xpath("audio")
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
                if len(res) > 0:
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   159
                    element_audio_src = res[0].get(u"source",u"")
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   160
                    element_audio_href =  res[0].text                
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
                
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
                element_tags = []
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
                
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   164
                tags = element_node.get(u"tags",u"")
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
                
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
                tags_list = map(lambda s:s.strip(),tags.split(","))
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
                #tags                                
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
                if tags is None or len(tags) == 0:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
                    tags_list = []
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   171
                    restagnode = element_node.xpath("tag/text()", smart_strings=False)
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
                    for tagnode in restagnode:
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   173
                        tags_list.append(tagnode)
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
                        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
                if tags_list is None or len(tags_list) == 0:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
                    tags_list = []
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   177
                    restagnode = element_node.xpath("tags/tag/text()", smart_strings=False)
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
                    for tagnode in restagnode:
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   179
                        tags_list.append(tagnode)
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
                
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
                tag_date = datetime.utcnow().isoformat()
24
1296cc9b23d5 correct id generation
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   182
                for tag_title in tags_list:
1296cc9b23d5 correct id generation
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   183
                    if tag_title not in self.tags:
26
5442e8569ff0 correct id generation, use string
ymh <ymh.work@gmail.com>
parents: 24
diff changeset
   184
                        tag_id = unicode(uuid.uuid1())
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
                        new_tag = {
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
                            "id":tag_id,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
                            "meta" : {
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
                                "dc:creator":"IRI",
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
                                "dc:created": tag_date,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
                                "dc:contributor":"IRI",
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
                                "dc:modified": tag_date,
24
1296cc9b23d5 correct id generation
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   192
                                "dc:title":tag_title
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
                            }
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
                        }
24
1296cc9b23d5 correct id generation
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   195
                        self.tags[tag_title] = new_tag
98
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   196
                        self.tags_dict[tag_id] = new_tag
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
                    element_tags.append({"id-ref":tag_id})
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
                if not element_tags:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
                    element_tags = None
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
                    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
                new_annotation = {
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
                    "begin": element_begin,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
                    "end": int(element_begin) + int(element_duration),
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
                    "id": element_id,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
                    "media": element_media,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
                    "content": {
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
                        "mimetype": "application/x-ldt-structured",
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
                        "title": element_title,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
                        "description": element_description,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
                        "color": element_color,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
                        "audio": {
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
                            "src" : element_audio_src,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
                            "mimetype": "audio/mp3",
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
                            "href": element_audio_href
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
                        },
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   217
                    },
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   218
                    "tags": element_tags,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
                    "meta": {
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
                        "id-ref": decoupage_id,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
                        "dc:creator": decoupage_creator,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
                        "dc:contributor": decoupage_contributor,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
                        "dc:created": decoupage_created,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
                        "dc:modified": decoupage_modified
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
                    }
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
                }
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   227
                
98
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   228
                self.annotations_dict[element_id] = new_annotation
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   229
        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
        if not list_items:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
            new_list["items"] = None
98
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   232
        self.lists_dict[ensemble_id] = new_list
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
    def __parse_ldt(self):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
        
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   237
        self.ldt_doc = lxml.etree.fromstring(self.project.ldt.encode("utf-8"))
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
        
98
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   239
        if self.from_display:
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   240
            xpath_str = "/iri/displays/display[position()=1]"
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   241
            if isinstance(self.from_display, basestring):
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   242
                xpath_str = "/iri/displays/display[@id='%s']" % self.from_display
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   243
            
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   244
            logging.debug("xpath_str " + xpath_str)
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   245
            self.__parse_views(self.ldt_doc.xpath(xpath_str))
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   246
            logging.debug("xpath_str " + repr(self.views_dict))
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   247
        
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   248
        res = self.ldt_doc.xpath("/iri/medias/media")
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
        for mediaNode in res:
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   250
            iri_id = mediaNode.attrib[u"id"]
98
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   251
            if self.from_display and iri_id not in self.display_contents_list:
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   252
                continue
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
            content = Content.objects.get(iri_id=iri_id)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   254
            self.__parse_content(content)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   255
            
88
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   256
        res = self.ldt_doc.xpath("/iri/annotations/content")
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
        for content_node in res:
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   258
            content_id = content_node.attrib[u"id"]
98
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   259
            if self.from_display and content_id not in self.display_contents_list:
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   260
                continue
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
            content = Content.objects.get(iri_id=content_id)
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   262
            for ensemble_node in content_node:
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   263
                if ensemble_node.tag != "ensemble" :
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
                    continue
98
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   265
                ensemble_id = ensemble_node.get("id")
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   266
                if self.from_display and ensemble_id not in self.display_ensemble_list:
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   267
                    continue
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   268
                self.__parse_ensemble(ensemble_node, content)            
88
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   269
        
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   270
        self.parsed = True
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   271
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   272
    def __parse_content(self, content):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   273
        
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   274
        doc = lxml.etree.parse(content.iri_file_path())
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   275
        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   276
        authors = content.authors.all()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   277
        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   278
        if len(authors) > 0 :
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   279
            author = authors[0].handle
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   280
        else :
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   281
            author = "IRI"
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   282
        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   283
        if len(authors) > 1 :
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   284
            contributor = authors[1].handle
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   285
        else :
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   286
            contributor = author
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   287
        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   288
        content_author = ""
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   289
        
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   290
        res = doc.xpath("/iri/head/meta[@name='author']/@content")
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   291
        if len(res) > 0:
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   292
            content_author = res[0]
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   293
        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   294
        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   295
        content_date = ""
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   296
        
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   297
        res = doc.xpath("/iri/head/meta[@name='date']/@content")
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   298
        if len(res) > 0:
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   299
            content_date = res[0]
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   300
94
9927a619d2b5 Merge and corrections due to merge
wakimd
parents: 88
diff changeset
   301
        href = ""
9927a619d2b5 Merge and corrections due to merge
wakimd
parents: 88
diff changeset
   302
        meta_item_value = ""
9927a619d2b5 Merge and corrections due to merge
wakimd
parents: 88
diff changeset
   303
        if content.videopath:
9927a619d2b5 Merge and corrections due to merge
wakimd
parents: 88
diff changeset
   304
            href = content.videopath.rstrip('/') + "/" + content.src
9927a619d2b5 Merge and corrections due to merge
wakimd
parents: 88
diff changeset
   305
            meta_item_value = content.videopath.rstrip('/') + "/"
9927a619d2b5 Merge and corrections due to merge
wakimd
parents: 88
diff changeset
   306
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   307
        new_media = {
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   308
             "http://advene.liris.cnrs.fr/ns/frame_of_reference/ms" : "o=0",
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   309
             "id" : content.iri_id,
94
9927a619d2b5 Merge and corrections due to merge
wakimd
parents: 88
diff changeset
   310
             "href" : href,
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   311
             "unit" : "ms",
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   312
             "origin" : "0",
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   313
             "meta": {
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   314
                 "dc:creator" : author,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   315
                 "dc:created" : content.creation_date.isoformat(),
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   316
                 "dc:contributor" : contributor,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   317
                 "dc:modified" : content.update_date.isoformat(),
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   318
                 "dc:creator.contents" : content_author,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   319
                 "dc:created.contents" : content_date,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   320
                 "dc:title" : content.title,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   321
                 "dc:description" : content.description,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   322
                 "dc:duration" : content.get_duration(),
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   323
                 "item": {
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   324
                     "name" : "streamer",
94
9927a619d2b5 Merge and corrections due to merge
wakimd
parents: 88
diff changeset
   325
                     "value": meta_item_value, 
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   326
                 },
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   327
             }
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   328
        }
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   329
        
97
66f6aff5c382 corrections on project serialize and update project
ymh <ymh.work@gmail.com>
parents: 89
diff changeset
   330
        self.medias_dict[content.iri_id] = new_media
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   331
        
85
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 49
diff changeset
   332
        if self.serialize_contents:        
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 49
diff changeset
   333
            res = doc.xpath("/iri/body/ensembles/ensemble")
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 49
diff changeset
   334
            for ensemble_node in res:
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 49
diff changeset
   335
                self.__parse_ensemble(ensemble_node, content)
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   336
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   337
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   338
    def serialize_to_cinelab(self):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   339
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   340
        res = {}
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   341
        
88
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   342
        if not self.parsed:
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   343
            self.__parse_ldt()    
98
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   344
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   345
        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   346
        project_main_media = ""
98
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   347
        if len(self.medias_dict) > 0:
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   348
            project_main_media = self.medias_dict.value_for_index(0)["id"]
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   349
        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   350
        res['meta'] = {
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   351
             'id': self.project.ldt_id,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   352
             'dc:created':self.project.creation_date.isoformat(),
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   353
             'dc:modified':self.project.modification_date.isoformat(),
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   354
             'dc:contributor':self.project.changed_by,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   355
             'dc:creator':self.project.created_by,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   356
             'dc:title':self.project.title,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   357
             'dc:description':self.project.get_description(self.ldt_doc), # get from doc, parse ldt
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   358
             'main_media': {"id-ref":project_main_media}
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   359
            }
98
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   360
                
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   361
                    
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   362
        res['medias'] =  self.medias_dict.values() if len(self.medias_dict) > 0 else None
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   363
        res['annotation-types'] = self.annotation_types_dict.values() if len(self.annotation_types_dict) > 0 else None
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   364
        res['annotations'] = self.annotations_dict.values() if len(self.annotations_dict) > 0 else None 
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   365
        res['lists'] = self.lists_dict.values() if len(self.lists_dict) > 0 else None
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   366
        res['tags'] = self.tags.values() if len(self.tags) > 0 else None
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   367
        res['views'] = self.views_dict.values() if len(self.views_dict) > 0 else None
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   368
        
98
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   369
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   370
        return res
88
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   371
    
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   372
    def getAnnotations(self, first_cutting=True):
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   373
        
88
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   374
        if not self.parsed:
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   375
            self.__parse_ldt()
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   376
        
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   377
        annotations = []
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   378
        
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   379
        current_cutting = None
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   380
        uri = None
98
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   381
        for annot in self.annotations_dict.values():
88
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   382
            if first_cutting and current_cutting and current_cuttings != annot['meta']['id-ref'] :
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   383
                break
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   384
            current_cuttings = annot['meta']['id-ref']
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   385
            content_id = annot['media']
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   386
            content = Content.objects.get(iri_id=content_id)
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   387
            if annot['tags']:
98
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   388
                tags_list = map(lambda tag_entry: self.tags_dict[tag_entry['id-ref']]['meta']['dc:title'],annot['tags'])
88
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   389
            else:
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   390
                tags_list = []
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   391
            begin = int(annot['begin'])
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   392
            duration = int(annot['end'])-begin
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   393
            if content.media_obj and content.media_obj.external_publication_url:
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   394
                uri = "%s#t=%d" % (content.media_obj.external_publication_url, begin)
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   395
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   396
        
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   397
            annotations.append({
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   398
                'begin': begin,
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   399
                'duration':duration,
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   400
                'title':annot['content']['title'],
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   401
                'desc':annot['content']['description'],
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   402
                'tags': tags_list,
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   403
                'id':annot['id'],
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   404
                'uri':uri
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   405
            })
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   406
            
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   407
        return annotations
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   408
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   409