Ejemplo n.º 1
0
def calc_yesterday(dates, dates_d=None):
    """ Given a list of date strings from a report, find "yesterday"
    and return it in datetime and string form.  Dates are always in order
    on input.  Return "yesterday" in both datetime.date and %Y-%m-%d format.

    "yesterday" is the next to last date with registrations if still in
    a registration period, or the last date with registrations otherwise.

    If the dates are already available as date objects, pass that in as dates_d.

    If there haven't been any registrations, dates will be empty; return
    None, None.
    """
    if not dates:
        return None, None

    if dates_d is None:
        dates_d = parse_dates(dates)

    today = now().date()

    if len(dates) > 1 and registration_in_progress(as_of=today):
        # registration ongoing, use day before most recent
        yesterday_date_str = dates[-2]  # not true if still in signup
        yesterday_date = dates_d[-2]
    else:
        # registration finished, final day of data
        yesterday_date_str = dates[-1]
        yesterday_date = dates_d[-1]

    return yesterday_date, yesterday_date_str
Ejemplo n.º 2
0
    def test_yesterday_general(self):
        today = now()
        first_dt = today - datetime.timedelta(7)
        input_strings = [(first_dt + datetime.timedelta(delta_days)).strftime('%Y-%m-%d')
                         for delta_days in range(7)]

        # the behavior depends on the current datetime relative to registration end,
        # so try it with registration end before and after the current time
        reg_period = RegistrationPeriodFactory(start_time=today - datetime.timedelta(days=5),
                                               end_time=today)
        for fake_registration_end in [today - datetime.timedelta(days=1),
                                      today + datetime.timedelta(days=1)]:
            reg_period.end_time = fake_registration_end
            reg_period.save()
            if registration_in_progress(as_of=today.date()):
                expected_str = input_strings[-2]
            else:
                expected_str = input_strings[-1]
            expected_date = datetime.datetime.strptime(expected_str, '%Y-%m-%d').date()
            date_and_string = calc_yesterday(input_strings)
            self.assertEqual(date_and_string, (expected_date, expected_str))
            # try again, providing the date objects
            input_dates = [datetime.datetime.strptime(s, '%Y-%m-%d').date()
                           for s in input_strings]
            date_and_string = calc_yesterday(input_strings, input_dates)
            self.assertEqual(date_and_string, (expected_date, expected_str))