# ... def send_facilitator_survey(study_group): """ send survey to all facilitators two days before their second to last meeting """ now = timezone.now() end_of_window = now.replace(minute=0, second=0, microsecond=0) start_of_window = end_of_window - datetime.timedelta(hours=1) last_two_meetings = study_group.meeting_set.active().order_by('-meeting_date', '-meeting_time')[:2] time_to_send = None if last_two_meetings.count() == 2: penultimate_meeting = last_two_meetings[1] time_to_send = penultimate_meeting.meeting_datetime() - datetime.timedelta(days=2, hours=1) if time_to_send and time_to_send > start_of_window and time_to_send <= end_of_window: facilitator_name = study_group.facilitator.first_name path = reverse('studygroups_facilitator_survey', kwargs={'study_group_id': study_group.id}) base_url = f'{settings.PROTOCOL}://{settings.DOMAIN}' survey_url = base_url + path # ...