From bfe40e4cb658252b51fbcc713d7e1afa77a7030b Mon Sep 17 00:00:00 2001 From: Amit Upreti Date: Fri, 10 Mar 2023 14:31:55 -0500 Subject: [PATCH] send email to host,cohost Whenever a host adds or removes a cohost to the event, email notification are sent to a) host and b) the added/removed cohost --- physionet-django/events/views.py | 16 +++++++--- physionet-django/notification/utility.py | 39 ++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/physionet-django/events/views.py b/physionet-django/events/views.py index 4d40d49ce8..75a89c2d5c 100644 --- a/physionet-django/events/views.py +++ b/physionet-django/events/views.py @@ -262,8 +262,12 @@ def manage_co_hosts(request): return JsonResponse({'error': 'User is not a cohost of this event'}, status=403) participant.is_cohost = False participant.save() - - # placeholder for notification to cohost that they have been removed and to host that they removed a cohost + # notify the cohost that their cohost permission has been removed + notification.notify_event_cohost_cohost_status_change(request=request, cohost=participant.user, event=event, + status='Remove cohost') + # notify the host that they have removed a cohost + notification.notify_event_host_cohost_status_change(request=request, cohost=participant.user, event=event, + status='Remove cohost') return JsonResponse({'success': 'Cohost removed successfully'}) elif 'Make cohost' in request.POST.get('submit'): @@ -271,8 +275,12 @@ def manage_co_hosts(request): return JsonResponse({'error': 'User is already a cohost of this event'}, status=403) participant.is_cohost = True participant.save() - - # placeholder for notification to cohost that they have been added and to host that they added a cohost + # notify the cohost that they have been added as a cohost for the event + notification.notify_event_cohost_cohost_status_change(request=request, cohost=participant.user, event=event, + status='Make cohost') + # notify the host that they have added a cohost for the event + notification.notify_event_host_cohost_status_change(request=request, cohost=participant.user, event=event, + status='Make cohost') return JsonResponse({'success': 'Cohost added successfully'}) diff --git a/physionet-django/notification/utility.py b/physionet-django/notification/utility.py index a2b2ceee22..efe6cebfb7 100644 --- a/physionet-django/notification/utility.py +++ b/physionet-django/notification/utility.py @@ -985,6 +985,45 @@ def notify_participant_event_decision(request, user, event, decision, comment_to send_mail(subject, body, settings.DEFAULT_FROM_EMAIL, [user.email], fail_silently=False) +def notify_event_cohost_cohost_status_change(request, cohost, event, status='Make cohost'): + """ + Send email to co-host about their cohost status change for the event. + """ + subject = f"{settings.SITE_NAME} Event Cohost Status Change" + context = { + 'name': cohost.get_full_name(), + 'domain': get_current_site(request), + 'url_prefix': get_url_prefix(request), + 'event_title': event.title, + 'event_url': reverse('event_detail', args=[event.slug]), + 'status': status, + 'SITE_NAME': settings.SITE_NAME, + } + body = loader.render_to_string('events/email/event_cohost_cohost_status_change.html', context) + # Not resend the email if there was an integrity error + send_mail(subject, body, settings.DEFAULT_FROM_EMAIL, [cohost.email], fail_silently=False) + + +def notify_event_host_cohost_status_change(request, cohost, event, status='Make cohost'): + """ + Send email to host about cohost status change for the event. + """ + subject = f"{settings.SITE_NAME} Event Cohost Status Change" + context = { + 'host_name': event.host.get_full_name(), + 'cohost_name': cohost.get_full_name(), + 'domain': get_current_site(request), + 'url_prefix': get_url_prefix(request), + 'event_title': event.title, + 'event_url': reverse('event_detail', args=[event.slug]), + 'status': status, + 'SITE_NAME': settings.SITE_NAME, + } + body = loader.render_to_string('events/email/event_host_cohost_status_change.html', context) + # Not resend the email if there was an integrity error + send_mail(subject, body, settings.DEFAULT_FROM_EMAIL, [cohost.email], fail_silently=False) + + def notify_event_participant_application(request, user, registered_user, event): """ Send email to host and co-host about new registration request on event.