src/ldtplatform/management/commands/importamateur.py
author ymh <ymh.work@gmail.com>
Wed, 05 Jul 2017 13:01:27 +0200
changeset 350 c4bb8e679d17
parent 337 6152504f5452
permissions -rw-r--r--
Update ldt version nb + increment version
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
335
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
     1
'''
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
     2
Imports amateur.iri.centrepompidou.fr/nouveaumonde files to ldt.iri
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
     3
'''
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
     4
import csv
336
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
     5
import re
337
6152504f5452 Correction on get_durations + small formatting pb
ymh <ymh.work@gmail.com>
parents: 336
diff changeset
     6
336
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
     7
import requests
337
6152504f5452 Correction on get_durations + small formatting pb
ymh <ymh.work@gmail.com>
parents: 336
diff changeset
     8
from django.core import management
335
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
     9
from django.core.management.base import BaseCommand
337
6152504f5452 Correction on get_durations + small formatting pb
ymh <ymh.work@gmail.com>
parents: 336
diff changeset
    10
from django.db import connections
335
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
    11
from lxml import etree
337
6152504f5452 Correction on get_durations + small formatting pb
ymh <ymh.work@gmail.com>
parents: 336
diff changeset
    12
335
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
    13
from ldt.ldt_utils import models
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
    14
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
    15
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
    16
def create_source(iri_id):
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
    17
    '''
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
    18
    create .iri source
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
    19
    '''
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
    20
    return "https://media.iri.centrepompidou.fr/video/enmi/" + iri_id + ".mp4"
337
6152504f5452 Correction on get_durations + small formatting pb
ymh <ymh.work@gmail.com>
parents: 336
diff changeset
    21
6152504f5452 Correction on get_durations + small formatting pb
ymh <ymh.work@gmail.com>
parents: 336
diff changeset
    22
335
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
    23
def create_iri_url(iri_url):
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
    24
    '''
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
    25
    create iri url
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
    26
    '''
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
    27
    return "http://amateur.iri.centrepompidou.fr" + iri_url
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
    28
337
6152504f5452 Correction on get_durations + small formatting pb
ymh <ymh.work@gmail.com>
parents: 336
diff changeset
    29
336
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
    30
def create_title(edition, day, name, order, session):
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
    31
    '''
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
    32
    create title
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
    33
    '''
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
    34
    if re.match(r'....\_1', edition) or edition == '2009':
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
    35
        event = "Prepa ENMI "
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
    36
    else:
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
    37
        event = "ENMI "
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
    38
    session += 1
337
6152504f5452 Correction on get_durations + small formatting pb
ymh <ymh.work@gmail.com>
parents: 336
diff changeset
    39
    return event + edition[0:4] + " - " + str(day) + "." \
6152504f5452 Correction on get_durations + small formatting pb
ymh <ymh.work@gmail.com>
parents: 336
diff changeset
    40
        + str(session) + "." + str(order) + " - " + name
6152504f5452 Correction on get_durations + small formatting pb
ymh <ymh.work@gmail.com>
parents: 336
diff changeset
    41
6152504f5452 Correction on get_durations + small formatting pb
ymh <ymh.work@gmail.com>
parents: 336
diff changeset
    42
336
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
    43
def create_name(fname, lname):
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
    44
    '''
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
    45
    create name
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
    46
    '''
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
    47
    if fname is None:
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
    48
        if lname is None:
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
    49
            return ""
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
    50
        return lname
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
    51
    if lname is None:
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
    52
        return fname
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
    53
    return fname + " " + lname
335
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
    54
337
6152504f5452 Correction on get_durations + small formatting pb
ymh <ymh.work@gmail.com>
parents: 336
diff changeset
    55
336
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
    56
def create_tmp(edition, title):
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
    57
    '''
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
    58
    create temp to check medias with two authors
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
    59
    '''
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
    60
    if re.match(r'....\_1', edition) or edition == '2009':
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
    61
        length = 24
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
    62
    else:
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
    63
        length = 18
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
    64
    return title[0:length]
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
    65
337
6152504f5452 Correction on get_durations + small formatting pb
ymh <ymh.work@gmail.com>
parents: 336
diff changeset
    66
336
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
    67
def create_tag(edition):
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
    68
    '''
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
    69
    create name
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
    70
    '''
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
    71
    if re.match(r'....\_1', edition) or edition == '2009':
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
    72
        event = "prepaenmi"
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
    73
    else:
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
    74
        event = "enmi"
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
    75
    return event + edition[0:4]
