Skip to content

Instantly share code, notes, and snippets.

@dirkcuys
Created May 27, 2019 11:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dirkcuys/3d24a3426abc3a7fa4a8a00868f9bdfc to your computer and use it in GitHub Desktop.
Save dirkcuys/3d24a3426abc3a7fa4a8a00868f9bdfc to your computer and use it in GitHub Desktop.
Sending logic
# ...
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
# ...
@dirkcuys
Copy link
Author

This will send an ~hour later than intended. The time_to_send will be in the period before the period we're currently in.

  • time_to_send - this is when we want to send
  • now.replace(minute=0, second=0, microsecond=0) is the start of the period we are currently in

The reasonable condition to satisfy is

start_of_window < now <= end_of_window
start_of_window < time_to_send <= end_of_window

Iow, trigger the event in the same period than the intended time to trigger the event.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment