src/ldt/ldt/management/commands/updateiriurlinprojects.py
author cavaliet
Mon, 15 Apr 2013 16:34:56 +0200
changeset 1153 2014becc0211
child 1190 129d45eec68c
permissions -rw-r--r--
new command to update iri url in project xml. update version number
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1153
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
     1
# -*- coding: utf-8 -*-
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
     2
'''
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
     3
Created on Apr 15, 2013
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
     4
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
     5
@author: tc
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
     6
'''
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
     7
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
     8
from ..utils import show_progress
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
     9
from django.core.management import call_command
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    10
from django.core.management.base import BaseCommand, CommandError
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    11
from django.db import transaction
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    12
from django.db.models import signals
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    13
from ldt.ldt_utils.contentindexer import index_project
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    14
from ldt.ldt_utils.models import Content, Project
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    15
import lxml.etree #@UnresolvedImport
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    16
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    17
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    18
class Command(BaseCommand):
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    19
    '''
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    20
    Updates iri files's url in project ldt xml
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    21
    '''
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    22
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    23
    help = "Updates iri files's url in project ldt xml"
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    24
    
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    25
    
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    26
    def __safe_get(self, dict_arg, key, conv = lambda x: x, default= None):
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    27
        val = dict_arg.get(key, default)
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    28
        return conv(val) if val else default
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    29
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    30
    def __safe_decode(self, s):
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    31
        if not isinstance(s, basestring):
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    32
            return s
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    33
        try:
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    34
            return s.decode('utf8')
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    35
        except:
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    36
            try:
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    37
                return s.decode('latin1')
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    38
            except:
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    39
                return s.decode('utf8','replace')
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    40
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    41
    def handle(self, *args, **options):
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    42
        # We avoid a useless reindex
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    43
        signals.post_save.disconnect(index_project, sender=Project)
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    44
        # We begin...
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    45
        writer = None
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    46
        i = 0
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    47
        print("Loading datas...")
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    48
        projects = Project.objects.all().prefetch_related('contents')
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    49
        total = len(projects)
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    50
        for p in projects:
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    51
            i += 1
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    52
            writer = show_progress(i, total, "project : %s" % (p.title), 50, writer)
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    53
            # Get ldt xml
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    54
            ldt = None
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    55
            try:
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    56
                ldt = lxml.etree.fromstring(p.ldt_encoded)
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    57
            except:
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    58
                # Unable to read xml.
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    59
                continue
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    60
            # Get content id
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    61
            result = ldt.xpath("/iri/medias/media")
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    62
            for medianode in result:
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    63
                # We loop on p.contents because they have already been loaded in the orm request
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    64
                # Requesting Content.objects.get() would cost too many db requests
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    65
                content_iri_id = medianode.get("id")
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    66
                for content in p.contents.all():
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    67
                    if content.iri_id == content_iri_id:
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    68
                        medianode.set('src', content.iri_url())
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    69
                        break
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    70
            # All iri urls have been updated, we can save the project
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    71
            p.ldt = lxml.etree.tostring(ldt, pretty_print=True)
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    72
            with transaction.commit_on_success():
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    73
                p.save()
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    74
        # This is the end
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    75
        signals.post_save.connect(index_project, sender=Project)
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    76
        print("This is the end")
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    77
        
2014becc0211 new command to update iri url in project xml. update version number
cavaliet
parents:
diff changeset
    78