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