bgneal@47: """ bgneal@47: import_old_articles.py - For importing articles from the older version of this bgneal@47: website. bgneal@47: bgneal@47: """ bgneal@47: import datetime bgneal@47: bgneal@47: from django.conf import settings bgneal@47: from django.core.management.base import LabelCommand bgneal@47: from django.utils import simplejson as json bgneal@47: from django.utils.html import linebreaks bgneal@47: import textile bgneal@47: bgneal@47: from articles.models import Article bgneal@47: bgneal@47: bgneal@47: class Command(LabelCommand): bgneal@47: args = '' bgneal@47: help = 'Imports older articles in JSON format' bgneal@47: bgneal@47: def handle_label(self, filename, **options): bgneal@47: """ bgneal@47: Process the file of older articles in JSON. Convert to the new model bgneal@47: scheme. bgneal@47: bgneal@47: """ bgneal@47: with open(filename, 'rb') as f: bgneal@47: items = json.load(f) bgneal@47: bgneal@47: for item in items: bgneal@47: if item['model'] == 'band.article': bgneal@47: self.process_item(item) bgneal@47: bgneal@47: def process_item(self, item): bgneal@47: bgneal@47: fields = item['fields'] bgneal@47: bgneal@47: content = fields['text'].strip() bgneal@47: if fields['markup_enabled']: bgneal@47: text = textile.textile(content, encoding='utf-8', output='utf-8') bgneal@47: else: bgneal@47: text = linebreaks(fields['text']) bgneal@47: bgneal@47: source = linebreaks(fields['source'].strip()) bgneal@47: bgneal@47: pdf = fields['pdf'].strip() bgneal@47: if pdf: bgneal@47: pdf = u"%s%s" % (settings.MEDIA_URL, pdf.replace('\\', '/')) bgneal@47: bgneal@47: article = Article( bgneal@47: id=item['pk'], bgneal@47: title=fields['title'].strip(), bgneal@47: date=datetime.datetime.strptime(fields['date'], '%Y-%m-%d'), bgneal@47: text=text, bgneal@47: source=source, bgneal@47: url=fields['url'].strip(), bgneal@47: pdf=pdf) bgneal@47: bgneal@47: article.save()