src/ldt/ldt/ldt_utils/utils.py
changeset 1415 c212b4f4e059
parent 1400 9e4cf0a50d93
child 1423 09d4840f213e
equal deleted inserted replaced
1414:9c76c7eea3fd 1415:c212b4f4e059
   167     def __init__(self, project, force_save=False):
   167     def __init__(self, project, force_save=False):
   168         self.project = project
   168         self.project = project
   169         self.parser = lxml.etree.XMLParser(remove_blank_text=True)
   169         self.parser = lxml.etree.XMLParser(remove_blank_text=True)
   170         self.ldtdoc = lxml.etree.parse(StringIO(project.ldt_encoded), self.parser)
   170         self.ldtdoc = lxml.etree.parse(StringIO(project.ldt_encoded), self.parser)
   171         self.to_add = True
   171         self.to_add = True
   172         
   172     
   173     #   add( a['media'], a['type'],  a['type_title, a[data], '', a['tags'], begin, dur, author, date)
   173     def update_annotation_element(self, element, title, text, begin, dur, author, date, color, audio_src, audio_href, tags_list):       
   174     def add(self, media, cutting_id, cutting_title, title, text, tags_list, begin, dur, author, date, view_id="0", color="2194379", audio_scr="", audio_href=""):
   174         element.set('begin', begin)
       
   175         element.set('dur', dur)
       
   176         element.set('author', author)
       
   177         element.set('date', date)
       
   178         element.set('color', color)
       
   179         element.set('src', "")
       
   180         
       
   181         abstract_nodes = element.xpath('abstract')
       
   182         if len(abstract_nodes) <= 0:
       
   183             abstract_node = lxml.etree.SubElement(element, 'abstract')
       
   184         else:
       
   185             for node in abstract_nodes:
       
   186                 if node != abstract_nodes[0]:
       
   187                     element.remove(node)
       
   188             abstract_node = abstract_nodes[0]
       
   189         abstract_node.text = text
       
   190         
       
   191         title_nodes = element.xpath('title')
       
   192         if len(title_nodes) <= 0:
       
   193             title_node = lxml.etree.SubElement(element, 'title')
       
   194         else:
       
   195             for node in title_nodes:
       
   196                 if node != title_nodes[0]:
       
   197                     element.remove(node)
       
   198             title_node = title_nodes[0]
       
   199         title_node.text = title
       
   200         
       
   201         audio_nodes = element.xpath('audio')
       
   202         if len(audio_nodes) <= 0:
       
   203             audio_node = lxml.etree.SubElement(element, 'audio')
       
   204         else:
       
   205             for node in audio_nodes:
       
   206                 if node != audio_nodes[0]:
       
   207                     element.remove(node)
       
   208             audio_node = audio_nodes[0]
       
   209         audio_node.set('source', audio_src)
       
   210         audio_node.text = audio_href
       
   211                                 
       
   212         polemics = self.get_polemic_syntax(title)
       
   213         
       
   214         if polemics:
       
   215             meta_nodes = element.xpath('meta')
       
   216             if len(meta_nodes) <= 0:
       
   217                 meta_node = lxml.etree.SubElement(element, 'meta')
       
   218             else:
       
   219                 for node in meta_nodes:
       
   220                     if node != meta_nodes[0]:
       
   221                         element.remove(node)
       
   222                 meta_node = meta_nodes[0]
       
   223                 
       
   224             for polemics_node in meta_node.xpath('polemics'):
       
   225                 meta_node.remove(polemics_node)
       
   226                 
       
   227             polemics_node = lxml.etree.SubElement(meta_node, 'polemics')
       
   228             
       
   229             for polemic in polemics:
       
   230                 polemic_node = lxml.etree.SubElement(polemics_node, 'polemic')
       
   231                 polemic_node.text = polemic
       
   232         
       
   233         for tag_node in element.xpath('tags'):
       
   234             element.remove(tag_node)
       
   235         
       
   236         tags = lxml.etree.SubElement(element, 'tags')
       
   237         for tag in tags_list:
       
   238             tag_node = lxml.etree.SubElement(tags, 'tag')
       
   239             tag_node.text = tag
       
   240 
       
   241     #   add(a['media'], a['type'],  a['type_title, a[data], '', a['tags'], begin, dur, author, date)
       
   242     def add(self, media, cutting_id, cutting_title, title, text, tags_list, begin, dur, author, date, view_id="0", color="2194379", audio_src="", audio_href=""):
   175         """
   243         """
   176         Add an annotation to a project. begin and dur must be strings. Default color is yellow.
   244         Add an annotation to a project. begin and dur must be strings. Default color is yellow.
   177         """
   245         """
   178         
       
   179         if dur < 0:
   246         if dur < 0:
   180             self.to_add = False
   247             self.to_add = False
   181             return False
   248             return False
   182             
   249             
   183         # We check if the project references the media.
   250         # We check if the project references the media.
   246                     dec.set('idens', ensemble_id)
   313                     dec.set('idens', ensemble_id)
   247                     dec.set('id', cutting_id)
   314                     dec.set('id', cutting_id)
   248                     dec.set('tagsSelect', '')
   315                     dec.set('tagsSelect', '')
   249         
   316         
   250         # We add the annotation/element node
   317         # We add the annotation/element node
   251         element = lxml.etree.SubElement(decoupage_elements[0], 'element')
   318         
   252         id_annotation = 's_' + generate_uuid()
   319         id_annotation = 's_' + generate_uuid()
       
   320         element = lxml.etree.SubElement(decoupage_elements[0], 'element')      
   253         element.set('id', id_annotation)
   321         element.set('id', id_annotation)
   254         element.set('begin', begin)
   322         
   255         element.set('dur', dur)
   323         self.update_annotation_element(element, title, text, begin, dur, author, date, color, audio_src, audio_href, tags_list)
   256         element.set('author', author)
   324                     
   257         element.set('date', date)
   325         return cutting_id, id_annotation, ensemble_id
   258         element.set('color', color)
   326     
   259         element.set('src', "")
   327         
   260         abstract = lxml.etree.SubElement(element, 'abstract')
   328     #   edit(      a["id"], a['media'], a['type'], a['type_title'], a[data], '', a['tags'], begin, dur, author, date)
   261         abstract.text = text
   329     def edit(self, ann_id, media, cutting_id, cutting_title, title, text, tags_list, begin, dur, author, date, view_id="0", color="2194379", audio_src="", audio_href=""):   
   262         title_node = lxml.etree.SubElement(element, 'title')
   330         """
   263         title_node.text = title
   331         Edit an annotation in a project. begin and dur must be strings. Default color is yellow.
   264         audio = lxml.etree.SubElement(element, 'audio')
   332         """
   265         audio.set('source', audio_scr)
   333         
   266         audio.text = audio_href
   334         if dur < 0:
   267         tags = lxml.etree.SubElement(element, 'tags')
   335             self.to_add = False
   268                                 
   336             return False
   269         polemics = self.get_polemic_syntax(title)
   337         
   270         
   338         # We check if the project references the media.
   271         if polemics:
   339         path_media = self.ldtdoc.xpath('/iri/medias/media[@id="%s"]' % media)
   272             meta = lxml.etree.SubElement(element, 'meta')
   340         if len(path_media) == 0:
   273             polemics_node = lxml.etree.SubElement(meta, 'polemics')
   341             self.to_add = False
       
   342             return False
       
   343         
       
   344         # We get the content node
       
   345         path_annotations = self.ldtdoc.xpath('/iri/annotations')[0]
       
   346         path_content = path_annotations.xpath('content[@id="%s"]' % media)
       
   347         if len(path_content) == 0:
       
   348             # If the content node does not exist, we abort as this should be an edition operation
       
   349             return False
       
   350         
       
   351         # We check if cutting_id is provided, if it isn't we abort as this is an edition operation
       
   352         if cutting_id is None or cutting_id=="" :
       
   353             return False
       
   354         # We get the ensemble node
       
   355         path_ensemble = path_content[0].xpath('ensemble[decoupage[@id="%s"]]' % cutting_id)
       
   356         
       
   357         if len(path_ensemble) == 0:
       
   358             # If the ensemble node does not exist, we abort as this should be an edition operation
       
   359             return False
       
   360         #else:
       
   361         #    path_ensemble = path_content[0].xpath('ensemble')
   274             
   362             
   275             for polemic in polemics:
   363         # We get the elements node in the good decoupage node
   276                 polemic_node = lxml.etree.SubElement(polemics_node, 'polemic')
   364         ensemble_id = path_ensemble[0].get('id')
   277                 polemic_node.text = polemic
   365         decoupage_elements = path_ensemble[0].xpath('decoupage[@id="%s"]/elements' % cutting_id)
   278         
   366         if len(decoupage_elements) == 0:
   279         for tag in tags_list:
   367             # If the decoupage node does not exist, we abort as this should be an edition operation 
   280             tag_node = lxml.etree.SubElement(tags, 'tag')
   368             return False
   281             tag_node.text = tag
   369         #else:
   282         
   370         #    cutting_id = path_ensemble[0].xpath('decoupage[title="%s"]' % cutting_title)[0].get('id')     
   283         return cutting_id, id_annotation, ensemble_id
   371         
       
   372         # We get the annotation/element node
       
   373         elements = decoupage_elements[0].xpath('element[@id="%s"]' % ann_id)
       
   374         if len(elements) == 0:
       
   375             # If the element doesn't exist there was a mistake calling edit method so we abort
       
   376             return False
       
   377         
       
   378         element = elements[0]
       
   379         self.update_annotation_element(element, title, text, begin, dur, author, date, color, audio_src, audio_href, tags_list)
       
   380            
       
   381         return ensemble_id
   284     
   382     
   285     def save(self, must_reindex=True):
   383     def save(self, must_reindex=True):
   286         if self.to_add:
   384         if self.to_add:
   287             self.project.ldt = lxml.etree.tostring(self.ldtdoc, pretty_print=True)
   385             self.project.ldt = lxml.etree.tostring(self.ldtdoc, pretty_print=True)
   288             self.project.save(must_reindex=must_reindex)
   386             self.project.save(must_reindex=must_reindex)