web/ldt/ldt_utils/projectserializer.py
author ymh <ymh.work@gmail.com>
Mon, 24 Jan 2011 10:27:42 +0100
changeset 137 d363fa6232b2
parent 100 c5514a2bfdcd
permissions -rw-r--r--
add polemic and twitter info
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
137
d363fa6232b2 add polemic and twitter info
ymh <ymh.work@gmail.com>
parents: 100
diff changeset
     4
from ldt.ldt_utils.utils import reduce_text_node
98
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
     5
import logging
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
     6
import lxml.etree
24
1296cc9b23d5 correct id generation
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
     7
import uuid
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
DATE_FORMATS = ["%d/%m/%Y","%Y-%m-%d"]
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
137
d363fa6232b2 add polemic and twitter info
ymh <ymh.work@gmail.com>
parents: 100
diff changeset
    11
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
"""
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
Serialize a project object to a cinelab compatible array
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
"""
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
class ProjectSerializer:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
    
88
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    17
    def __init__(self, project, from_contents=True, from_display=True):
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
        self.project = project
88
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    19
        self.parsed = False
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
        self.ldt_doc = None
97
66f6aff5c382 corrections on project serialize and update project
ymh <ymh.work@gmail.com>
parents: 89
diff changeset
    21
        self.medias_dict = SortedDict()
66f6aff5c382 corrections on project serialize and update project
ymh <ymh.work@gmail.com>
parents: 89
diff changeset
    22
        self.annotations_dict = SortedDict()
100
c5514a2bfdcd - error correction on login template
ymh <ymh.work@gmail.com>
parents: 99
diff changeset
    23
        self.annotations_by_annotation_types = {}
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
        self.tags = {}
98
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    25
        self.tags_dict = SortedDict()
97
66f6aff5c382 corrections on project serialize and update project
ymh <ymh.work@gmail.com>
parents: 89
diff changeset
    26
        self.annotation_types_dict = SortedDict()
98
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    27
        self.views_dict = SortedDict()
97
66f6aff5c382 corrections on project serialize and update project
ymh <ymh.work@gmail.com>
parents: 89
diff changeset
    28
        self.lists_dict = SortedDict()
88
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    29
        self.serialize_contents = from_contents
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
    30
        self.from_display = from_display
98
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    31
        self.display_contents_list = []
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    32
        self.display_cuttings_list = []
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    33
        self.display_ensemble_list = []
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    34
        
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    35
        
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    36
    def __parse_views(self, display_node_list):
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    37
        for display_node in display_node_list:
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    38
            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
    39
            if not display_id:
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    40
                continue
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    41
            content_list = []
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    42
            cuttings_list = []
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    43
            new_display = {
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    44
                "id": display_id,
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    45
                "contents": content_list,
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    46
                "annotation_types": cuttings_list,
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    47
            }
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    48
            
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    49
            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
    50
                content_id = content_node.get("id")
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    51
                if content_id not in content_list:
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    52
                    content_list.append(content_id)                    
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    53
                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
    54
                    self.display_contents_list.append(content_id)
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    55
                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
    56
                    cutting_id = cutting_node.get("id")
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    57
                    if cutting_id not in cuttings_list:
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    58
                        cuttings_list.append(cutting_id)
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    59
                    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
    60
                        self.display_cuttings_list.append(cutting_id)
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    61
                    ensemble_id = cutting_node.get("idens")
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    62
                    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
    63
                        self.display_ensemble_list.append(ensemble_id)
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    64
            self.views_dict[display_id] = new_display
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
    65
                    
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
    def __parse_ensemble(self, ensemble_node, content):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
        
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
    70
        ensemble_id = ensemble_node.attrib[u"id"]
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
    71
        ensemble_author = ensemble_node.attrib[u"author"]
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
    72
        ensemble_title = ensemble_node.attrib[u"title"]
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
    73
        ensemble_description = ensemble_node.attrib[u"abstract"]
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
        ensemble_created = datetime.utcnow().isoformat()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
        ensemble_modified = ensemble_created 
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    77
        list_items = []
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
        new_list = {
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
            "id" : ensemble_id,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
            "items" : list_items,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
            "meta" : {
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
                "dc:creator":ensemble_author,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
                "dc:created": ensemble_created,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
                "dc:contributor":"undefined",
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
                "dc:modified": ensemble_modified,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
                "dc:title":ensemble_title,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
                "dc:description": ensemble_description,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
                "id-ref":content.iri_id,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
                "editable":"false"
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
            }
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
        }
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
        
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
    94
        for decoupage_node in ensemble_node:
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
    95
            if decoupage_node.tag != "decoupage" :
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
                continue
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
            
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
    98
            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
    99
            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
   100
                continue
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   101
            decoupage_creator = decoupage_node.attrib[u"author"]
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
            if not decoupage_creator:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
                decoupage_creator = "IRI"
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
            decoupage_contributor = decoupage_creator
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   105
            date_str = decoupage_node.get(u"date")
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
            decoupage_created = None
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
            if date_str :
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
                for date_format in DATE_FORMATS:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
                    try:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
                        decoupage_created = datetime.strptime(date_str,date_format).isoformat()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
                        break
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
                    except Exception:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
                        decoupage_created = None
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
            if decoupage_created is None:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
                decoupage_created = datetime.utcnow().isoformat()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
            decoupage_modified = decoupage_created
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
            
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
            decoupage_title = ""
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   119
            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
   120
                    decoupage_title += txtRes
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
            decoupage_description = ""
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   123
            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
   124
                    decoupage_description += txtRes
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
            
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
            list_items.append({"id-ref":decoupage_id})
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
            
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
            new_annotation_types = {
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   130
                "id":decoupage_id,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
                "dc:creator":decoupage_creator,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
                "dc:created":decoupage_created,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   133
                "dc:contributor":decoupage_contributor,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
                "dc:modified":decoupage_modified,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
                "dc:title":decoupage_title,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
                "dc:description":decoupage_description
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
            }
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
            
98
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   139
            self.annotation_types_dict[decoupage_id] = new_annotation_types
100
c5514a2bfdcd - error correction on login template
ymh <ymh.work@gmail.com>
parents: 99
diff changeset
   140
            self.annotations_by_annotation_types[decoupage_id] = []
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
                        
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   142
            res = decoupage_node.xpath("elements/element")
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
            for element_node in res:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
                
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   145
                element_id = element_node.attrib[u"id"]
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   146
                element_begin = element_node.attrib[u"begin"]
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   147
                element_duration = element_node.attrib[u"dur"]
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   148
                element_media = content.iri_id
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   149
                element_color = element_node.attrib[u"color"]
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
                
137
d363fa6232b2 add polemic and twitter info
ymh <ymh.work@gmail.com>
parents: 100
diff changeset
   151
                element_title = reduce_text_node(element_node,"title/text()")        
d363fa6232b2 add polemic and twitter info
ymh <ymh.work@gmail.com>
parents: 100
diff changeset
   152
                element_description = reduce_text_node(element_node, "abstract/text()")                
d363fa6232b2 add polemic and twitter info
ymh <ymh.work@gmail.com>
parents: 100
diff changeset
   153
                element_twitter = reduce_text_node(element_node, "meta/twitter/text()")
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
                        },
137
d363fa6232b2 add polemic and twitter info
ymh <ymh.work@gmail.com>
parents: 100
diff changeset
   217
                        "polemics" :[pol_elem.text for pol_elem in element_node.xpath("meta/polemics/polemic")],
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   218
                    },
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
                    "tags": element_tags,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   220
                    "meta": {
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
                        "id-ref": decoupage_id,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   222
                        "dc:creator": decoupage_creator,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
                        "dc:contributor": decoupage_contributor,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
                        "dc:created": decoupage_created,
137
d363fa6232b2 add polemic and twitter info
ymh <ymh.work@gmail.com>
parents: 100
diff changeset
   225
                        "dc:modified": decoupage_modified,
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
                    }
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   227
                }
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
                
137
d363fa6232b2 add polemic and twitter info
ymh <ymh.work@gmail.com>
parents: 100
diff changeset
   229
                if element_twitter:
d363fa6232b2 add polemic and twitter info
ymh <ymh.work@gmail.com>
parents: 100
diff changeset
   230
                    new_annotation['meta']['twitter'] = element_twitter
d363fa6232b2 add polemic and twitter info
ymh <ymh.work@gmail.com>
parents: 100
diff changeset
   231
                
98
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   232
                self.annotations_dict[element_id] = new_annotation
100
c5514a2bfdcd - error correction on login template
ymh <ymh.work@gmail.com>
parents: 99
diff changeset
   233
                self.annotations_by_annotation_types[decoupage_id].append(new_annotation)
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
        if not list_items:
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   236
            new_list["items"] = None
98
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   237
        self.lists_dict[ensemble_id] = new_list
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
    def __parse_ldt(self):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
        
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   242
        self.ldt_doc = lxml.etree.fromstring(self.project.ldt.encode("utf-8"))
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
        
98
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   244
        if self.from_display:
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   245
            xpath_str = "/iri/displays/display[position()=1]"
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   246
            if isinstance(self.from_display, basestring):
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   247
                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
   248
            
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   249
            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
   250
        
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   251
        res = self.ldt_doc.xpath("/iri/medias/media")
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   252
        for mediaNode in res:
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   253
            iri_id = mediaNode.attrib[u"id"]
98
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   254
            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
   255
                continue
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   256
            content = Content.objects.get(iri_id=iri_id)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
            self.__parse_content(content)
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   258
            
88
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   259
        res = self.ldt_doc.xpath("/iri/annotations/content")
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
        for content_node in res:
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   261
            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
   262
            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
   263
                continue
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
            content = Content.objects.get(iri_id=content_id)
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   265
            for ensemble_node in content_node:
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   266
                if ensemble_node.tag != "ensemble" :
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
                    continue
98
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   268
                ensemble_id = ensemble_node.get("id")
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   269
                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
   270
                    continue
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   271
                self.__parse_ensemble(ensemble_node, content)            
88
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   272
        
100
c5514a2bfdcd - error correction on login template
ymh <ymh.work@gmail.com>
parents: 99
diff changeset
   273
        #reorder annotations and annotation type from view
c5514a2bfdcd - error correction on login template
ymh <ymh.work@gmail.com>
parents: 99
diff changeset
   274
        if self.from_display and len(self.views_dict) > 0:
c5514a2bfdcd - error correction on login template
ymh <ymh.work@gmail.com>
parents: 99
diff changeset
   275
            new_annotation_types_dict = SortedDict()
c5514a2bfdcd - error correction on login template
ymh <ymh.work@gmail.com>
parents: 99
diff changeset
   276
            new_annotations_dict = SortedDict()
c5514a2bfdcd - error correction on login template
ymh <ymh.work@gmail.com>
parents: 99
diff changeset
   277
            for annotation_type in self.display_cuttings_list:
c5514a2bfdcd - error correction on login template
ymh <ymh.work@gmail.com>
parents: 99
diff changeset
   278
                if annotation_type in self.annotation_types_dict:
c5514a2bfdcd - error correction on login template
ymh <ymh.work@gmail.com>
parents: 99
diff changeset
   279
                    new_annotation_types_dict[annotation_type] = self.annotation_types_dict[annotation_type]
c5514a2bfdcd - error correction on login template
ymh <ymh.work@gmail.com>
parents: 99
diff changeset
   280
                    for annot in self.annotations_by_annotation_types[annotation_type]:
c5514a2bfdcd - error correction on login template
ymh <ymh.work@gmail.com>
parents: 99
diff changeset
   281
                        new_annotations_dict[annot['id']] = annot
c5514a2bfdcd - error correction on login template
ymh <ymh.work@gmail.com>
parents: 99
diff changeset
   282
                    
