src/ldtplatform/management/commands/importamateur.py
changeset 336 ff3b847c14a2
parent 335 c7a01f03c19c
child 337 6152504f5452
equal deleted inserted replaced
335:c7a01f03c19c 336:ff3b847c14a2
     1 '''
     1 '''
     2 Imports amateur.iri.centrepompidou.fr/nouveaumonde files to ldt.iri
     2 Imports amateur.iri.centrepompidou.fr/nouveaumonde files to ldt.iri
     3 '''
     3 '''
     4 import csv
     4 import csv
       
     5 import re
     5 import sys
     6 import sys
     6 
       
     7 import requests
     7 import requests
       
     8 
     8 from django.core.management.base import BaseCommand
     9 from django.core.management.base import BaseCommand
     9 from django.core import management
    10 from django.core import management
    10 from lxml import etree
    11 from lxml import etree
    11 from django.db import connections
    12 from django.db import connections
    12 from ldt.ldt_utils import models
    13 from ldt.ldt_utils import models
    21     '''
    22     '''
    22     create iri url
    23     create iri url
    23     '''
    24     '''
    24     return "http://amateur.iri.centrepompidou.fr" + iri_url
    25     return "http://amateur.iri.centrepompidou.fr" + iri_url
    25 
    26 
    26 
    27 def create_title(edition, day, name, order, session):
       
    28     '''
       
    29     create title
       
    30     '''
       
    31     if re.match(r'....\_1', edition) or edition == '2009':
       
    32         event = "Prepa ENMI "
       
    33     else:
       
    34         event = "ENMI "
       
    35     session += 1
       
    36     return event + edition[0:4] + " - " + str(day) + "." + str(session) + "." + str(order) + " - " + name
       
    37 def create_name(fname, lname):
       
    38     '''
       
    39     create name
       
    40     '''
       
    41     if fname is None:
       
    42         if lname is None:
       
    43             return ""
       
    44         return lname
       
    45     if lname is None:
       
    46         return fname
       
    47     return fname + " " + lname
       
    48 
       
    49 def create_tmp(edition, title):
       
    50     '''
       
    51     create temp to check medias with two authors
       
    52     '''
       
    53     if re.match(r'....\_1', edition) or edition == '2009':
       
    54         length = 24
       
    55     else:
       
    56         length = 18
       
    57     return title[0:length]
       
    58 
       
    59 def create_tag(edition):
       
    60     '''
       
    61     create name
       
    62     '''
       
    63     if re.match(r'....\_1', edition) or edition == '2009':
       
    64         event = "prepaenmi"
       
    65     else:
       
    66         event = "enmi"
       
    67     return event + edition[0:4]
    27 class Command(BaseCommand):
    68 class Command(BaseCommand):
    28     '''
    69     '''
    29     command
    70     command
    30     '''
    71     '''
    31     def add_arguments(self, parser):
    72     def add_arguments(self, parser):
    88         except IOError:
   129         except IOError:
    89             self.stdout.write('file can\'t be opened')
   130             self.stdout.write('file can\'t be opened')
    90             return
   131             return
    91 
   132 
    92         cursor = connections['default2'].cursor()
   133         cursor = connections['default2'].cursor()
    93         cursor.execute('SELECT ct.iri_id, ct.iriurl, ct.creation_date, ct.title, ct.description,\
   134         cursor.execute('SELECT ct.iri_id, ct.iriurl, ct.creation_date, ct.description,\
    94                         pj.ldt_id, pj.ldt\
   135                         pj.ldt,\
    95                         FROM ldt_content AS ct INNER JOIN ldt_ldtproject_contents\
   136                         day._order,\
    96                         AS ctpj ON ct.id = ctpj.content_id\
   137                         edit.code,\
    97                         INNER JOIN ldt_ldtproject AS pj ON ctpj.ldtproject_id = pj.id;'
   138                         pers.firstname, pers.lastname,\
       
   139                         speak.order,\
       
   140                         sess._order\
       
   141                         FROM ldt_content AS ct\
       
   142                         LEFT JOIN ldt_ldtproject_contents AS ctpj ON ct.id = ctpj.content_id\
       
   143                         LEFT JOIN ldt_ldtproject AS pj ON ctpj.ldtproject_id = pj.id\
       
   144                         LEFT JOIN conf_speak AS speak ON ct.id=speak.content_id\
       
   145                         LEFT JOIN conf_session AS sess ON sess.event_ptr_id=speak.session_id\
       
   146                         LEFT JOIN conf_day AS day ON sess.day_id=day.event_ptr_id\
       
   147                         LEFT JOIN conf_edition AS edit ON day.edition_id=edit.event_ptr_id\
       
   148                         LEFT JOIN conf_speak_speakers AS spkr ON speak.event_ptr_id=spkr.speak_id\
       
   149                         LEFT JOIN conf_person AS pers ON spkr.person_id=pers.id;'
    98                       )
   150                       )
    99         amateurdata = cursor.fetchall()
   151         amateurdata = cursor.fetchall()
   100         reload(sys)
   152         reload(sys)
   101         sys.setdefaultencoding('utf8')
   153         sys.setdefaultencoding('utf8')
   102         self.parser = etree.XMLParser(encoding='utf-8')
   154         self.parser = etree.XMLParser(encoding='utf-8')
   103         self.myfile = csv.reader(csvfile)
   155         self.myfile = csv.reader(csvfile)
   104         self.writefile = csv.writer(csvfile2)
   156         self.writefile = csv.writer(csvfile2)
   105         self.mylist = list(self.myfile)
   157         self.mylist = list(self.myfile)
       
   158         titletmp = ''
   106         for mediaproj in amateurdata:
   159         for mediaproj in amateurdata:
   107             iri_id = mediaproj[0]
   160             iri_id = mediaproj[0]
       
   161             mysource = create_source(iri_id)
       
   162             dayorder = mediaproj[5]
       
   163             edition = mediaproj[6]
       
   164             sessionorder = mediaproj[10]
       
   165             speakorder = mediaproj[9]
       
   166             firstname = mediaproj[7]
       
   167             lastname = mediaproj[8]
       
   168             if dayorder is None and edition is None:
       
   169                 title = iri_id
       
   170                 fullname = ''
       
   171                 tag = iri_id
       
   172             else:
       
   173                 tag = create_tag(edition)
       
   174                 fullname = create_name(firstname, lastname)
       
   175                 title = create_title(edition, dayorder + 1, fullname, speakorder, sessionorder)
   108             try:
   176             try:
   109                 models.Project.objects.get(title='front project : %s' % iri_id)
   177                 mymedia = models.Media.objects.get(src=mysource)
       
   178                 mycontent = models.Content.objects.get(media_obj_id=mymedia.id)
       
   179                 thisregex = r"front project : " + titletmp + r".*"
       
   180                 myproject = models.Project.objects.get(title__iregex=thisregex)
       
   181                 myregex = r".*" + re.escape(fullname) + r".*"
       
   182                 if not re.match(myregex, mycontent.title):
       
   183                     mycontent.title += " & "
       
   184                     mycontent.title += fullname
       
   185                     mycontent.save()
       
   186                     myproject.title += " & "
       
   187                     myproject.title += fullname
       
   188                     myproject.save()
   110                 continue
   189                 continue
   111             except models.Project.MultipleObjectsReturned:
   190             except (models.Media.MultipleObjectsReturned,
       
   191                     models.Content.MultipleObjectsReturned,
       
   192                     models.Project.MultipleObjectsReturned
       
   193                    ):
   112                 continue
   194                 continue
   113             except models.Project.DoesNotExist:
   195             except (models.Media.DoesNotExist,
   114                 self.stdout.write('Media %s will be created'%iri_id)
   196                     models.Content.DoesNotExist,
       
   197                     models.Project.DoesNotExist
       
   198                    ):
       
   199                 self.stdout.write('Media %s and Content %s will be created'%(iri_id, title))
   115             iriurl = mediaproj[1]
   200             iriurl = mediaproj[1]
   116             #TODO set creationdate, title...
   201             if dayorder is None and edition is None:
   117             # creationdate = mediaproj[2]
   202                 titletmp = ''
   118             # title = mediaproj[3]
   203             else:
   119             description = mediaproj[4]
   204                 titletmp = create_tmp(edition, title)
   120             # ldt_id = mediaproj[5]
   205             #TODO set creationdate
   121             # ldt = mediaproj[6]
   206             #creationdate = mediaproj[2]
   122             mysource = create_source(iri_id)
   207             description = mediaproj[3]
       
   208             # ldt = mediaproj[4]
   123             myiriurl = create_iri_url(iriurl)
   209             myiriurl = create_iri_url(iriurl)
   124             myiri = requests.get(myiriurl)._content
   210             myiri = requests.get(myiriurl)._content
   125             duration = self.get_duration(mysource)
   211             duration = self.get_duration(mysource)
   126             if requests.head(mysource).status_code == 200:
   212             if requests.head(mysource).status_code == 200:
   127                 management.call_command(
   213                 management.call_command(
   128                     'createmediacontent',
   214                     'createmediacontent',
   129                     source=mysource,
   215                     source=mysource,
   130                     title=iri_id,
   216                     title=title,
   131                     videopath='',
   217                     videopath='',
   132                     description=description,
   218                     description=description,
   133                     duration=duration,
   219                     duration=duration,
   134                     public=True,
   220                     public=True,
   135                     creator='admin',
   221                     creator='admin',
   136                     update=True
   222                     update=True,
       
   223                     tags=tag
   137                     )
   224                     )
   138                 myfrontproj = models.Project.objects.get(title='front project : %s' % iri_id)
   225                 myfrontproj = models.Project.objects.get(title='front project : %s' % title)
   139                 myfrontproj.ldt = self.change_annotations(myiri, myfrontproj.ldt)
   226                 myfrontproj.ldt = self.change_annotations(myiri, myfrontproj.ldt)
   140                 self.writefile.writerow([mysource,
   227                 self.writefile.writerow([mysource,
   141                                          iri_id,
   228                                          iri_id,
   142                                          models.Content.objects.get(title=iri_id).iri_id,
   229                                          models.Content.objects.get(title=title).iri_id,
   143                                          myfrontproj.ldt_id
   230                                          title,
       
   231                                          myfrontproj.ldt_id,
   144                                         ])
   232                                         ])
   145                 self.stdout.write("Project changed")
   233                 self.stdout.write("Project changed")
   146                 myfrontproj.save()
   234                 myfrontproj.save()
   147         csvfile.close()
   235         csvfile.close()
   148         csvfile2.close()
   236         csvfile2.close()