Skip to content

Commit

Permalink
do not add responses by default for national data import
Browse files Browse the repository at this point in the history
should make testing it a bit less fraught
  • Loading branch information
struan committed Dec 10, 2024
1 parent e8c1164 commit 31200b1
Showing 1 changed file with 53 additions and 22 deletions.
75 changes: 53 additions & 22 deletions crowdsourcer/management/commands/import_national_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ def add_arguments(self, parser):
help="JSON file containing the configuration for national points",
)

parser.add_argument(
"--commit",
action="store_true",
help="Save the responses to the database",
)

def add_options(self, q, details):
if details.get("type", None) is not None:
expected_options = 2
Expand Down Expand Up @@ -265,6 +271,9 @@ def import_answers(self, user, rt, df, q, details):
if not self.quiet:
self.print_info(f"{authority.name}: {option}")

if self.check_options_only:
continue

if details.get("update_points_only", False):
try:
r = Response.objects.get(
Expand Down Expand Up @@ -311,29 +320,37 @@ def import_answers(self, user, rt, df, q, details):
count += 1
if details.get("default_if_missing", None) is not None:
default = details["default_if_missing"]
if type(default) is int:
option = Option.objects.get(question=q, score=default)
else:
option = Option.objects.get(question=q, description=default)
groups = q.questiongroup.all()
answered = Response.objects.filter(response_type=rt, question=q).values(
"authority"
)
councils = PublicAuthority.objects.filter(
questiongroup__in=groups
).exclude(id__in=answered)
if details.get("missing_filter", None) is not None:
councils = councils.filter(**details["missing_filter"])

for council in councils:
r, _ = Response.objects.update_or_create(
question=q,
authority=council,
user=user,
response_type=rt,
defaults={"option": option},
try:
if type(default) is int:
option = Option.objects.get(question=q, score=default)
else:
option = Option.objects.get(question=q, description=default)
except Option.DoesNotExist:
self.print_info(
f"{YELLOW}No matching default response for {q.number}, {default}{NOBOLD}",
1,
)
auto_zero += 1

if option and not self.check_options_only:
groups = q.questiongroup.all()
answered = Response.objects.filter(
response_type=rt, question=q
).values("authority")
councils = PublicAuthority.objects.filter(
questiongroup__in=groups
).exclude(id__in=answered)
if details.get("missing_filter", None) is not None:
councils = councils.filter(**details["missing_filter"])

for council in councils:
r, _ = Response.objects.update_or_create(
question=q,
authority=council,
user=user,
response_type=rt,
defaults={"option": option},
)
auto_zero += 1

message = f"{GREEN}Added {count} responses, {auto_zero} default 0 responses, bad authorities {bad_authority_count}{NOBOLD}"

Expand Down Expand Up @@ -375,11 +392,13 @@ def handle(
quiet: bool = False,
only_sheet: str = None,
negative_only: bool = False,
commit: bool = False,
*args,
**kwargs,
):
self.quiet = quiet

self.check_options_only = not commit
self.question_file = settings.BASE_DIR / "data" / kwargs["file"]
self.config_file = settings.BASE_DIR / "data" / kwargs["config"]

Expand All @@ -395,6 +414,12 @@ def handle(
self.session = MarkingSession.objects.get(label=kwargs["session"])
self.add_options = kwargs["add_options"]

if self.check_options_only:
self.print_info(
f"{YELLOW}Not saving any responses, run with --commit to do so{NOBOLD}",
1,
)

for details in self.sheets:
sheet = details["sheet"]
if only_sheet is not None and sheet != only_sheet:
Expand All @@ -414,3 +439,9 @@ def handle(
continue

self.handle_sheet(sheet, details, user)

if self.check_options_only:
self.print_info(
f"{YELLOW}Not saving any responses, run with --commit to do so{NOBOLD}",
1,
)

0 comments on commit 31200b1

Please sign in to comment.