summaryrefslogtreecommitdiff
path: root/program/management/commands/importshows.py
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/commands/importshows.py
parent0176bc873cb1b46a1ebf277fe1f138f26bd95e31 (diff)
data migration.
Diffstat (limited to 'program/management/commands/importshows.py')
-rw-r--r--program/management/commands/importshows.py62
1 files changed, 62 insertions, 0 deletions
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