<commit_before>from django.apps import apps
from django.core.management.base import BaseCommand

from addressbase.models import UprnToCouncil
from councils.models import Council
from data_importers.models import DataQuality
from pollingstations.models import PollingStation, PollingDistrict

"""
Clear PollingDistrict and PollingStation models
Clear polling_station_id field in UprnToCouncil model
Clear report, num_addresses, num_districts and num_stations
fields in DataQuality model
"""


class Command(BaseCommand):

    """
    Turn off auto system check for all apps
    We will maunally run system checks only for the
    'data_importers' and 'pollingstations' apps
    """

    requires_system_checks = []

    def add_arguments(self, parser):
        group = parser.add_mutually_exclusive_group(required=True)

        group.add_argument(
            "-c",
            "--council",
            nargs=1,
            help="Council ID to clear in the format X01000001",
        )

        group.add_argument(
            "-a",
            "--all",
            help="Clear data for all councils (will completely ruin your database)",
            action="store_true",
            default=False,
        )

    def handle(self, *args, **kwargs):
        """
        Manually run system checks for the
        'data_importers' and 'pollingstations' apps
        Management commands can ignore checks that only apply to
        the apps supporting the website part of the project
        """
        self.check(
            [
                apps.get_app_config("data_importers"),
                apps.get_app_config("pollingstations"),
            ]
        )

        if kwargs["council"]:
            for council_id in kwargs["council"]:
                print("Deleting data for council %s..." % (council_id))
                # check this council exists
                council_obj = Council.objects.get(pk=council_id)
                gss_code = council_obj.geography.gss

                PollingStation.objects.filter(council=council_id).delete()
                PollingDistrict.objects.filter(council=council_id).delete()

                UprnToCouncil.objects.filter(lad=gss_code).update(polling_station_id="")

                dq = DataQuality.objects.get(council_id=council_id)
                dq.report = ""
                dq.num_addresses = 0
                dq.num_districts = 0
                dq.num_stations = 0
                dq.save()
                print("..done")

        elif kwargs.get("all"):
            print("Deleting ALL data...")
            PollingDistrict.objects.all().delete()
            PollingStation.objects.all().delete()

            UprnToCouncil.objects.exclude(polling_station_id="").update(
                polling_station_id=""
            )<commit_msg>Add AdvanceVotingStations to teardown command<commit_after>from django.apps import apps
from django.core.management.base import BaseCommand

from addressbase.models import UprnToCouncil
from councils.models import Council
from data_importers.models import DataQuality
from pollingstations.models import PollingStation, PollingDistrict, AdvanceVotingStation

"""
Clear PollingDistrict, PollingStation, and AdvancedVotingStation models
Clear polling_station_id field in UprnToCouncil model
Clear report, num_addresses, num_districts and num_stations
fields in DataQuality model
"""


class Command(BaseCommand):

    """
    Turn off auto system check for all apps
    We will maunally run system checks only for the
    'data_importers' and 'pollingstations' apps
    """

    requires_system_checks = []

    def add_arguments(self, parser):
        group = parser.add_mutually_exclusive_group(required=True)

        group.add_argument(
            "-c",
            "--council",
            nargs=1,
            help="Council ID to clear in the format X01000001",
        )

        group.add_argument(
            "-a",
            "--all",
            help="Clear data for all councils (will completely ruin your database)",
            action="store_true",
            default=False,
        )

    def handle(self, *args, **kwargs):
        """
        Manually run system checks for the
        'data_importers' and 'pollingstations' apps
        Management commands can ignore checks that only apply to
        the apps supporting the website part of the project
        """
        self.check(
            [
                apps.get_app_config("data_importers"),
                apps.get_app_config("pollingstations"),
            ]
        )

        if kwargs["council"]:
            for council_id in kwargs["council"]:
                print("Deleting data for council %s..." % (council_id))
                # check this council exists
                council_obj = Council.objects.get(pk=council_id)
                gss_code = council_obj.geography.gss

                PollingStation.objects.filter(council=council_id).delete()
                PollingDistrict.objects.filter(council=council_id).delete()
                AdvanceVotingStation.objects.filter(council=council_id).delete()

                UprnToCouncil.objects.filter(lad=gss_code).update(polling_station_id="")

                dq = DataQuality.objects.get(council_id=council_id)
                dq.report = ""
                dq.num_addresses = 0
                dq.num_districts = 0
                dq.num_stations = 0
                dq.save()
                print("..done")

        elif kwargs.get("all"):
            print("Deleting ALL data...")
            PollingDistrict.objects.all().delete()
            PollingStation.objects.all().delete()
            AdvanceVotingStation.objects.all().delete()

            UprnToCouncil.objects.exclude(polling_station_id="").update(
                polling_station_id=""
            )
{'fraction_non_alphanumeric': 0.08739033201445037, 'fraction_numerical': 0.0041286771030449, 'mean_word_length': 2.134231805929919, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1919073', 'n_tokens_mistral': 1691, 'n_tokens_neox': 1530, 'n_words': 368}