Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import argparse
import json
from datetime import datetime, timezone
from django.contrib.auth import get_user_model
from django.core.management.base import BaseCommand
from django.db import transaction
from django.db.utils import IntegrityError
from opentech.apply.activity.models import Activity
from opentech.apply.funds.models import ApplicationSubmission
class Command(BaseCommand):
help = "Comment migration script. Requires a source JSON file."
data = []
def add_arguments(self, parser):
parser.add_argument('source', type=argparse.FileType('r'), help='Migration source JSON file')
@transaction.atomic
def handle(self, *args, **options):
with options['source'] as json_data:
self.data = json.load(json_data)
for id in self.data:
self.process(id)
def process(self, id):
comment = self.data[id]
activity = Activity.objects.create(
timestamp=datetime.fromtimestamp(int(comment['created']), timezone.utc),
user=self.get_user(comment['uid']),
submission=self.get_submission(comment['nid']),
message=self.get_message(comment['subject'], comment['comment_body']['safe_value']),
)
try:
activity.save()
self.stdout.write(f"Processed \"{comment['subject']}\" ({comment['cid']})")
except IntegrityError:
self.stdout.write(f"Skipped \"{comment['subject']}\" ({comment['cid']}) due to IntegrityError")
pass
def get_user(self, uid):
try:
User = get_user_model()
return User.objects.get(drupal_id=uid)
except User.DoesNotExist:
return None
def get_message(self, comment_subject, comment_body):
message = f"<p>{comment_subject}</p>{comment_body}"
return self.nl2br(message)
def get_submission(self, nid):
try:
return ApplicationSubmission.objects.get(drupal_id=nid)
except ApplicationSubmission.DoesNotExist:
return 'None'
def nl2br(self, value):
return value.replace('\r\n', '<br>\n')