c5514a2bfdcd - error correction on login template
ymh <ymh.work@gmail.com>
parents: 99
diff changeset
   283
            self.annotations_dict = new_annotations_dict
c5514a2bfdcd - error correction on login template
ymh <ymh.work@gmail.com>
parents: 99
diff changeset
   284
            self.annotation_types_dict = new_annotation_types_dict
c5514a2bfdcd - error correction on login template
ymh <ymh.work@gmail.com>
parents: 99
diff changeset
   285
        
88
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   286
        self.parsed = True
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   287
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   288
    def __parse_content(self, content):
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
        doc = lxml.etree.parse(content.iri_file_path())
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   291
        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   292
        authors = content.authors.all()
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   293
        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   294
        if len(authors) > 0 :
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   295
            author = authors[0].handle
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   296
        else :
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   297
            author = "IRI"
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   298
        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   299
        if len(authors) > 1 :
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   300
            contributor = authors[1].handle
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   301
        else :
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   302
            contributor = author
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   303
        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   304
        content_author = ""
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   305
        
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   306
        res = doc.xpath("/iri/head/meta[@name='author']/@content")
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   307
        if len(res) > 0:
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   308
            content_author = res[0]
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   309
        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   310
        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   311
        content_date = ""
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   312
        
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   313
        res = doc.xpath("/iri/head/meta[@name='date']/@content")
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   314
        if len(res) > 0:
49
55f91a1f9df8 migrate to lxml + new version
ymh <ymh.work@gmail.com>
parents: 26
diff changeset
   315
            content_date = res[0]
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   316
94
9927a619d2b5 Merge and corrections due to merge
wakimd
parents: 88
diff changeset
   317
        href = ""
9927a619d2b5 Merge and corrections due to merge
wakimd
parents: 88
diff changeset
   318
        meta_item_value = ""
9927a619d2b5 Merge and corrections due to merge
wakimd
parents: 88
diff changeset
   319
        if content.videopath:
9927a619d2b5 Merge and corrections due to merge
wakimd
parents: 88
diff changeset
   320
            href = content.videopath.rstrip('/') + "/" + content.src
9927a619d2b5 Merge and corrections due to merge
wakimd
parents: 88
diff changeset
   321
            meta_item_value = content.videopath.rstrip('/') + "/"
9927a619d2b5 Merge and corrections due to merge
wakimd
parents: 88
diff changeset
   322
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   323
        new_media = {
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   324
             "http://advene.liris.cnrs.fr/ns/frame_of_reference/ms" : "o=0",
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   325
             "id" : content.iri_id,
94
9927a619d2b5 Merge and corrections due to merge
wakimd
parents: 88
diff changeset
   326
             "href" : href,
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   327
             "unit" : "ms",
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   328
             "origin" : "0",
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   329
             "meta": {
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   330
                 "dc:creator" : author,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   331
                 "dc:created" : content.creation_date.isoformat(),
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   332
                 "dc:contributor" : contributor,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   333
                 "dc:modified" : content.update_date.isoformat(),
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   334
                 "dc:creator.contents" : content_author,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   335
                 "dc:created.contents" : content_date,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   336
                 "dc:title" : content.title,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   337
                 "dc:description" : content.description,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   338
                 "dc:duration" : content.get_duration(),
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   339
                 "item": {
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   340
                     "name" : "streamer",
94
9927a619d2b5 Merge and corrections due to merge
wakimd
parents: 88
diff changeset
   341
                     "value": meta_item_value, 
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   342
                 },
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   343
             }
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   344
        }
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   345
        
97
66f6aff5c382 corrections on project serialize and update project
ymh <ymh.work@gmail.com>
parents: 89
diff changeset
   346
        self.medias_dict[content.iri_id] = new_media
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   347
        
85
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 49
diff changeset
   348
        if self.serialize_contents:        
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 49
diff changeset
   349
            res = doc.xpath("/iri/body/ensembles/ensemble")
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 49
diff changeset
   350
            for ensemble_node in res:
