def user_status_email_reminder(sender, instance, created, raw, **kwargs): """Send email notifications when a user submits an unavailability notice. """ rep_profile = instance.user.userprofile # Make sure that the user has a mentor mentor_profile = None if instance.user.userprofile.mentor: mentor_profile = instance.user.userprofile.mentor.userprofile if created: subject_rep = 'Confirm if you are available for Reps activities' rep_template = 'emails/rep_availability_reminder.jinja' notification_datetime = datetime.datetime.combine( instance.expected_date - datetime.timedelta(days=1), datetime.datetime.min.time()) data = {'user_status': instance} time_diff = (timezone.make_aware(notification_datetime, pytz.UTC) - timezone.now()) # If the notification date is in the past, then send the notification # x/2 hours from now, where x is the diff between the return date and # now if time_diff.days < 0: hours_added = (time_diff.seconds / 3600) / 2 notification_datetime = (timezone.now() + datetime.timedelta(hours=hours_added)) rep_reminder = send_remo_mail.apply_async( eta=notification_datetime, kwargs={'recipients_list': [rep_profile.user.id], 'email_template': rep_template, 'subject': subject_rep, 'data': data}) if mentor_profile: mentor_template = 'emails/mentor_availability_reminder.jinja' subject_mentor = ('Reach out to {0} - ' 'expected to be available again' .format(instance.user.get_full_name())) mentor_reminder = send_remo_mail.apply_async( eta=notification_datetime, kwargs={'recipients_list': [mentor_profile.user.id], 'email_template': mentor_template, 'subject': subject_mentor, 'data': data}) (UserProfile.objects.filter(pk=mentor_profile.id) .update(unavailability_task_id=mentor_reminder.task_id)) # Update user profiles with the task IDs (UserProfile.objects.filter(pk=rep_profile.id) .update(unavailability_task_id=rep_reminder.task_id)) elif not settings.CELERY_ALWAYS_EAGER: # revoke the tasks in case a user returns sooner if rep_profile.unavailability_task_id: celery_app.control.revoke(rep_profile.unavailability_task_id) if mentor_profile.unavailability_task_id: celery_app.control.revoke(mentor_profile.unavailability_task_id)
def user_status_email_reminder(sender, instance, created, raw, **kwargs): rep_profile = instance.user.userprofile mentor_profile = instance.user.userprofile.mentor.userprofile if created: subject_rep = 'Confirm if you are available for Reps activities' subject_mentor = ( 'Reach out to {0} - expected to be available again'.format( instance.user.get_full_name())) rep_template = 'emails/rep_availability_reminder.txt' mentor_template = 'emails/mentor_availability_reminder.txt' notification_datetime = datetime.datetime.combine( instance.expected_date - datetime.timedelta(days=1), datetime.datetime.min.time()) data = {'user_status': instance} rep_reminder = send_remo_mail.apply_async(eta=notification_datetime, kwargs={ 'recipients_list': [rep_profile.user.id], 'email_template': rep_template, 'subject': subject_rep, 'data': data }) mentor_reminder = send_remo_mail.apply_async( eta=notification_datetime, kwargs={ 'recipients_list': [mentor_profile.user.id], 'email_template': mentor_template, 'subject': subject_mentor, 'data': data }) # Update user profiles with the task IDs (UserProfile.objects.filter(pk=rep_profile.id).update( unavailability_task_id=rep_reminder.task_id)) (UserProfile.objects.filter(pk=mentor_profile.id).update( unavailability_task_id=mentor_reminder.task_id)) elif not settings.CELERY_ALWAYS_EAGER: # revoke the tasks in case a user returns sooner if rep_profile.unavailability_task_id: celery_control.revoke(rep_profile.unavailability_task_id) if mentor_profile.unavailability_task_id: celery_control.revoke(mentor_profile.unavailability_task_id)
def user_status_email_reminder(sender, instance, created, raw, **kwargs): rep_profile = instance.user.userprofile mentor_profile = instance.user.userprofile.mentor.userprofile if created: subject_rep = 'Confirm if you are available for Reps activities' subject_mentor = ('Reach out to {0} - expected to be available again' .format(instance.user.get_full_name())) rep_template = 'emails/rep_availability_reminder.txt' mentor_template = 'emails/mentor_availability_reminder.txt' notification_datetime = datetime.datetime.combine( instance.expected_date - datetime.timedelta(days=1), datetime.datetime.min.time()) data = {'user_status': instance} rep_reminder = send_remo_mail.apply_async( eta=notification_datetime, kwargs={'recipients_list': [rep_profile.user.id], 'email_template': rep_template, 'subject': subject_rep, 'data': data}) mentor_reminder = send_remo_mail.apply_async( eta=notification_datetime, kwargs={'recipients_list': [mentor_profile.user.id], 'email_template': mentor_template, 'subject': subject_mentor, 'data': data}) # Update user profiles with the task IDs (UserProfile.objects.filter(pk=rep_profile.id) .update(unavailability_task_id=rep_reminder.task_id)) (UserProfile.objects.filter(pk=mentor_profile.id) .update(unavailability_task_id=mentor_reminder.task_id)) elif not settings.CELERY_ALWAYS_EAGER: # revoke the tasks in case a user returns sooner if rep_profile.unavailability_task_id: celery_control.revoke(rep_profile.unavailability_task_id) if mentor_profile.unavailability_task_id: celery_control.revoke(mentor_profile.unavailability_task_id)