bgneal@45: """ bgneal@45: import_old_news.py - For importing news stories from the older version of this bgneal@45: website. bgneal@45: bgneal@45: """ bgneal@45: import datetime bgneal@45: bgneal@45: from django.conf import settings bgneal@45: from django.core.management.base import LabelCommand bgneal@45: from django.utils import simplejson as json bgneal@45: from django.utils.html import linebreaks bgneal@45: import textile bgneal@45: bgneal@45: from news.models import News bgneal@45: bgneal@45: IMG_TAG = u"""\ bgneal@45: {title}""" bgneal@45: bgneal@45: bgneal@45: class Command(LabelCommand): bgneal@45: args = '' bgneal@45: help = 'Imports older news stories in JSON format' bgneal@45: bgneal@45: def handle_label(self, filename, **options): bgneal@45: """ bgneal@45: Process the file of older news stories in JSON. Convert to the new model bgneal@45: scheme. bgneal@45: bgneal@45: """ bgneal@45: with open(filename, 'rb') as f: bgneal@45: items = json.load(f) bgneal@45: bgneal@45: for item in items: bgneal@45: if item['model'] == 'band.news': bgneal@45: self.process_item(item) bgneal@45: bgneal@45: def process_item(self, item): bgneal@45: bgneal@45: fields = item['fields'] bgneal@45: bgneal@45: content = fields['text'].strip() bgneal@45: if fields['markup_enabled']: bgneal@45: content = textile.textile(content, encoding='utf-8', output='utf-8') bgneal@45: else: bgneal@45: content = linebreaks(fields['text']) bgneal@45: bgneal@45: author = fields['author'].strip() bgneal@45: if author: bgneal@45: content += u"

\u2013 %s" % author bgneal@45: bgneal@45: image = fields['photo'].strip() bgneal@45: if image: bgneal@45: caption = fields['photo_caption'].strip() bgneal@45: caption = caption if caption else 'Image' bgneal@45: bgneal@45: src = u"%s%s" % (settings.MEDIA_URL, image) bgneal@45: bgneal@45: content = IMG_TAG.format(src=src, title=caption) + content bgneal@45: bgneal@45: news = News(id=item['pk'], bgneal@45: title=fields['title'].strip(), bgneal@45: date=datetime.datetime.strptime(fields['date'], '%Y-%m-%d'), bgneal@45: content=content) bgneal@45: bgneal@45: news.save()