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