337
6152504f5452 Correction on get_durations + small formatting pb
ymh <ymh.work@gmail.com>
parents: 336
diff changeset
    76
6152504f5452 Correction on get_durations + small formatting pb
ymh <ymh.work@gmail.com>
parents: 336
diff changeset
    77
335
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
    78
class Command(BaseCommand):
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
    79
    '''
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
    80
    command
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
    81
    '''
337
6152504f5452 Correction on get_durations + small formatting pb
ymh <ymh.work@gmail.com>
parents: 336
diff changeset
    82
6152504f5452 Correction on get_durations + small formatting pb
ymh <ymh.work@gmail.com>
parents: 336
diff changeset
    83
    def __init__(self, *args, **kwargs):
6152504f5452 Correction on get_durations + small formatting pb
ymh <ymh.work@gmail.com>
parents: 336
diff changeset
    84
        super(Command, self).__init__(*args, **kwargs)
6152504f5452 Correction on get_durations + small formatting pb
ymh <ymh.work@gmail.com>
parents: 336
diff changeset
    85
        self.parser = None
6152504f5452 Correction on get_durations + small formatting pb
ymh <ymh.work@gmail.com>
parents: 336
diff changeset
    86
        self.myfile = None
6152504f5452 Correction on get_durations + small formatting pb
ymh <ymh.work@gmail.com>
parents: 336
diff changeset
    87
        self.writefile = None
6152504f5452 Correction on get_durations + small formatting pb
ymh <ymh.work@gmail.com>
parents: 336
diff changeset
    88
        self.durations = None
6152504f5452 Correction on get_durations + small formatting pb
ymh <ymh.work@gmail.com>
parents: 336
diff changeset
    89
6152504f5452 Correction on get_durations + small formatting pb
ymh <ymh.work@gmail.com>
parents: 336
diff changeset
    90
335
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
    91
    def add_arguments(self, parser):
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
    92
        '''
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
    93
        add arguments
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
    94
        '''
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
    95
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
    96
        parser.add_argument('-i',
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
    97
                            '--pathin',
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
    98
                            dest='pathin',
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
    99
                            default=None
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   100
                           )
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   101
        parser.add_argument('-o',
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   102
                            '--pathout',
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   103
                            dest='pathout',
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   104
                            default=None
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   105
                           )
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   106
337
6152504f5452 Correction on get_durations + small formatting pb
ymh <ymh.work@gmail.com>
parents: 336
diff changeset
   107
    def parse_duration(self, xmlstr):
6152504f5452 Correction on get_durations + small formatting pb
ymh <ymh.work@gmail.com>
parents: 336
diff changeset
   108
        '''
6152504f5452 Correction on get_durations + small formatting pb
ymh <ymh.work@gmail.com>
parents: 336
diff changeset
   109
        parse duration from xml def
6152504f5452 Correction on get_durations + small formatting pb
ymh <ymh.work@gmail.com>
parents: 336
diff changeset
   110
        '''
6152504f5452 Correction on get_durations + small formatting pb
ymh <ymh.work@gmail.com>
parents: 336
diff changeset
   111
        root = etree.XML(xmlstr.encode('utf-8'), self.parser)
6152504f5452 Correction on get_durations + small formatting pb
ymh <ymh.work@gmail.com>
parents: 336
diff changeset
   112
        return root.xpath('format')[0].get('duration')[:7]
6152504f5452 Correction on get_durations + small formatting pb
ymh <ymh.work@gmail.com>
parents: 336
diff changeset
   113
335
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   114
    def get_duration(self, elem):
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   115
        '''
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   116
        get duration
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   117
        '''
337
6152504f5452 Correction on get_durations + small formatting pb
ymh <ymh.work@gmail.com>
parents: 336
diff changeset
   118
        return self.durations.get(elem)
335
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   119
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   120
    def change_annotations(self, iriin, ldtout):
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   121
        '''
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   122
        change annotations
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   123
        '''
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   124
        rootout = etree.XML(ldtout.encode('utf-8'), self.parser)
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   125
        rootin = etree.XML(iriin.encode('utf-8'), self.parser)
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   126
        ensembles = rootin.xpath('body/ensembles/ensemble')
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   127
        for ensemble in ensembles:
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   128
            idens = ensemble.get("id")
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   129
            decoups = ensemble.xpath('decoupage')
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   130
            rootout.xpath('annotations/content')[0].append(ensemble)
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   131
            for decoup in decoups:
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   132
                iddec = decoup.get("id")
337
6152504f5452 Correction on get_durations + small formatting pb
ymh <ymh.work@gmail.com>
parents: 336
diff changeset
   133
                nouveaudecoup = '<decoupage idens=\"' \
6152504f5452 Correction on get_durations + small formatting pb
ymh <ymh.work@gmail.com>
parents: 336
diff changeset
   134
                    + idens +'\" id=\"' \
6152504f5452 Correction on get_durations + small formatting pb
ymh <ymh.work@gmail.com>
parents: 336
diff changeset
   135
                    + iddec +'\" tagsSelect=\"\"/>'
335
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   136
                nouveaudecxml = etree.fromstring(nouveaudecoup)
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   137
                rootout.xpath('displays/display/content')[0].append(nouveaudecxml)
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   138
        return etree.tostring(rootout)
337
6152504f5452 Correction on get_durations + small formatting pb
ymh <ymh.work@gmail.com>
parents: 336
diff changeset
   139
335
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   140
    def handle(self, *args, **options):
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   141
        '''
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   142
        handle command
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   143
        '''
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   144
        pathin = options['pathin']
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   145
        pathout = options['pathout']
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   146
        if not pathin or not pathout:
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   147
            return "Error : specify path in and path out"
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   148
        try:
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   149
            csvfile = open(pathin, 'r')
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   150
            csvfile2 = open(pathout, 'wb')
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   151
        except IOError:
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   152
            self.stdout.write('file can\'t be opened')
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   153
            return
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   154
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   155
        cursor = connections['default2'].cursor()
336
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   156
        cursor.execute('SELECT ct.iri_id, ct.iriurl, ct.creation_date, ct.description,\
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   157
                        pj.ldt,\
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   158
                        day._order,\
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   159
                        edit.code,\
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   160
                        pers.firstname, pers.lastname,\
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   161
                        speak.order,\
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   162
                        sess._order\
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   163
                        FROM ldt_content AS ct\
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   164
                        LEFT JOIN ldt_ldtproject_contents AS ctpj ON ct.id = ctpj.content_id\
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   165
                        LEFT JOIN ldt_ldtproject AS pj ON ctpj.ldtproject_id = pj.id\
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   166
                        LEFT JOIN conf_speak AS speak ON ct.id=speak.content_id\
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   167
                        LEFT JOIN conf_session AS sess ON sess.event_ptr_id=speak.session_id\
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   168
                        LEFT JOIN conf_day AS day ON sess.day_id=day.event_ptr_id\
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   169
                        LEFT JOIN conf_edition AS edit ON day.edition_id=edit.event_ptr_id\
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   170
                        LEFT JOIN conf_speak_speakers AS spkr ON speak.event_ptr_id=spkr.speak_id\
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   171
                        LEFT JOIN conf_person AS pers ON spkr.person_id=pers.id;'
335
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   172
                      )
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   173
        amateurdata = cursor.fetchall()
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   174
        self.parser = etree.XMLParser(encoding='utf-8')
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   175
        self.myfile = csv.reader(csvfile)
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   176
        self.writefile = csv.writer(csvfile2)
337
6152504f5452 Correction on get_durations + small formatting pb
ymh <ymh.work@gmail.com>
parents: 336
diff changeset
   177
        self.durations = dict([
6152504f5452 Correction on get_durations + small formatting pb
ymh <ymh.work@gmail.com>
parents: 336
diff changeset
   178
            (duration_def[0].strip(), self.parse_duration(duration_def[1].strip()))
6152504f5452 Correction on get_durations + small formatting pb
ymh <ymh.work@gmail.com>
parents: 336
diff changeset
   179
            for duration_def in self.myfile
6152504f5452 Correction on get_durations + small formatting pb
ymh <ymh.work@gmail.com>
parents: 336
diff changeset
   180
        ])
336
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   181
        titletmp = ''
335
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   182
        for mediaproj in amateurdata:
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   183
            iri_id = mediaproj[0]
