'
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()