def handle(self, *args, **options):
        """
        This management command gets data for a given election date from
        elex. Then, it loops through each row of the data and calls
        `process_row`.

        In order for this command to work, you must have bootstrapped all
        of the dependent apps: entity, geography, government, election, vote,
        and almanac.
        """
        self.senate_class = options["senate_class"]

        writefile = open("bootstrap.json", "w")
        elex_args = [
            "elex",
            "results",
            options["election_date"],
            "-o",
            "json",
            "--national-only",
        ]
        if options["test"]:
            elex_args.append("-t")
        subprocess.run(elex_args, stdout=writefile)

        with open("bootstrap.json", "r") as readfile:
            data = json.load(readfile)
            candidates = collections.defaultdict(list)
            for d in data:
                key = "{0} {1}: {2}, {3}".format(
                    d["officename"], d["statename"], d["last"], d["first"]
                )
                candidates[key].append(d)
            for candidate_races in tqdm(
                candidates.values(), desc="Candidates"
            ):
                tqdm.write(
                    "{0} {1}: {2}, {3}".format(
                        candidate_races[0]["statename"],
                        candidate_races[0]["officename"],
                        candidate_races[0]["last"],
                        candidate_races[0]["first"],
                    )
                )
                for race in tqdm(
                    candidate_races, desc="Contests", leave=False
                ):
                    if race["level"] == geography.DivisionLevel.TOWNSHIP:
                        continue
                    # TODO: Check this with Tyler
                    if not race.get("level", None):
                        continue

                    if race["is_ballot_measure"]:
                        continue

                    self.process_row(race)
def handle ( self , * args , ** options ) : self . senate_class = options [ "senate_class" ] writefile = open ( "bootstrap.json" , "w" ) elex_args = [ "elex" , "results" , options [ "election_date" ] , "-o" , "json" , "--national-only" , ] if options [ "test" ] : elex_args . append ( "-t" ) subprocess . run ( elex_args , stdout = writefile ) with open ( "bootstrap.json" , "r" ) as readfile : data = json . load ( readfile ) candidates = collections . defaultdict ( list ) for d in data : key = "{0} {1}: {2}, {3}" . format ( d [ "officename" ] , d [ "statename" ] , d [ "last" ] , d [ "first" ] ) candidates [ key ] . append ( d ) for candidate_races in tqdm ( candidates . values ( ) , desc = "Candidates" ) : tqdm . write ( "{0} {1}: {2}, {3}" . format ( candidate_races [ 0 ] [ "statename" ] , candidate_races [ 0 ] [ "officename" ] , candidate_races [ 0 ] [ "last" ] , candidate_races [ 0 ] [ "first" ] , ) ) for race in tqdm ( candidate_races , desc = "Contests" , leave = False ) : if race [ "level" ] == geography . DivisionLevel . TOWNSHIP : continue if not race . get ( "level" , None ) : continue if race [ "is_ballot_measure" ] : continue self . process_row ( race )
This management command gets data for a given election date from
        elex. Then, it loops through each row of the data and calls
        `process_row`.

        In order for this command to work, you must have bootstrapped all
        of the dependent apps: entity, geography, government, election, vote,
        and almanac.