annotate articles/management/commands/import_old_articles.py @ 130:3062c547bb90
For Django 1.6: new test discovery plus reverse now does urlquote().
My base64 keys were padded with '=' and these got quoted when doing
a reverse to generate the URL. So changed the test to look for a
quoted version of the key. This will change the URLs sent to users, but
I believe it will all be taken care of by Django.
author |
Brian Neal <bgneal@gmail.com> |
date |
Tue, 24 Dec 2013 16:47:27 -0600 |
parents |
b7cdfdde3999 |
children |
|
rev |
line source |
bgneal@47
|
1 """
|
bgneal@47
|
2 import_old_articles.py - For importing articles from the older version of this
|
bgneal@47
|
3 website.
|
bgneal@47
|
4
|
bgneal@47
|
5 """
|
bgneal@47
|
6 import datetime
|
bgneal@89
|
7 import json
|
bgneal@47
|
8
|
bgneal@47
|
9 from django.conf import settings
|
bgneal@47
|
10 from django.core.management.base import LabelCommand
|
bgneal@47
|
11 from django.utils.html import linebreaks
|
bgneal@47
|
12 import textile
|
bgneal@47
|
13
|
bgneal@47
|
14 from articles.models import Article
|
bgneal@47
|
15
|
bgneal@47
|
16
|
bgneal@47
|
17 class Command(LabelCommand):
|
bgneal@47
|
18 args = '<filename filename ...>'
|
bgneal@47
|
19 help = 'Imports older articles in JSON format'
|
bgneal@47
|
20
|
bgneal@47
|
21 def handle_label(self, filename, **options):
|
bgneal@47
|
22 """
|
bgneal@47
|
23 Process the file of older articles in JSON. Convert to the new model
|
bgneal@47
|
24 scheme.
|
bgneal@47
|
25
|
bgneal@47
|
26 """
|
bgneal@47
|
27 with open(filename, 'rb') as f:
|
bgneal@47
|
28 items = json.load(f)
|
bgneal@47
|
29
|
bgneal@47
|
30 for item in items:
|
bgneal@47
|
31 if item['model'] == 'band.article':
|
bgneal@47
|
32 self.process_item(item)
|
bgneal@47
|
33
|
bgneal@47
|
34 def process_item(self, item):
|
bgneal@47
|
35
|
bgneal@47
|
36 fields = item['fields']
|
bgneal@47
|
37
|
bgneal@47
|
38 content = fields['text'].strip()
|
bgneal@47
|
39 if fields['markup_enabled']:
|
bgneal@47
|
40 text = textile.textile(content, encoding='utf-8', output='utf-8')
|
bgneal@47
|
41 else:
|
bgneal@47
|
42 text = linebreaks(fields['text'])
|
bgneal@47
|
43
|
bgneal@47
|
44 source = linebreaks(fields['source'].strip())
|
bgneal@47
|
45
|
bgneal@47
|
46 pdf = fields['pdf'].strip()
|
bgneal@47
|
47 if pdf:
|
bgneal@47
|
48 pdf = u"%s%s" % (settings.MEDIA_URL, pdf.replace('\\', '/'))
|
bgneal@47
|
49
|
bgneal@47
|
50 article = Article(
|
bgneal@47
|
51 id=item['pk'],
|
bgneal@47
|
52 title=fields['title'].strip(),
|
bgneal@47
|
53 date=datetime.datetime.strptime(fields['date'], '%Y-%m-%d'),
|
bgneal@47
|
54 text=text,
|
bgneal@47
|
55 source=source,
|
bgneal@47
|
56 url=fields['url'].strip(),
|
bgneal@47
|
57 pdf=pdf)
|
bgneal@47
|
58
|
bgneal@47
|
59 article.save()
|