diff articles/management/commands/import_old_articles.py @ 71:e2868ad47a1e

For Django 1.4, using the new manage.py.
author Brian Neal <bgneal@gmail.com>
date Sat, 14 Apr 2012 16:40:29 -0500
parents madeira/articles/management/commands/import_old_articles.py@0e51e5be34b9
children b7cdfdde3999
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/articles/management/commands/import_old_articles.py	Sat Apr 14 16:40:29 2012 -0500
@@ -0,0 +1,59 @@
+"""
+import_old_articles.py - For importing articles from the older version of this
+website.
+
+"""
+import datetime
+
+from django.conf import settings
+from django.core.management.base import LabelCommand
+from django.utils import simplejson as json
+from django.utils.html import linebreaks
+import textile
+
+from articles.models import Article
+
+
+class Command(LabelCommand):
+    args = '<filename filename ...>'
+    help = 'Imports older articles in JSON format'
+
+    def handle_label(self, filename, **options):
+        """
+        Process the file of older articles in JSON. Convert to the new model
+        scheme.
+
+        """
+        with open(filename, 'rb') as f:
+            items = json.load(f)
+
+        for item in items:
+            if item['model'] == 'band.article':
+                self.process_item(item)
+
+    def process_item(self, item):
+
+        fields = item['fields']
+
+        content = fields['text'].strip()
+        if fields['markup_enabled']:
+            text = textile.textile(content, encoding='utf-8', output='utf-8')
+        else:
+            text = linebreaks(fields['text'])
+
+        source = linebreaks(fields['source'].strip())
+
+        pdf = fields['pdf'].strip()
+        if pdf:
+            pdf = u"%s%s" % (settings.MEDIA_URL, pdf.replace('\\', '/'))
+
+        article = Article(
+                id=item['pk'],
+                title=fields['title'].strip(),
+                date=datetime.datetime.strptime(fields['date'], '%Y-%m-%d'),
+                text=text,
+                source=source,
+                url=fields['url'].strip(),
+                pdf=pdf)
+
+        article.save()