src/ldt/ldt/utils/projectldt_parser.py
author grandjoncl
Thu, 20 Dec 2012 15:22:42 +0100
branchmodifications_relative_ldtxml
changeset 1044 e3902e1bd785
child 1082 28f591b96612
permissions -rw-r--r--
modifications to have relative src for media in the xml ldt of a project. - Migration in order to set all the project ldt with the new values in the xml. If the media has a content that doesn't exist, the media is erased. If the project has no valid media, the project is deleted. There is a test in project_tests. - The migration uses the command : set_projectldtiri - The command set_projectldtiri uses the function in utils relative_src_xml that returns an xml. The command deletes the projects that have no ldt or the ldt isn't valid (empty string) - Modifications in lignesdetemps to have absolute urls in the xml when we open a project or in the research when we display the results in lignes de temps
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1044
e3902e1bd785 modifications to have relative src for media in the xml ldt of a project.
grandjoncl
parents:
diff changeset
     1
from ldt.utils.url import absolute_media_url, is_absolute
e3902e1bd785 modifications to have relative src for media in the xml ldt of a project.
grandjoncl
parents:
diff changeset
     2
from ldt.ldt_utils.models import Content, Project
e3902e1bd785 modifications to have relative src for media in the xml ldt of a project.
grandjoncl
parents:
diff changeset
     3
import lxml.etree
e3902e1bd785 modifications to have relative src for media in the xml ldt of a project.
grandjoncl
parents:
diff changeset
     4
e3902e1bd785 modifications to have relative src for media in the xml ldt of a project.
grandjoncl
parents:
diff changeset
     5
def absolute_src_xml(doc):
e3902e1bd785 modifications to have relative src for media in the xml ldt of a project.
grandjoncl
parents:
diff changeset
     6
    media_list = doc.xpath("/iri/medias/media")
e3902e1bd785 modifications to have relative src for media in the xml ldt of a project.
grandjoncl
parents:
diff changeset
     7
    for element in media_list:
e3902e1bd785 modifications to have relative src for media in the xml ldt of a project.
grandjoncl
parents:
diff changeset
     8
        src = element.get("src") #we split to hate two parts, one with the src
e3902e1bd785 modifications to have relative src for media in the xml ldt of a project.
grandjoncl
parents:
diff changeset
     9
        if not is_absolute(src):
e3902e1bd785 modifications to have relative src for media in the xml ldt of a project.
grandjoncl
parents:
diff changeset
    10
            new_src = absolute_media_url()+src
e3902e1bd785 modifications to have relative src for media in the xml ldt of a project.
grandjoncl
parents:
diff changeset
    11
            element.set('src', new_src) #we replace the old value by the new one
e3902e1bd785 modifications to have relative src for media in the xml ldt of a project.
grandjoncl
parents:
diff changeset
    12
    return doc
e3902e1bd785 modifications to have relative src for media in the xml ldt of a project.
grandjoncl
parents:
diff changeset
    13
e3902e1bd785 modifications to have relative src for media in the xml ldt of a project.
grandjoncl
parents:
diff changeset
    14
def relative_src_xml(ldt):
e3902e1bd785 modifications to have relative src for media in the xml ldt of a project.
grandjoncl
parents:
diff changeset
    15
    modif = False
e3902e1bd785 modifications to have relative src for media in the xml ldt of a project.
grandjoncl
parents:
diff changeset
    16
    no_more_media=False
e3902e1bd785 modifications to have relative src for media in the xml ldt of a project.
grandjoncl
parents:
diff changeset
    17
    media_list = ldt.xpath("/iri/medias/media")
e3902e1bd785 modifications to have relative src for media in the xml ldt of a project.
grandjoncl
parents:
diff changeset
    18
    if len(media_list) == 0 :
e3902e1bd785 modifications to have relative src for media in the xml ldt of a project.
grandjoncl
parents:
diff changeset
    19
        no_more_media = True
e3902e1bd785 modifications to have relative src for media in the xml ldt of a project.
grandjoncl
parents:
diff changeset
    20
    if not no_more_media :
e3902e1bd785 modifications to have relative src for media in the xml ldt of a project.
grandjoncl
parents:
diff changeset
    21
        for element in media_list:
e3902e1bd785 modifications to have relative src for media in the xml ldt of a project.
grandjoncl
parents:
diff changeset
    22
            src = element.get("src")
e3902e1bd785 modifications to have relative src for media in the xml ldt of a project.
grandjoncl
parents:
diff changeset
    23
            id_content=element.get("id")
e3902e1bd785 modifications to have relative src for media in the xml ldt of a project.
grandjoncl
parents:
diff changeset
    24
            content = None
e3902e1bd785 modifications to have relative src for media in the xml ldt of a project.
grandjoncl
parents:
diff changeset
    25
            try :
e3902e1bd785 modifications to have relative src for media in the xml ldt of a project.
grandjoncl
parents:
diff changeset
    26
                content = Content.objects.get(iri_id=id_content)
e3902e1bd785 modifications to have relative src for media in the xml ldt of a project.
grandjoncl
parents:
diff changeset
    27
            except :
e3902e1bd785 modifications to have relative src for media in the xml ldt of a project.
grandjoncl
parents:
diff changeset
    28
                element.getparent().remove(element)
e3902e1bd785 modifications to have relative src for media in the xml ldt of a project.
grandjoncl
parents:
diff changeset
    29
                modif = True
e3902e1bd785 modifications to have relative src for media in the xml ldt of a project.
grandjoncl
parents:
diff changeset
    30
                media_list = ldt.xpath("/iri/medias/media")
e3902e1bd785 modifications to have relative src for media in the xml ldt of a project.
grandjoncl
parents:
diff changeset
    31
                if len(media_list) == 0 :
e3902e1bd785 modifications to have relative src for media in the xml ldt of a project.
grandjoncl
parents:
diff changeset
    32
                    no_more_media =  True
e3902e1bd785 modifications to have relative src for media in the xml ldt of a project.
grandjoncl
parents:
diff changeset
    33
            if content is not None :
e3902e1bd785 modifications to have relative src for media in the xml ldt of a project.
grandjoncl
parents:
diff changeset
    34
                right_src = content.relative_iri_url()
e3902e1bd785 modifications to have relative src for media in the xml ldt of a project.
grandjoncl
parents:
diff changeset
    35
                if not src == right_src: #we will modify only the project that have an absolute url
e3902e1bd785 modifications to have relative src for media in the xml ldt of a project.
grandjoncl
parents:
diff changeset
    36
                    modif = True
e3902e1bd785 modifications to have relative src for media in the xml ldt of a project.
grandjoncl
parents:
diff changeset
    37
                    new_src = right_src
e3902e1bd785 modifications to have relative src for media in the xml ldt of a project.
grandjoncl
parents:
diff changeset
    38
                    element.set('src', new_src) #we replace the old value by the new one
e3902e1bd785 modifications to have relative src for media in the xml ldt of a project.
grandjoncl
parents:
diff changeset
    39
e3902e1bd785 modifications to have relative src for media in the xml ldt of a project.
grandjoncl
parents:
diff changeset
    40
    return ldt, modif, no_more_media