示例#1
0
    def test_distribution_by_election(self):
        tz = timezone(settings.TIME_ZONE)
        elections = Election.objects.all()

        # for each election, grab the report and initialize a counter for CenterOpen objects
        for election in elections:
            election.report = retrieve_report(
                election_key(ELECTION_DAY_REPORT_KEY, election))
            election.center_opens_found = 0

        # for each CenterOpen in the db, bump the counter for the particular election
        for center_open in CenterOpen.objects.all():
            open_date_str = astz(center_open.creation_date,
                                 tz).strftime('%Y-%m-%d')
            for election in elections:
                if open_date_str in election.report['dates']:
                    election.center_opens_found += 1

        for election in elections:
            center_opens_expected = 0
            for d in election.report['dates']:
                for office_data in election.report['by_office'].values():
                    if d in office_data and 'opened' in office_data[d]:
                        center_opens_expected += office_data[d]['opened']
            self.assertEqual(center_opens_expected,
                             election.center_opens_found)
示例#2
0
def load_election_day_report(election, data_out):
    election_day_dt = election.polling_start_time
    election_day_dt = astz(election_day_dt, timezone(settings.TIME_ZONE))
    election_day = election_day_dt.strftime('%Y-%m-%d')

    day_after_election_day_dt = election_day_dt + timedelta(days=1)
    day_after_election_day = day_after_election_day_dt.strftime('%Y-%m-%d')

    polling_centers_by_office = generate_centers_by_office(data_out['offices'],
                                                           data_out['by_polling_center'])

    metadata = {
        'offices': data_out['offices'],
        'centers_by_office': polling_centers_by_office,
        'election_day': election_day,
        'dates': data_out['dates'],
        'last_updated': data_out['last_updated']
    }
    offices_table = generate_offices_table(data_out['offices'], deepcopy(data_out['by_office']),
                                           data_out['by_polling_center'],
                                           election_day, day_after_election_day)
    polling_centers_table = deepcopy(data_out['by_polling_center'])
    update_polling_centers_table(data_out['dates'], polling_centers_table, election,
                                 election_day_dt, election_day, day_after_election_day)

    centers = [polling_centers_table[key] for key in sorted(polling_centers_table.keys())]
    pipe = report_store.pipeline(transaction=False)
    pipe.set(redis_key(election_key(ELECTION_DAY_REPORT_KEY, election)), json.dumps(data_out))
    pipe.set(redis_key(election_key(ELECTION_DAY_BY_COUNTRY_KEY, election)),
             json.dumps(data_out['by_country']))
    pipe.set(redis_key(election_key(ELECTION_DAY_BY_OFFICE_KEY, election)),
             json.dumps(data_out['by_office']))
    pipe.set(redis_key(election_key(ELECTION_DAY_OFFICES_TABLE_KEY, election)),
             json.dumps(offices_table))
    pipe.set(redis_key(election_key(ELECTION_DAY_POLLING_CENTERS_TABLE_KEY, election)),
             json.dumps(centers))
    for center in centers:
        pipe.set(redis_key(election_day_polling_center_table_key(
            election, center['polling_center_code'])),
            json.dumps(center))
    pipe.set(redis_key(election_key(ELECTION_DAY_METADATA_KEY, election)),
             json.dumps(metadata, cls=DateTimeEncoder))
    pipe.execute()
示例#3
0
    def test_distribution_by_election(self):
        tz = timezone(settings.TIME_ZONE)
        elections = Election.objects.all()

        # for each election, grab the report and initialize a counter for CenterOpen objects
        for election in elections:
            election.report = retrieve_report(election_key(ELECTION_DAY_REPORT_KEY, election))
            election.center_opens_found = 0

        # for each CenterOpen in the db, bump the counter for the particular election
        for center_open in CenterOpen.objects.all():
            open_date_str = astz(center_open.creation_date, tz).strftime('%Y-%m-%d')
            for election in elections:
                if open_date_str in election.report['dates']:
                    election.center_opens_found += 1

        for election in elections:
            center_opens_expected = 0
            for d in election.report['dates']:
                for office_data in election.report['by_office'].values():
                    if d in office_data and 'opened' in office_data[d]:
                        center_opens_expected += office_data[d]['opened']
            self.assertEqual(center_opens_expected, election.center_opens_found)
