Newer
Older
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 django_fsm import FSMField
from opentech.apply.categories.models import Category, Option
from opentech.apply.categories.categories_seed import CATEGORIES
from opentech.apply.funds.models import ApplicationSubmission, FundType, Round, RoundForm, LabType, LabForm
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
from opentech.apply.funds.workflow import INITIAL_STATE
class MigrateCommand(BaseCommand):
help = "Application migration script. Requires a source JSON file."
data = []
terms = {}
# Monkey patch the status field so it is no longer protected
patched_status_field = FSMField(default=INITIAL_STATE, protected=False)
setattr(ApplicationSubmission, 'status', patched_status_field)
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):
# Prepare the list of categories.
for item in CATEGORIES:
category, _ = Category.objects.get_or_create(name=item['category'])
option, _ = Option.objects.get_or_create(value=item['name'], category=category)
self.terms[item['tid']] = option
with options['source'] as json_data:
self.data = json.load(json_data)
for id in self.data:
self.process(id)
def process(self, id):
node = self.data[id]
try:
submission = ApplicationSubmission.objects.get(drupal_id=node['nid'])
except ApplicationSubmission.DoesNotExist:
submission = ApplicationSubmission(drupal_id=node['nid'])
# TODO timezone?
submission.submit_time = datetime.fromtimestamp(int(node['created']), timezone.utc)
submission.user = self.get_user(node['uid'])
if self.CONTENT_TYPE == "fund":
FUND = FundType.objects.get(title=self.FUND_NAME)
submission.page = FUND
ROUND = Round.objects.get(title=self.ROUND_NAME)
submission.round = ROUND
if self.APPLICATION_TYPE == "request":
FORM = RoundForm.objects.get(round=ROUND)
elif self.APPLICATION_TYPE == "concept":
FORM = RoundForm.objects.filter(round=ROUND)[0]
elif self.APPLICATION_TYPE == "proposal":
FORM = RoundForm.objects.filter(round=ROUND)[1]
submission.form_fields = FORM.form.form_fields
elif self.CONTENT_TYPE == "lab":
LAB = LabType.objects.get(title=self.LAB_NAME)
submission.page = LAB
FORM = LabForm.objects.get(lab=LAB)
submission.form_fields = FORM.form.form_fields
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
submission.status = self.get_workflow_state(node)
form_data = {
'skip_account_creation_notification': True,
}
for field in node:
if field in self.STREAMFIELD_MAP:
try:
id = self.STREAMFIELD_MAP[field]['id']
form_data[id] = self.get_field_value(field, node)
except TypeError:
pass
if "value" not in form_data:
form_data["value"] = 0
submission.form_data = form_data
try:
submission.save()
self.stdout.write(f"Processed \"{node['title']}\" ({node['nid']})")
except IntegrityError:
self.stdout.write(f"Skipped \"{node['title']}\" ({node['nid']}) 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_field_value(self, field, node):
"""
Handles the following formats:
field: {(safe_)value: VALUE}
field: {target_id: ID} -- Drupal ForeignKey. Reference to other node or user entities.
field: {tid: ID} -- or term ID. fk to Categories
field: []
field: [{value|target_id|tid: VALUE},]
"""
mapping = self.STREAMFIELD_MAP[field]
mapping_type = mapping['type']
key = mapping.get('key', 'value')
source_value = node[field]
value = None
if mapping_type == "direct":
value = source_value
elif mapping_type == 'value':
value = self.nl2br(source_value[key]) if source_value else ''
elif mapping_type == 'map' and 'map' in 'mapping':
value = mapping['map'].get(source_value[key])
elif mapping_type == 'address' and 'map' in mapping:
try:
value_map = mapping['map']
value = {}
for item in value_map:
value[value_map[item]] = source_value[item]
except TypeError:
value = {}
elif mapping_type == 'boolean':
value = source_value[key] == '1' if source_value else False
elif mapping_type == 'category':
if not source_value:
value = []
else:
if isinstance(source_value, dict):
option = self.get_referenced_term(source_value[key])
value = [option] if option else []
else:
value = []
for item in source_value:
option = self.get_referenced_term(item[key])
if option:
value.append(option)
elif mapping_type == 'file':
# TODO finish mapping. Requires access to the files.
value = {}
return value
def get_referenced_term(self, tid):
try:
term = self.terms[tid]
return term.id
except KeyError:
return None
def get_referenced_node(self, nid):
pass
def get_workflow_state(self, node):
"""
workbench_moderation: {'current': {'state': STATE, 'timestamp': TS}}
"""
states_request = {
"draft": "",
"published": "in_discussion",
"in_discussion": "in_discussion",
"council_review": "internal_review",
"ready_for_reply": "post_review_discussion",
"contract_review": "post_review_discussion",
"in_contract": "accepted",
"invited_for_proposal": "accepted",
"dropped_concept_note": "rejected",
"dropped": "rejected",
"dropped_without_review": "rejected"
}
states_concept = {
"draft": "",
"published": "in_discussion",
"in_discussion": "in_discussion",
"council_review": "concept_internal_review",
"ready_for_reply": "concept_review_discussion",
"contract_review": "concept_review_discussion",
"in_contract": "invited_to_proposal",
"invited_for_proposal": "invited_to_proposal",
"dropped_concept_note": "concept_rejected",
"dropped": "concept_rejected",
"dropped_without_review": "concept_rejected"
}
states_proposal = {
"draft": "draft_proposal",
"published": "proposal_discussion",
"in_discussion": "proposal_discussion",
"council_review": "external_review",
"ready_for_reply": "post_external_review_discussion",
"contract_review": "post_external_review_discussion",
"in_contract": "proposal_accepted",
"invited_for_proposal": "proposal_accepted",
"dropped_concept_note": "proposal_rejected",
"dropped": "proposal_rejected",
"dropped_without_review": "proposal_rejected"
}
if self.APPLICATION_TYPE == "request":
workflow_state = states_request.get(node['workbench_moderation']['current']['state'], "in_discussion")
elif self.APPLICATION_TYPE == "concept":
workflow_state = states_concept.get(node['workbench_moderation']['current']['state'], "in_discussion")
elif self.APPLICATION_TYPE == "proposal":
workflow_state = states_proposal.get(node['workbench_moderation']['current']['state'], "draft_proposal")
else:
workflow_state = None
return workflow_state
def nl2br(self, value):
return value.replace('\r\n', '<br>\n')