Mercurial > public > madeira
comparison articles/management/commands/import_old_articles.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/articles/management/commands/import_old_articles.py@0e51e5be34b9 |
children | b7cdfdde3999 |
comparison
equal
deleted
inserted
replaced
70:f26cdda0ad8b | 71:e2868ad47a1e |
---|---|
1 """ | |
2 import_old_articles.py - For importing articles 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 articles.models import Article | |
15 | |
16 | |
17 class Command(LabelCommand): | |
18 args = '<filename filename ...>' | |
19 help = 'Imports older articles in JSON format' | |
20 | |
21 def handle_label(self, filename, **options): | |
22 """ | |
23 Process the file of older articles in JSON. Convert to the new model | |
24 scheme. | |
25 | |
26 """ | |
27 with open(filename, 'rb') as f: | |
28 items = json.load(f) | |
29 | |
30 for item in items: | |
31 if item['model'] == 'band.article': | |
32 self.process_item(item) | |
33 | |
34 def process_item(self, item): | |
35 | |
36 fields = item['fields'] | |
37 | |
38 content = fields['text'].strip() | |
39 if fields['markup_enabled']: | |
40 text = textile.textile(content, encoding='utf-8', output='utf-8') | |
41 else: | |
42 text = linebreaks(fields['text']) | |
43 | |
44 source = linebreaks(fields['source'].strip()) | |
45 | |
46 pdf = fields['pdf'].strip() | |
47 if pdf: | |
48 pdf = u"%s%s" % (settings.MEDIA_URL, pdf.replace('\\', '/')) | |
49 | |
50 article = Article( | |
51 id=item['pk'], | |
52 title=fields['title'].strip(), | |
53 date=datetime.datetime.strptime(fields['date'], '%Y-%m-%d'), | |
54 text=text, | |
55 source=source, | |
56 url=fields['url'].strip(), | |
57 pdf=pdf) | |
58 | |
59 article.save() |