示例#4
0
    def setUp(self):
        self.staff_user = UserFactory()
        self.staff_user.is_staff = True
        self.staff_user.save()
        assert self.client.login(username=self.staff_user.username, password=DEFAULT_USER_PASSWORD)
        self.reporting_user = test_reports.TEST_USERNAME
        self.reporting_password = test_reports.TEST_PASSWORD
        REPORT_USER_DB[self.reporting_user] = self.reporting_password
        # Pick a start time that represents different days in Libya vs UTC
        tz = timezone(settings.TIME_ZONE)
        polling_start_time = astz(FUTURE_DAY.replace(hour=22), tz)
        polling_end_time = tz.normalize(polling_start_time + timedelta(hours=16))
        self.election = ElectionFactory(
            polling_start_time=polling_start_time,
            polling_end_time=polling_end_time,
        )
        self.election_day_dt = self.election.polling_start_time
        # Create "decoy" election just to confirm that it doesn't break reports.
        decoy_start_time = tz.normalize(polling_start_time - timedelta(days=10))
        decoy_end_time = tz.normalize(decoy_start_time + timedelta(hours=16))
        ElectionFactory(
            polling_start_time=decoy_start_time,
            polling_end_time=decoy_end_time,
        )
        self.all_centers = []
        self.rc_1 = RegistrationCenterFactory()
        self.all_centers.append(self.rc_1)
        self.rc_2 = RegistrationCenterFactory()
        self.all_centers.append(self.rc_2)
        self.rc_3 = RegistrationCenterFactory()
        self.all_centers.append(self.rc_3)
        self.rc_4 = RegistrationCenterFactory()
        self.all_centers.append(self.rc_4)
        self.copy_of_rc_1 = RegistrationCenterFactory(copy_of=self.rc_1, office=self.rc_1.office)
        self.all_centers.append(self.copy_of_rc_1)
        # rc_5 is inactive for this election
        self.rc_5 = RegistrationCenterFactory(office=self.rc_1.office)
        self.all_centers.append(self.rc_5)
        inactive_on_election = CenterClosedForElection(
            registration_center=self.rc_5, election=self.election
        )
        inactive_on_election.full_clean()
        inactive_on_election.save()
        self.all_office_ids = [center.office_id for center in self.all_centers]
        self.carrier_1 = BackendFactory()
        self.citizen_1 = CitizenFactory()

        # Create registrations on the 4 days leading up to election day
        # Put the registrations at different hours of the day to stress TZ handling.
        self.registration_dates = []
        self.registration_date_strs = []
        hour_of_day = 0
        for delta_days in range(10, 4, -1):
            assert hour_of_day < 24
            reg_date = astz(self.election_day_dt - timedelta(days=delta_days), tz)\
                .replace(hour=hour_of_day)
            hour_of_day += 4
            self.registration_dates.append(reg_date)
            self.registration_date_strs.append(reg_date.strftime('%Y-%m-%d'))
        self.yesterday_date, _ = calc_yesterday(self.registration_date_strs)
        self.yesterday_date_dm = self.yesterday_date.strftime('%d/%m')
        # yesterday_date is a date; get a datetime form
        self.yesterday_date_dt = tz.localize(datetime(self.yesterday_date.year,
                                                      self.yesterday_date.month,
                                                      self.yesterday_date.day,
                                                      0, 0, 0))
        self.staff_phone_number = STAFF_PHONE_NUMBER_PATTERN % 12345
示例#5
0
    def setUp(self):
        self.staff_user = UserFactory()
        self.staff_user.is_staff = True
        self.staff_user.save()
        assert self.client.login(username=self.staff_user.username,
                                 password=DEFAULT_USER_PASSWORD)
        self.reporting_user = test_reports.TEST_USERNAME
        self.reporting_password = test_reports.TEST_PASSWORD
        REPORT_USER_DB[self.reporting_user] = self.reporting_password
        # Pick a start time that represents different days in Libya vs UTC
        tz = timezone(settings.TIME_ZONE)
        polling_start_time = astz(FUTURE_DAY.replace(hour=22), tz)
        polling_end_time = tz.normalize(polling_start_time +
                                        timedelta(hours=16))
        self.election = ElectionFactory(
            polling_start_time=polling_start_time,
            polling_end_time=polling_end_time,
        )
        self.election_day_dt = self.election.polling_start_time
        # Create "decoy" election just to confirm that it doesn't break reports.
        decoy_start_time = tz.normalize(polling_start_time -
                                        timedelta(days=10))
        decoy_end_time = tz.normalize(decoy_start_time + timedelta(hours=16))
        ElectionFactory(
            polling_start_time=decoy_start_time,
            polling_end_time=decoy_end_time,
        )
        self.all_centers = []
        self.rc_1 = RegistrationCenterFactory()
        self.all_centers.append(self.rc_1)
        self.rc_2 = RegistrationCenterFactory()
        self.all_centers.append(self.rc_2)
        self.rc_3 = RegistrationCenterFactory()
        self.all_centers.append(self.rc_3)
        self.rc_4 = RegistrationCenterFactory()
        self.all_centers.append(self.rc_4)
        self.copy_of_rc_1 = RegistrationCenterFactory(copy_of=self.rc_1,
                                                      office=self.rc_1.office)
        self.all_centers.append(self.copy_of_rc_1)
        # rc_5 is inactive for this election
        self.rc_5 = RegistrationCenterFactory(office=self.rc_1.office)
        self.all_centers.append(self.rc_5)
        inactive_on_election = CenterClosedForElection(
            registration_center=self.rc_5, election=self.election)
        inactive_on_election.full_clean()
        inactive_on_election.save()
        self.all_office_ids = [center.office_id for center in self.all_centers]
        self.carrier_1 = BackendFactory()
        self.citizen_1 = CitizenFactory()

        # Create registrations on the 4 days leading up to election day
        # Put the registrations at different hours of the day to stress TZ handling.
        self.registration_dates = []
        self.registration_date_strs = []
        hour_of_day = 0
        for delta_days in range(10, 4, -1):
            assert hour_of_day < 24
            reg_date = astz(self.election_day_dt - timedelta(days=delta_days), tz)\
                .replace(hour=hour_of_day)
            hour_of_day += 4
            self.registration_dates.append(reg_date)
            self.registration_date_strs.append(reg_date.strftime('%Y-%m-%d'))
        self.yesterday_date, _ = calc_yesterday(self.registration_date_strs)
        self.yesterday_date_dm = self.yesterday_date.strftime('%d/%m')
        # yesterday_date is a date; get a datetime form
        self.yesterday_date_dt = tz.localize(
            datetime(self.yesterday_date.year, self.yesterday_date.month,
                     self.yesterday_date.day, 0, 0, 0))
        self.staff_phone_number = STAFF_PHONE_NUMBER_PATTERN % 12345