changeset 237:1085dc38399e

In support of #92, create management commands to import link and podcast data from the old site in CSV format.
author Brian Neal <bgneal@gmail.com>
date Sat, 11 Sep 2010 21:07:28 +0000
parents 953c71f382df
children a3b47d0f4df1
files gpp/podcast/management/__init__.py gpp/podcast/management/commands/__init__.py gpp/podcast/management/commands/import_old_podcasts.py gpp/weblinks/management/__init__.py gpp/weblinks/management/commands/__init__.py gpp/weblinks/management/commands/import_old_links.py gpp/weblinks/models.py
diffstat 3 files changed, 137 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gpp/podcast/management/commands/import_old_podcasts.py	Sat Sep 11 21:07:28 2010 +0000
@@ -0,0 +1,62 @@
+"""
+import_old_podcasts.py - For importing podcasts from SG101 1.0 as csv files.
+"""
+import csv
+import datetime
+
+from django.core.management.base import LabelCommand, CommandError
+
+from podcast.models import Channel, Item
+
+
+class Command(LabelCommand):
+    args = '<filename filename ...>'
+    help = 'Imports podcasts from the old database in CSV format'
+
+    def handle_label(self, filename, **options):
+        """
+        Process each line in the CSV file given by filename by
+        creating a new weblink object and saving it to the database.
+
+        """
+        try:
+            self.channel = Channel.objects.get(pk=1)
+        except Channel.DoesNotExist:
+            raise CommandError("Need a default channel with pk=1")
+
+        try:
+            with open(filename, "rb") as f:
+                self.reader = csv.DictReader(f)
+                try:
+                    for row in self.reader:
+                        self.process_row(row)
+                except csv.Error, e:
+                    raise CommandError("CSV error: %s %s %s" % (
+                        filename, self.reader.line_num, e))
+
+        except IOError:
+            raise CommandError("Could not open file: %s" % filename)
+
+    def process_row(self, row):
+        """
+        Process one row from the CSV file: create an object for the row
+        and save it in the database.
+
+        """
+        item = Item(channel=self.channel,
+            title=row['title'],
+            author=row['author'],
+            subtitle=row['subtitle'],
+            summary=row['summary'],
+            enclosure_url=row['enclosure_url'],
+            alt_enclosure_url='',
+            enclosure_length=int(row['enclosure_length']),
+            enclosure_type=row['enclosure_type'],
+            guid=row['guid'],
+            pubdate=datetime.datetime.strptime(row['pubdate'],
+                "%Y-%m-%d %H:%M:%S"),
+            duration=row['duration'],
+            keywords=row['keywords'],
+            explicit=row['explicit'])
+
+        item.save()
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/gpp/weblinks/management/commands/import_old_links.py	Sat Sep 11 21:07:28 2010 +0000
@@ -0,0 +1,72 @@
+"""
+import_old_links.py - For importing links from SG101 1.0 as csv files.
+"""
+import csv
+import datetime
+
+from django.core.management.base import LabelCommand, CommandError
+from django.contrib.auth.models import User
+
+from weblinks.models import Link, Category
+
+
+class Command(LabelCommand):
+    args = '<filename filename ...>'
+    help = 'Imports weblinks from the old database in CSV format'
+
+    def handle_label(self, filename, **options):
+        """
+        Process each line in the CSV file given by filename by
+        creating a new weblink object and saving it to the database.
+
+        """
+        self.cats = {}
+        try:
+            self.user = User.objects.get(pk=1)
+        except User.DoesNotExist:
+            raise CommandError("Need a default user with pk=1")
+
+        try:
+            with open(filename, "rb") as f:
+                self.reader = csv.DictReader(f)
+                try:
+                    for row in self.reader:
+                        self.process_row(row)
+                except csv.Error, e:
+                    raise CommandError("CSV error: %s %s %s" % (
+                        filename, self.reader.line_num, e))
+
+        except IOError:
+            raise CommandError("Could not open file: %s" % filename)
+
+    def get_category(self, row):
+        """
+        Return the Category object for the row.
+
+        """
+        cat_id = row['cid']
+        if cat_id not in self.cats:
+            try:
+                cat = Category.objects.get(pk=cat_id)
+            except Category.DoesNotExist:
+                raise CommandError("Category does not exist: %s on line %s" % (
+                    cat_id, self.reader.line_num))
+            else:
+                self.cats[cat_id] = cat
+        return self.cats[cat_id]
+
+    def process_row(self, row):
+        """
+        Process one row from the CSV file: create an object for the row
+        and save it in the database.
+
+        """
+        link = Link(category=self.get_category(row),
+            title=row['title'],
+            url=row['url'],
+            description=row['description'],
+            user=self.user,
+            date_added=datetime.datetime.strptime(row['date'], "%Y-%m-%d %H:%M:%S"),
+            hits=int(row['hits']),
+            is_public=True)
+        link.save()
--- a/gpp/weblinks/models.py	Fri Sep 10 03:22:01 2010 +0000
+++ b/gpp/weblinks/models.py	Sat Sep 11 21:07:28 2010 +0000
@@ -4,7 +4,7 @@
 import datetime
 
 from django.db import models
-from django.contrib import auth
+from django.contrib.auth.models import User
 
 
 class Category(models.Model):
@@ -34,7 +34,7 @@
     title = models.CharField(max_length=128)
     url = models.URLField(verify_exists=False, db_index=True)
     description = models.TextField(blank=True)
-    user = models.ForeignKey(auth.models.User)
+    user = models.ForeignKey(User)
     date_added = models.DateField()
 
     class Meta:
@@ -93,7 +93,7 @@
 class FlaggedLink(models.Model):
     """Model to represent links that have been flagged as broken by users"""
     link = models.ForeignKey(Link)
-    user = models.ForeignKey(auth.models.User)
+    user = models.ForeignKey(User)
     date_flagged = models.DateField(auto_now_add = True)
     approved = models.BooleanField(default = False, 
         help_text = 'Check this and save to remove the referenced link from the database')