1
1
from collections import defaultdict
2
2
3
3
from django .contrib .auth .models import User
4
- from django .core .management .base import BaseCommand
5
4
6
5
import pandas as pd
7
6
7
+ from crowdsourcer .import_utils import BaseImporter
8
8
from crowdsourcer .models import (
9
9
MarkingSession ,
10
10
Option ,
14
14
ResponseType ,
15
15
)
16
16
17
- YELLOW = "\033 [33m"
18
- RED = "\033 [31m"
19
- GREEN = "\033 [32m"
20
- NOBOLD = "\033 [0m"
21
17
22
-
23
- class Command (BaseCommand ):
18
+ class Command (BaseImporter ):
24
19
help = "Add automatic points"
25
20
26
21
def add_arguments (self , parser ):
@@ -71,12 +66,6 @@ def add_arguments(self, parser):
71
66
"--commit" , action = "store_true" , help = "Commits changes to DB"
72
67
)
73
68
74
- def print_info (self , message , level = 2 ):
75
- if self .quiet and level > 1 :
76
- return
77
-
78
- self .stdout .write (message )
79
-
80
69
def get_points (self , file ):
81
70
df = pd .read_csv (file )
82
71
df ["answer in GRACE" ] = df ["answer in GRACE" ].astype (str )
@@ -126,7 +115,7 @@ def scrub_council_type(self, types):
126
115
if type_map .get (t ) is not None :
127
116
scrubbed .append (type_map [t ])
128
117
else :
129
- self .print_info (f"bad council type { t } " , 1 )
118
+ self .print_error (f"bad council type { t } " )
130
119
return scrubbed
131
120
132
121
def get_mapped_answer (self , answer , q , answer_map ):
@@ -232,9 +221,9 @@ def handle(
232
221
** q_args ,
233
222
)
234
223
except Question .DoesNotExist :
235
- self .print_info (
224
+ self .print_error (
236
225
f"no matching question for { point ['section' ]} , { q_args } "
237
- ), 1
226
+ )
238
227
continue
239
228
240
229
copy_last_year = False
@@ -250,9 +239,8 @@ def handle(
250
239
try :
251
240
option = Option .objects .get (question = question , description = answer )
252
241
except Option .DoesNotExist :
253
- self .print_info (
254
- f"no matching option for { question .number_and_part } , { point ['section' ]} - '{ answer } '" ,
255
- 1 ,
242
+ self .print_error (
243
+ f"no matching option for { question .number_and_part } , { point ['section' ]} - '{ answer } ' { point ['answer in GRACE' ]} "
256
244
)
257
245
continue
258
246
@@ -269,12 +257,12 @@ def handle(
269
257
response_type = prev_rt ,
270
258
)
271
259
except Response .DoesNotExist :
272
- self .print_info (
260
+ self .print_error (
273
261
f"no previous response exists for { council .name } for { question .number_and_part } , { question .section .title } "
274
262
)
275
263
continue
276
264
except Response .MultipleObjectsReturned :
277
- self .print_info (
265
+ self .print_error (
278
266
f"multiple previous responses exist for { council .name } for { question .number_and_part } , { question .section .title } "
279
267
)
280
268
continue
@@ -298,9 +286,8 @@ def handle(
298
286
question = question , description = answer
299
287
)
300
288
except Option .DoesNotExist :
301
- self .print_info (
302
- f"no matching option for { question .number_and_part } , { point ['section' ]} - '{ prev_response .option .description } '" ,
303
- 1 ,
289
+ self .print_error (
290
+ f"no matching option for { question .number_and_part } , { point ['section' ]} - '{ prev_response .option .description } '"
304
291
)
305
292
continue
306
293
@@ -313,14 +300,12 @@ def handle(
313
300
options = [x .id for x in response .multi_option .all ()]
314
301
if option .id not in options :
315
302
self .print_info (
316
- f"{ YELLOW } existing response does not contain expected response for { question .number_and_part } , { point ['section' ]} , { council .name } { NOBOLD } " ,
317
- 1 ,
303
+ f"existing response does not contain expected response for { question .number_and_part } , { point ['section' ]} , { council .name } "
318
304
)
319
305
else :
320
306
if response .option != option :
321
307
self .print_info (
322
- f"{ YELLOW } different existing response for { question .number_and_part } , { point ['section' ]} , { council .name } { NOBOLD } " ,
323
- 1 ,
308
+ f"different existing response for { question .number_and_part } , { point ['section' ]} , { council .name } "
324
309
)
325
310
if point .get ("override_response" , None ) is not None :
326
311
override_response = True
@@ -368,7 +353,7 @@ def handle(
368
353
369
354
if add_response :
370
355
responses_added += 1
371
- self .print_info (
356
+ self .print_debug (
372
357
f"creating response for { council .name } for { question .number_and_part } , { question .section .title } "
373
358
)
374
359
@@ -387,8 +372,7 @@ def handle(
387
372
elif override_response or update_existing_responses :
388
373
responses_overidden += 1
389
374
self .print_info (
390
- f"overriding response for { council .name } for { question .number_and_part } , { question .section .title } " ,
391
- 1 ,
375
+ f"overriding response for { council .name } for { question .number_and_part } , { question .section .title } "
392
376
)
393
377
394
378
if question .question_type != "multiple_choice" :
@@ -414,12 +398,8 @@ def handle(
414
398
else :
415
399
response .multi_option .add (option .id )
416
400
417
- self .print_info (
418
- f"{ GREEN } Added { responses_added } responses for { question .section .title } { question .number_and_part } , { existing_responses } existing responses, { responses_overidden } responses overridden{ NOBOLD } " ,
419
- 1 ,
401
+ self .print_success (
402
+ f"Added { responses_added } responses for { question .section .title } { question .number_and_part } , { existing_responses } existing responses, { responses_overidden } responses overridden" ,
420
403
)
421
404
if not commit :
422
- self .print_info (
423
- f"{ YELLOW } call with --commit to commit changed to database{ NOBOLD } " ,
424
- 1 ,
425
- )
405
+ self .print_info ("call with --commit to commit changed to database" )
0 commit comments