<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Load the Candidacy models from records extracted from Form501Filings.
"""
from calaccess_raw.models import FilerToFilerTypeCd, LookupCodesCd
from calaccess_processed.management.commands import LoadOCDModelsCommand
from calaccess_processed.models import Form501Filing
from opencivicdata.models import (
    CandidateContest,
    Candidacy,
    Division,
    Election,
)


class Command(LoadOCDModelsCommand):
    """
    Load the Candidacy models from records extracted from Form501Filings.
    """
    help = 'Load the Candidacy models from records extracted from Form501Filings.'

    def handle(self, *args, **options):
        """
        Make it happen.
        """
        super(Command, self).handle(*args, **options)
        self.header("Loading additional candidacies from Form 501 filings")
        self.load()
        self.success("Done!")

    def get_election(self, year, election_type):
        """
        Return Election occurring in year with name in including election_type.

        Return None if none found.
        """
        try:
            election = Election.objects.get(
                start_time__year=year,<commit_msg>Check if contests loaded before inserting candidates from 501s #69<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Load the Candidacy models from records extracted from Form501Filings.
"""
from django.core.management.base import CommandError
from calaccess_raw.models import FilerToFilerTypeCd, LookupCodesCd
from calaccess_processed.management.commands import LoadOCDModelsCommand
from calaccess_processed.models import Form501Filing
from opencivicdata.models import (
    CandidateContest,
    Candidacy,
    Division,
    Election,
)


class Command(LoadOCDModelsCommand):
    """
    Load the Candidacy models from records extracted from Form501Filings.
    """
    help = 'Load the Candidacy models from records extracted from Form501Filings.'

    def handle(self, *args, **options):
        """
        Make it happen.
        """
        super(Command, self).handle(*args, **options)

        if not CandidateContest.objects.exists():
            error_message = 'No contests currently loaded (run loadcandidatecontests).'
            if self._called_from_command_line:
                self.failure(error_message)
            else:
                raise CommandError(error_message)
        else:
            self.header("Loading additional candidacies from Form 501 filings")
            self.load()
            self.success("Done!")

    def get_election(self, year, election_type):
        """
        Return Election occurring in year with name in including election_type.

        Return None if none found.
        """
        try:
            election = Election.objects.get(
                start_time__year=year,
{'fraction_non_alphanumeric': 0.08473389355742297, 'fraction_numerical': 0.012955182072829132, 'mean_word_length': 2.8348993288590605, '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': '6987548', 'n_tokens_mistral': 797, 'n_tokens_neox': 745, 'n_words': 221}