Skip to content

Commit 7b6114e

Browse files
committed
update the auto points script to use the base importer
so far only uses the print functions
1 parent d422c0e commit 7b6114e

File tree

1 file changed

+18
-38
lines changed

1 file changed

+18
-38
lines changed

crowdsourcer/management/commands/add_automatic_points.py

+18-38
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
from collections import defaultdict
22

33
from django.contrib.auth.models import User
4-
from django.core.management.base import BaseCommand
54

65
import pandas as pd
76

7+
from crowdsourcer.import_utils import BaseImporter
88
from crowdsourcer.models import (
99
MarkingSession,
1010
Option,
@@ -14,13 +14,8 @@
1414
ResponseType,
1515
)
1616

17-
YELLOW = "\033[33m"
18-
RED = "\033[31m"
19-
GREEN = "\033[32m"
20-
NOBOLD = "\033[0m"
2117

22-
23-
class Command(BaseCommand):
18+
class Command(BaseImporter):
2419
help = "Add automatic points"
2520

2621
def add_arguments(self, parser):
@@ -71,12 +66,6 @@ def add_arguments(self, parser):
7166
"--commit", action="store_true", help="Commits changes to DB"
7267
)
7368

74-
def print_info(self, message, level=2):
75-
if self.quiet and level > 1:
76-
return
77-
78-
self.stdout.write(message)
79-
8069
def get_points(self, file):
8170
df = pd.read_csv(file)
8271
df["answer in GRACE"] = df["answer in GRACE"].astype(str)
@@ -126,7 +115,7 @@ def scrub_council_type(self, types):
126115
if type_map.get(t) is not None:
127116
scrubbed.append(type_map[t])
128117
else:
129-
self.print_info(f"bad council type {t}", 1)
118+
self.print_error(f"bad council type {t}")
130119
return scrubbed
131120

132121
def get_mapped_answer(self, answer, q, answer_map):
@@ -232,9 +221,9 @@ def handle(
232221
**q_args,
233222
)
234223
except Question.DoesNotExist:
235-
self.print_info(
224+
self.print_error(
236225
f"no matching question for {point['section']}, {q_args}"
237-
), 1
226+
)
238227
continue
239228

240229
copy_last_year = False
@@ -250,9 +239,8 @@ def handle(
250239
try:
251240
option = Option.objects.get(question=question, description=answer)
252241
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']}"
256244
)
257245
continue
258246

@@ -269,12 +257,12 @@ def handle(
269257
response_type=prev_rt,
270258
)
271259
except Response.DoesNotExist:
272-
self.print_info(
260+
self.print_error(
273261
f"no previous response exists for {council.name} for {question.number_and_part}, {question.section.title}"
274262
)
275263
continue
276264
except Response.MultipleObjectsReturned:
277-
self.print_info(
265+
self.print_error(
278266
f"multiple previous responses exist for {council.name} for {question.number_and_part}, {question.section.title}"
279267
)
280268
continue
@@ -298,9 +286,8 @@ def handle(
298286
question=question, description=answer
299287
)
300288
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}'"
304291
)
305292
continue
306293

@@ -313,14 +300,12 @@ def handle(
313300
options = [x.id for x in response.multi_option.all()]
314301
if option.id not in options:
315302
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}"
318304
)
319305
else:
320306
if response.option != option:
321307
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}"
324309
)
325310
if point.get("override_response", None) is not None:
326311
override_response = True
@@ -368,7 +353,7 @@ def handle(
368353

369354
if add_response:
370355
responses_added += 1
371-
self.print_info(
356+
self.print_debug(
372357
f"creating response for {council.name} for {question.number_and_part}, {question.section.title}"
373358
)
374359

@@ -387,8 +372,7 @@ def handle(
387372
elif override_response or update_existing_responses:
388373
responses_overidden += 1
389374
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}"
392376
)
393377

394378
if question.question_type != "multiple_choice":
@@ -414,12 +398,8 @@ def handle(
414398
else:
415399
response.multi_option.add(option.id)
416400

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",
420403
)
421404
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

Comments
 (0)