From 31200b18e0f4dd0cda7ba7615c33d3d004b38394 Mon Sep 17 00:00:00 2001 From: Struan Donald Date: Tue, 10 Dec 2024 17:43:59 +0000 Subject: [PATCH] do not add responses by default for national data import should make testing it a bit less fraught --- .../commands/import_national_data.py | 75 +++++++++++++------ 1 file changed, 53 insertions(+), 22 deletions(-) diff --git a/crowdsourcer/management/commands/import_national_data.py b/crowdsourcer/management/commands/import_national_data.py index ecfa0820..3d1b8830 100644 --- a/crowdsourcer/management/commands/import_national_data.py +++ b/crowdsourcer/management/commands/import_national_data.py @@ -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 @@ -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( @@ -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}" @@ -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"] @@ -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: @@ -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, + )