336
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   184
            mysource = create_source(iri_id)
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   185
            dayorder = mediaproj[5]
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   186
            edition = mediaproj[6]
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   187
            sessionorder = mediaproj[10]
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   188
            speakorder = mediaproj[9]
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   189
            firstname = mediaproj[7]
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   190
            lastname = mediaproj[8]
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   191
            if dayorder is None and edition is None:
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   192
                title = iri_id
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   193
                fullname = ''
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   194
                tag = iri_id
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   195
            else:
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   196
                tag = create_tag(edition)
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   197
                fullname = create_name(firstname, lastname)
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   198
                title = create_title(edition, dayorder + 1, fullname, speakorder, sessionorder)
335
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   199
            try:
336
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   200
                mymedia = models.Media.objects.get(src=mysource)
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   201
                mycontent = models.Content.objects.get(media_obj_id=mymedia.id)
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   202
                thisregex = r"front project : " + titletmp + r".*"
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   203
                myproject = models.Project.objects.get(title__iregex=thisregex)
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   204
                myregex = r".*" + re.escape(fullname) + r".*"
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   205
                if not re.match(myregex, mycontent.title):
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   206
                    mycontent.title += " & "
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   207
                    mycontent.title += fullname
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   208
                    mycontent.save()
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   209
                    myproject.title += " & "
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   210
                    myproject.title += fullname
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   211
                    myproject.save()
335
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   212
                continue
336
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   213
            except (models.Media.MultipleObjectsReturned,
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   214
                    models.Content.MultipleObjectsReturned,
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   215
                    models.Project.MultipleObjectsReturned
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   216
                   ):
335
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   217
                continue
336
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   218
            except (models.Media.DoesNotExist,
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   219
                    models.Content.DoesNotExist,
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   220
                    models.Project.DoesNotExist
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   221
                   ):
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   222
                self.stdout.write('Media %s and Content %s will be created'%(iri_id, title))
335
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   223
            iriurl = mediaproj[1]
336
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   224
            if dayorder is None and edition is None:
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   225
                titletmp = ''
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   226
            else:
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   227
                titletmp = create_tmp(edition, title)
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   228
            #TODO set creationdate
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   229
            #creationdate = mediaproj[2]
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   230
            description = mediaproj[3]
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   231
            # ldt = mediaproj[4]
335
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   232
            myiriurl = create_iri_url(iriurl)
337
6152504f5452 Correction on get_durations + small formatting pb
ymh <ymh.work@gmail.com>
parents: 336
diff changeset
   233
            myiri = requests.get(myiriurl).text
335
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   234
            duration = self.get_duration(mysource)
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   235
            if requests.head(mysource).status_code == 200:
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   236
                management.call_command(
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   237
                    'createmediacontent',
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   238
                    source=mysource,
337
6152504f5452 Correction on get_durations + small formatting pb
ymh <ymh.work@gmail.com>
parents: 336
diff changeset
   239
                    title=title.encode('utf-8') if title else '',
335
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   240
                    videopath='',
337
6152504f5452 Correction on get_durations + small formatting pb
ymh <ymh.work@gmail.com>
parents: 336
diff changeset
   241
                    description=description.encode('utf-8') if description else '',
335
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   242
                    duration=duration,
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   243
                    public=True,
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   244
                    creator='admin',
336
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   245
                    update=True,
337
6152504f5452 Correction on get_durations + small formatting pb
ymh <ymh.work@gmail.com>
parents: 336
diff changeset
   246
                    tags=tag.encode('utf-8') if tag else ''
335
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   247
                    )
336
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   248
                myfrontproj = models.Project.objects.get(title='front project : %s' % title)
335
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   249
                myfrontproj.ldt = self.change_annotations(myiri, myfrontproj.ldt)
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   250
                self.writefile.writerow([mysource,
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   251
                                         iri_id,
336
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   252
                                         models.Content.objects.get(title=title).iri_id,
337
6152504f5452 Correction on get_durations + small formatting pb
ymh <ymh.work@gmail.com>
parents: 336
diff changeset
   253
                                         title.encode('utf-8') if title else '',
336
ff3b847c14a2 set contents order in title + add edition tag
bellierp
parents: 335
diff changeset
   254
                                         myfrontproj.ldt_id,
335
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   255
                                        ])
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   256
                self.stdout.write("Project changed")
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   257
                myfrontproj.save()
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   258
        csvfile.close()
c7a01f03c19c add importamateur command
bellierp
parents:
diff changeset
   259
        csvfile2.close()