Beispiel #1
0
    def test_auto_timestamps(self):
        office = Office.objects.create(state="MD",
                                       name="House of Delegates",
                                       district="35B",
                                       chamber="lower")

        contest = Contest(
            result_type='certified',
            election_type='general',
            start_date=datetime.strptime("2012-11-06", "%Y-%m-%d"),
            end_date=datetime.strptime("2012-11-06", "%Y-%m-%d"),
            source='20121106__md__general__state_legislative.csv',
            state='MD',
            election_id='md-2012-11-06-general',
            office=office,
        )
        # Check that created and updated timestamps are auto-set when a new
        # instance is created
        self.assertIsNotNone(contest.created)
        self.assertIsNotNone(contest.updated)
        self.assert_times_almost_equal(contest.created, contest.updated)

        # Test that the timestamp is updated when the contest is saved
        updated = contest.updated
        contest.save()
        self.assertGreater(contest.updated, updated)
Beispiel #2
0
 def _get_or_create_contest(self, row, mapping):
     year = int(re.search(r'\d{4}', self.election_id).group())
     elecs = self.datasource.elections(year)[year]
     # Get election metadata by matching on election slug
     elec_meta = [e for e in elecs if e['slug'] == self.election_id][0]
     party = row['Party'].strip()
     slug = self._build_contest_slug(row)
     key = (self.election_id, slug)
     try:
         contest = self.contest_lkup[key]
     except KeyError:
         kwargs = {
             'created':
             self.timestamp,
             'updated':
             self.timestamp,
             'source':
             self.source,
             'election_id':
             self.election_id,
             'slug':
             slug,
             'state':
             self.state.upper(),
             'start_date':
             datetime.datetime.strptime(elec_meta['start_date'],
                                        "%Y-%m-%d"),
             'end_date':
             datetime.datetime.strptime(elec_meta['end_date'], "%Y-%m-%d"),
             'election_type':
             elec_meta['race_type'],
             'result_type':
             elec_meta['result_type'],
             'special':
             elec_meta['special'],
             'raw_office':
             row['Office Name'].strip(),
             'raw_district':
             row['Office District'].strip(),
         }
         contest = Contest(**kwargs)
         # Add party if it's a primary
         if 'primary' in self.election_id:
             kwargs['raw_party'] = party
         contest.save()
         self.contest_lkup[key] = contest
     return contest
Beispiel #3
0
 def test_auto_slug(self):
     office = Office(state="MD",
                     name="House of Delegates",
                     district="35B",
                     chamber="lower")
     dem = Party(name="Democrat", abbrev="DEM")
     contest = Contest(office=office, primary_party=dem)
     self.assertEqual(contest.slug, "house-of-delegates-35b-dem")
Beispiel #4
0
    def __call__(self):
        contests = []
        seen = set()

        for rr in self.get_rawresults():
            key = self._contest_key(rr)
            if key not in seen:
                fields = self.get_contest_fields(rr)
                fields['updated'] = fields['created'] = datetime.now()
                contest = Contest(**fields)
                contests.append(contest)
                seen.add(key)

        Contest.objects.insert(contests, load_bulk=False)
        print "Created %d contests." % len(contests)
Beispiel #5
0
    def validate_contests(self):
        expected_contest_slugs = self.contests
        contests = Contest.objects.filter(election_id=self.election_id)
        expected = len(expected_contest_slugs)
        count = contests.count()
        assert count == expected, (
            "There should be %d contests, but there are %d" %
            (expected, count))

        for slug in expected_contest_slugs:
            try:
                contests.get(slug=slug)
            except Contest.DoesNotExist:
                raise Contest.DoesNotExist("No contest with slug '%s' found" %
                                           slug)
    def test_auto_timestamps(self):
        office = Office.objects.create(state="MD", name= "House of Delegates", 
            district="35B", chamber="lower")

        contest = Contest(
            result_type='certified',
            election_type='general',
            start_date=datetime.strptime("2012-11-06", "%Y-%m-%d"),
            end_date=datetime.strptime("2012-11-06", "%Y-%m-%d"),
            source='20121106__md__general__state_legislative.csv',
            state='MD',
            election_id='md-2012-11-06-general',
            office=office,
        )
        # Check that created and updated timestamps are auto-set when a new
        # instance is created
        self.assertIsNotNone(contest.created)
        self.assertIsNotNone(contest.updated)
        self.assert_times_almost_equal(contest.created, contest.updated)

        # Test that the timestamp is updated when the contest is saved
        updated = contest.updated
        contest.save()
        self.assertGreater(contest.updated, updated)
Beispiel #7
0
    def __call__(self):
        contests = []
        seen = set()

        for result in self.get_raw_results():
            key = self._contest_key(result)
            if key not in seen:
                fields = self.get_contest_fields(result)
                fields['updated'] = fields['created'] = datetime.now()
                contest = Contest(**fields)
                contests.append(contest)
                seen.add(key)

        print(seen)
        Contest.objects.insert(contests, load_bulk=False)
        logger.info("Created {} contests.".format(len(contests)))