annotate news/management/commands/import_old_news.py @ 113:8a0076d7d041

Bootstrap: press.
author Brian Neal <bgneal@gmail.com>
date Sun, 20 Oct 2013 11:20:24 -0500
parents b7cdfdde3999
children
rev   line source
bgneal@45 1 """
bgneal@45 2 import_old_news.py - For importing news stories from the older version of this
bgneal@45 3 website.
bgneal@45 4
bgneal@45 5 """
bgneal@45 6 import datetime
bgneal@89 7 import json
bgneal@45 8
bgneal@45 9 from django.conf import settings
bgneal@45 10 from django.core.management.base import LabelCommand
bgneal@45 11 from django.utils.html import linebreaks
bgneal@45 12 import textile
bgneal@45 13
bgneal@45 14 from news.models import News
bgneal@45 15
bgneal@45 16 IMG_TAG = u"""\
bgneal@45 17 <img src="{src}" class="floatLeftBox" alt="{title}" title="{title}" border="0" />"""
bgneal@45 18
bgneal@45 19
bgneal@45 20 class Command(LabelCommand):
bgneal@45 21 args = '<filename filename ...>'
bgneal@45 22 help = 'Imports older news stories in JSON format'
bgneal@45 23
bgneal@45 24 def handle_label(self, filename, **options):
bgneal@45 25 """
bgneal@45 26 Process the file of older news stories in JSON. Convert to the new model
bgneal@45 27 scheme.
bgneal@45 28
bgneal@45 29 """
bgneal@45 30 with open(filename, 'rb') as f:
bgneal@45 31 items = json.load(f)
bgneal@45 32
bgneal@45 33 for item in items:
bgneal@45 34 if item['model'] == 'band.news':
bgneal@45 35 self.process_item(item)
bgneal@45 36
bgneal@45 37 def process_item(self, item):
bgneal@45 38
bgneal@45 39 fields = item['fields']
bgneal@45 40
bgneal@45 41 content = fields['text'].strip()
bgneal@45 42 if fields['markup_enabled']:
bgneal@45 43 content = textile.textile(content, encoding='utf-8', output='utf-8')
bgneal@45 44 else:
bgneal@45 45 content = linebreaks(fields['text'])
bgneal@45 46
bgneal@45 47 author = fields['author'].strip()
bgneal@45 48 if author:
bgneal@45 49 content += u"<p>\u2013&nbsp;%s" % author
bgneal@45 50
bgneal@45 51 image = fields['photo'].strip()
bgneal@45 52 if image:
bgneal@45 53 caption = fields['photo_caption'].strip()
bgneal@45 54 caption = caption if caption else 'Image'
bgneal@45 55
bgneal@45 56 src = u"%s%s" % (settings.MEDIA_URL, image)
bgneal@45 57
bgneal@45 58 content = IMG_TAG.format(src=src, title=caption) + content
bgneal@45 59
bgneal@45 60 news = News(id=item['pk'],
bgneal@45 61 title=fields['title'].strip(),
bgneal@45 62 date=datetime.datetime.strptime(fields['date'], '%Y-%m-%d'),
bgneal@45 63 content=content)
bgneal@45 64
bgneal@45 65 news.save()