summaryrefslogtreecommitdiff
path: root/program/management
diff options
context:
space:
mode:
authorErnesto Rico-Schmidt <e.rico.schmidt@gmail.com>2011-03-19 23:15:01 (GMT)
committerErnesto Rico-Schmidt <e.rico.schmidt@gmail.com>2011-03-19 23:15:01 (GMT)
commite1d853c457d77d1f36ad211c85dc28a8833cd6cd (patch)
tree3668b67ab4d13e102d777e3e0e67d59ce16bd668 /program/management
parent0176bc873cb1b46a1ebf277fe1f138f26bd95e31 (diff)
data migration.
Diffstat (limited to 'program/management')
-rw-r--r--program/management/commands/importhosts.py34
-rw-r--r--program/management/commands/importprogramslots.py91
-rw-r--r--program/management/commands/importshows.py62
3 files changed, 187 insertions, 0 deletions
diff --git a/program/management/commands/importhosts.py b/program/management/commands/importhosts.py
new file mode 100644
index 0000000..0f1b154
--- /dev/null
+++ b/program/management/commands/importhosts.py
@@ -0,0 +1,34 @@
+from django.core.management.base import NoArgsCommand
+
+import MySQLdb
+
+from program.models import Host
+
+USER = 'helsinki'
+PASSWD = 'helsinki'
+DB = 'helsinki'
+
+class Command(NoArgsCommand):
+ help = 'Import hosts from current program'
+
+ def handle_noargs(self, **options):
+ connection = MySQLdb.connect(user=USER, passwd=PASSWD, db=DB)
+ cursor = connection.cursor()
+
+ cursor.execute("""SELECT DISTINCT macher
+FROM sendungen
+WHERE letzter_termin > current_date AND macher != '' AND titel NOT LIKE 'Musikprogramm'""")
+
+ counter = 0
+
+ for row in cursor.fetchall():
+ for macher in row[0].decode('latin1').encode('utf8').split(','):
+ host = Host(name=macher.strip())
+ host.save()
+
+ counter += 1
+
+ cursor.close()
+ connection.close()
+
+ print '%i hosts imported' % counter \ No newline at end of file
diff --git a/program/management/commands/importprogramslots.py b/program/management/commands/importprogramslots.py
new file mode 100644
index 0000000..904036e
--- /dev/null
+++ b/program/management/commands/importprogramslots.py
@@ -0,0 +1,91 @@
+from django.core.exceptions import ObjectDoesNotExist
+from django.core.management.base import NoArgsCommand
+from django.utils.html import strip_tags
+
+from datetime import time
+import MySQLdb
+
+from program.models import Show, ProgramSlot, RRule
+
+USER = 'helsinki'
+PASSWD = 'helsinki'
+DB = 'helsinki'
+
+RRULES = {
+ 0: RRule.objects.get(pk=1),
+ 7: RRule.objects.get(pk=3),
+ 14: RRule.objects.get(pk=4),
+ 28: RRule.objects.get(pk=5)
+}
+
+class Command(NoArgsCommand):
+ help = 'Import programslots from the current program'
+
+ def handle_noargs(self, **options):
+ connection = MySQLdb.connect(user=USER, passwd=PASSWD, db=DB)
+ cursor = connection.cursor()
+
+ cursor.execute("""SELECT titel, beginn, ende, erster_termin, letzter_termin, rhytmus, termin
+FROM sendungen
+WHERE letzter_termin > current_date AND titel NOT LIKE 'Musikprogramm' AND titel NOT LIKE '%%(Wiederholung)'""")
+
+ counter = 0
+
+ for titel, beginn, ende, erster_termin, letzter_termin, rhytmus, termin in cursor.fetchall():
+ titel = strip_tags(titel.decode('latin1').encode('utf8'))
+
+ hours, seconds = divmod(beginn.seconds, 3600)
+ minutes, seconds = divmod(seconds, 60)
+ tstart = time(hour=hours, minute=minutes, second=seconds)
+
+ hours, seconds = divmod(ende.seconds, 3600)
+ minutes, seconds = divmod(seconds, 60)
+ tend = time(hour=hours, minute=minutes, second=seconds)
+
+ try:
+ rrule = RRULES[rhytmus]
+
+ try:
+ show = Show.objects.get(name=titel)
+
+ programslot = ProgramSlot(rrule=rrule, byweekday=termin, show=show, dstart=erster_termin, tstart=tstart, tend=tend, until=letzter_termin)
+ programslot.save()
+ counter += 1
+ except ObjectDoesNotExist:
+ print 'show with name "%s" not found' % titel
+ except KeyError:
+ print 'rhythmus "%i" is not supported for sendung "%s"' % (rhytmus, titel)
+
+ cursor.execute("""SELECT titel, beginn, ende, erster_termin, letzter_termin, rhytmus, termin
+FROM sendungen
+WHERE letzter_termin > current_date AND titel LIKE '%%(Wiederholung)'""")
+
+ for titel, beginn, ende, erster_termin, letzter_termin, rhytmus, termin in cursor.fetchall():
+ titel = titel.decode('latin1').encode('utf8')[:-15]
+
+ hours, seconds = divmod(beginn.seconds, 3600)
+ minutes, seconds = divmod(seconds, 60)
+ tstart = time(hour=hours, minute=minutes, second=seconds)
+
+ hours, seconds = divmod(ende.seconds, 3600)
+ minutes, seconds = divmod(seconds, 60)
+ tend = time(hour=hours, minute=minutes, second=seconds)
+
+ try:
+ rrule = RRULES[rhytmus]
+
+ try:
+ show = Show.objects.get(name=titel)
+
+ programslot = ProgramSlot(rrule=rrule, byweekday=termin, show=show, dstart=erster_termin, tstart=tstart, tend=tend, until=letzter_termin, is_repetition=True)
+ programslot.save()
+ counter += 1
+ except ObjectDoesNotExist:
+ print 'show with name "%s" not found' % titel
+ except KeyError:
+ print 'rhythmus "%i" is not supported for sendung "%s"' % (rhytmus, titel)
+
+ cursor.close()
+ connection.close()
+
+ print '%i programslots imported' % counter
diff --git a/program/management/commands/importshows.py b/program/management/commands/importshows.py
new file mode 100644
index 0000000..71eb6fb
--- /dev/null
+++ b/program/management/commands/importshows.py
@@ -0,0 +1,62 @@
+from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned
+from django.core.management.base import NoArgsCommand
+from django.template.defaultfilters import slugify
+from django.utils.html import clean_html, strip_tags
+
+import MySQLdb
+
+from program.models import BroadcastFormat, Host, Show
+
+USER = 'helsinki'
+PASSWD = 'helsinki'
+DB = 'helsinki'
+
+TALK = BroadcastFormat.objects.get(pk=1)
+
+class Command(NoArgsCommand):
+ help = 'Import shows from the current program'
+
+ def handle_noargs(self, **options):
+ connection = MySQLdb.connect(user=USER, passwd=PASSWD, db=DB)
+ cursor = connection.cursor()
+
+ cursor.execute("""SELECT titel, beschreibung, web, macher
+FROM sendungen
+WHERE letzter_termin > current_date AND titel NOT LIKE 'Musikprogramm' AND titel NOT LIKE '%%(Wiederholung)'
+ORDER BY titel, beginn, ende""")
+
+ counter = 0
+
+ for titel, beschreibung, web, macher in cursor.fetchall():
+ titel = strip_tags(titel.decode('latin1').encode('utf8'))
+ beschreibung = clean_html(beschreibung.decode('latin1').encode('utf8'))
+
+ slug = slugify(titel)
+
+ hosts = []
+
+ for macher in macher.decode('latin1').encode('utf8').split(','):
+ try:
+ host = Host.objects.get(name=macher.strip())
+ hosts.append(host)
+ except MultipleObjectsReturned:
+ print 'multiple hosts with name "%s" found' % macher
+ except ObjectDoesNotExist:
+ print 'host with name "%s" not found' % macher
+
+ try:
+ show = Show.objects.get(name=titel)
+ print 'sendung "%s" already imported as show "%s"' % (titel, show)
+ except ObjectDoesNotExist:
+ show = Show(broadcastformat=TALK, name=titel, slug=slug, short_description='FIXME', description=beschreibung)
+ show.save()
+ counter += 1
+
+ for h in hosts:
+ show.hosts.add(h)
+ show.save()
+
+ cursor.close()
+ connection.close()
+
+ print '%i shows imported' % counter