Esempio n. 1
0
    def date(self):
        """
        Use a scraped candidate election name to look up the election date.

        Return a timezone aware date object, if found, else None.
        """
        # If this is the 2008, we have a hacked out edge case solution
        if self.name == '2008 PRIMARY':
            return date(2008, 6, 3)

        try:
            # Check if the date is in our hardcoded list of special election
            dt = special_elections.names_to_dates_dict[self.name]
            return timezone.datetime.strptime(dt, '%Y-%m-%d').date()
        except KeyError:
            pass

        # If not check the alternative list kept by the scraped IncumbentElection model
        try:
            incumbent = IncumbentElection.objects.get(
                date__year=self.parsed_name['year'],
                name__icontains=self.parsed_name['type'],
            )
            return incumbent.date
        except (IncumbentElection.DoesNotExist, IncumbentElection.MultipleObjectsReturned):
            pass

        # If that doesn't work either, try parsing the election date from the name
        try:
            return get_expected_election_date(
                self.date.year, self.election_type
            )
        except:
            # If that fails, just give up and return None
            return None
    def ocd_election(self):
        """
        Return Election occurring in year with name in including election_type.

        Return None if none found.
        """
        from calaccess_processed.models import OCDElectionProxy

        if not self.election_year or not self.election_type:
            return None
        try:
            return OCDElectionProxy.objects.get(
                date__year=self.election_year,
                name__contains=self.election_type,
            )
        except (OCDElectionProxy.DoesNotExist,
                OCDElectionProxy.MultipleObjectsReturned):
            # if it's a future primary, try to calculate the date
            if self.election_year >= date.today(
            ).year and self.election_type == 'PRIMARY':
                try:
                    dt_obj = calaccess_processed.get_expected_election_date(
                        self.election_year, self.election_type)
                except:
                    return None
                return OCDElectionProxy.objects.create_from_calaccess(
                    '{0} {1}'.format(self.election_year, self.election_type),
                    dt_obj,
                    election_type=self.election_type,
                )
            else:
                return None
Esempio n. 3
0
 def test_2010_general_date(self):
     """
     Confirm correct calculation of 2010 general date.
     """
     self.assertEqual(
         date(2010, 11, 2),
         calaccess_processed.get_expected_election_date(2010, 'GENERAL'),
     )
Esempio n. 4
0
 def test_2014_primary_date(self):
     """
     Confirm correct calculation of 2014 primary date.
     """
     self.assertEqual(
         date(2014, 6, 3),
         calaccess_processed.get_expected_election_date(2014, 'PRIMARY'),
     )