3b70d84e661a Bug corrections
ymh <ymh.work@gmail.com>
parents: 49
diff changeset
   351
                self.__parse_ensemble(ensemble_node, content)
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   352
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   353
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   354
    def serialize_to_cinelab(self):
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   355
    
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   356
        res = {}
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   357
        
88
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   358
        if not self.parsed:
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   359
            self.__parse_ldt()    
98
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   360
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   361
        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   362
        project_main_media = ""
98
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   363
        if len(self.medias_dict) > 0:
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   364
            project_main_media = self.medias_dict.value_for_index(0)["id"]
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   365
        
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   366
        res['meta'] = {
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   367
             'id': self.project.ldt_id,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   368
             'dc:created':self.project.creation_date.isoformat(),
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   369
             'dc:modified':self.project.modification_date.isoformat(),
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   370
             'dc:contributor':self.project.changed_by,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   371
             'dc:creator':self.project.created_by,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   372
             'dc:title':self.project.title,
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   373
             '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
   374
             'main_media': {"id-ref":project_main_media}
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   375
            }
98
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   376
                
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   377
                    
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   378
        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
   379
        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
   380
        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
   381
        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
   382
        
100
c5514a2bfdcd - error correction on login template
ymh <ymh.work@gmail.com>
parents: 99
diff changeset
   383
        res['annotation-types'] = self.annotation_types_dict.values() if len(self.annotation_types_dict) > 0 else None
c5514a2bfdcd - error correction on login template
ymh <ymh.work@gmail.com>
parents: 99
diff changeset
   384
        res['annotations'] = self.annotations_dict.values() if len(self.annotations_dict) > 0 else None 
c5514a2bfdcd - error correction on login template
ymh <ymh.work@gmail.com>
parents: 99
diff changeset
   385
c5514a2bfdcd - error correction on login template
ymh <ymh.work@gmail.com>
parents: 99
diff changeset
   386
        
98
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   387
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   388
        return res
88
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   389
    
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   390
    def getAnnotations(self, first_cutting=True):
0
cc4a51750724 first commit
ymh <ymh.work@gmail.com>
parents:
diff changeset
   391
        
88
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   392
        if not self.parsed:
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   393
            self.__parse_ldt()
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   394
        
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   395
        annotations = []
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
        current_cutting = None
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   398
        uri = None
98
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   399
        for annot in self.annotations_dict.values():
100
c5514a2bfdcd - error correction on login template
ymh <ymh.work@gmail.com>
parents: 99
diff changeset
   400
            logging.debug("current cutting" + repr(current_cutting) + " : annot " + annot['meta']['id-ref'])
c5514a2bfdcd - error correction on login template
ymh <ymh.work@gmail.com>
parents: 99
diff changeset
   401
            if first_cutting and current_cutting and current_cutting != annot['meta']['id-ref'] :
88
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   402
                break
100
c5514a2bfdcd - error correction on login template
ymh <ymh.work@gmail.com>
parents: 99
diff changeset
   403
            current_cutting = annot['meta']['id-ref']
88
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   404
            content_id = annot['media']
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   405
            content = Content.objects.get(iri_id=content_id)
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   406
            if annot['tags']:
98
c9460033138f correct project serializer and view for rdf
ymh <ymh.work@gmail.com>
parents: 97
diff changeset
   407
                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
   408
            else:
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   409
                tags_list = []
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   410
            begin = int(annot['begin'])
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   411
            duration = int(annot['end'])-begin
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   412
            if content.media_obj and content.media_obj.external_publication_url:
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   413
                uri = "%s#t=%d" % (content.media_obj.external_publication_url, begin)
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
            annotations.append({
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   417
                'begin': begin,
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   418
                'duration':duration,
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   419
                'title':annot['content']['title'],
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   420
                'desc':annot['content']['description'],
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   421
                'tags': tags_list,
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   422
                'id':annot['id'],
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   423
                'uri':uri
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   424
            })
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   425
            
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   426
        return annotations
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   427
7f2c2d9adf58 correct project serializer
ymh <ymh.work@gmail.com>
parents: 85
diff changeset
   428