<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
OCD Election Contest-related models.
"""
from __future__ import unicode_literals
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
from calaccess_processed.models.scraped import ScrapedProposition
from calaccess_processed.models.opencivicdata.elections import Election
from calaccess_processed.models.opencivicdata.division import Division
from calaccess_processed.models.opencivicdata.base import (
    OCDIDField,
    OCDBase,
)
import re
from datetime import datetime


class ContestBase(OCDBase):
    """
    An abstract base class with properties shared by all contest types.
    """
    name = models.CharField(
        max_length=300,
        help_text='Name of the contest, not necessarily as it appears on the '
                  'ballot.'
    )
    division = models.ForeignKey(
        'Division',
        related_name='%(class)s_contests',
        help_text='Reference to the OCD ``Division`` that defines the '
                  'geographical scope of the contest, e.g., a specific '
                  'Congressional or State Senate district.',
    )
    election = models.ForeignKey(
        'Election',
        related_name='%(class)s_contests',
        help_text='Reference to the OCD ``Election`` in which the contest is '
                  'decided.',
    )

    class Meta:
        """
        Model options.
        """
        abstract = True


class BallotMeasureContestManager(models.Manager):
    """
    Manager with custom methods for OCD ballot measure contest.
    """
    def load_raw_data(self):
        """
        Load BallotMeasureContest model from ScrapedProposition.
        """
        self.all().delete()

        date_name_regex = r'^(?P<date>[A-Z]+\s\d{1,2},\s\d{4})\s(?P<name>.+)'

        for p in ScrapedProposition.objects.all():
            # Get the election
            match = re.match(date_name_regex, p.election.name)
            dt_obj = datetime.strptime(
                match.groupdict()['date'],
                '%B %d, %Y',
            )
            election_obj = Election.objects.get(
                start_time=dt_obj,
                name='{0} {1}'.format(
                    dt_obj.year,
                    match.groupdict()['name']
                )
            )

            # Get the division: CA statewide
            division_obj = Division.objects.get(
                id='ocd-division/country:us/state:ca'
            )

            # Measure is either an initiative or a referendum
            ballot_measure_type = ''
            if 'REFERENDUM' in p.name:
                ballot_measure_type = 'r'
            elif 'RECALL' in p.name:<commit_msg>Get Election via ElectionIdentifier #40<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
OCD Election Contest-related models.
"""
from __future__ import unicode_literals
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
from calaccess_processed.models.scraped import ScrapedProposition
from calaccess_processed.models.opencivicdata.elections import (
    ElectionIdentifier,
)
from calaccess_processed.models.opencivicdata.division import Division
from calaccess_processed.models.opencivicdata.base import (
    OCDIDField,
    OCDBase,
)


class ContestBase(OCDBase):
    """
    An abstract base class with properties shared by all contest types.
    """
    name = models.CharField(
        max_length=300,
        help_text='Name of the contest, not necessarily as it appears on the '
                  'ballot.'
    )
    division = models.ForeignKey(
        'Division',
        related_name='%(class)s_contests',
        help_text='Reference to the OCD ``Division`` that defines the '
                  'geographical scope of the contest, e.g., a specific '
                  'Congressional or State Senate district.',
    )
    election = models.ForeignKey(
        'Election',
        related_name='%(class)s_contests',
        help_text='Reference to the OCD ``Election`` in which the contest is '
                  'decided.',
    )

    class Meta:
        """
        Model options.
        """
        abstract = True


class BallotMeasureContestManager(models.Manager):
    """
    Manager with custom methods for OCD ballot measure contest.
    """
    def load_raw_data(self):
        """
        Load BallotMeasureContest model from ScrapedProposition.
        """
        self.all().delete()

        for p in ScrapedProposition.objects.all():
            # Get the election
            election_obj = ElectionIdentifier.objects.get(
                scheme='PropositionScrapedElection.id',
                identifier=str(p.election_id),
            ).election

            # Get the division: CA statewide
            division_obj = Division.objects.get(
                id='ocd-division/country:us/state:ca'
            )

            # Measure is either an initiative or a referendum
            ballot_measure_type = ''
            if 'REFERENDUM' in p.name:
                ballot_measure_type = 'r'
            elif 'RECALL' in p.name:
{'fraction_non_alphanumeric': 0.09552529182879377, 'fraction_numerical': 0.0033073929961089494, 'mean_word_length': 2.1714990746452805, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '676562', 'n_tokens_mistral': 1486, 'n_tokens_neox': 1382, 'n_words': 360}