def send_task_updated_email(task, url, task_images: List[TaskImages]): if not hasattr(settings, 'LAB_MANAGERS'): return attachments = None if task_images: attachments = [ create_email_attachment(task_image.image, task_image.image.name) for task_image in task_images ] task.refresh_from_db() if task.resolved: task_user = task.resolver task_status = 'resolved' else: task_user = task.last_updated_by task_status = 'updated' message = f""" A task for the {task.tool} was just modified by {task_user}. <br/><br/> The latest update is at the bottom of the description. The entirety of the task status follows: <br/><br/> Task problem description:<br/> {task.problem_description} <br/><br/> Task progress description:<br/> {task.progress_description} <br/><br/> Task resolution description:<br/> {task.resolution_description} <br/><br/> Visit {url} to view the tool control page for the task.<br/> """ send_mail(f'{task.tool} task {task_status}', message, settings.SERVER_EMAIL, settings.LAB_MANAGERS, attachments)
def send_new_task_emails(request, task: Task, task_images: List[TaskImages]): message = get_media_file_contents('new_task_email.html') attachments = None if task_images: attachments = [create_email_attachment(task_image.image, task_image.image.name) for task_image in task_images] if message: dictionary = { 'template_color': bootstrap_primary_color('danger') if task.force_shutdown else bootstrap_primary_color('warning'), 'user': request.user, 'task': task, 'tool': task.tool, 'tool_control_absolute_url': request.build_absolute_uri(task.tool.get_absolute_url()) } # Send an email to the appropriate staff that a new task has been created: subject = ('SAFETY HAZARD: ' if task.safety_hazard else '') + task.tool.name + (' shutdown' if task.force_shutdown else ' problem') message = Template(message).render(Context(dictionary)) managers = [] if hasattr(settings, 'LAB_MANAGERS'): managers = settings.LAB_MANAGERS recipients = tuple([r for r in [task.tool.primary_owner.email, *task.tool.backup_owners.all().values_list('email', flat=True), task.tool.notification_email_address, *managers] if r]) send_mail(subject, message, request.user.email, recipients, attachments) # Send an email to any user (excluding staff) with a future reservation on the tool: user_office_email = get_customization('user_office_email_address') message = get_media_file_contents('new_task_email.html') if user_office_email and message: upcoming_reservations = Reservation.objects.filter(start__gt=timezone.now(), cancelled=False, tool=task.tool, user__is_staff=False) for reservation in upcoming_reservations: if not task.tool.operational: subject = reservation.tool.name + " reservation problem" rendered_message = Template(message).render(Context({'reservation': reservation, 'template_color': bootstrap_primary_color('danger'), 'fatal_error': True})) else: subject = reservation.tool.name + " reservation warning" rendered_message = Template(message).render(Context({'reservation': reservation, 'template_color': bootstrap_primary_color('warning'), 'fatal_error': False})) reservation.user.email_user(subject, rendered_message, user_office_email)
def create_ics_for_reservation(reservation: Reservation, cancelled=False): method = 'METHOD:CANCEL\n' if cancelled else 'METHOD:PUBLISH\n' status = 'STATUS:CANCELLED\n' if cancelled else 'STATUS:CONFIRMED\n' uid = 'UID:' + str(reservation.id) + '\n' sequence = 'SEQUENCE:2\n' if cancelled else 'SEQUENCE:0\n' priority = 'PRIORITY:5\n' if cancelled else 'PRIORITY:0\n' now = datetime.now().strftime('%Y%m%dT%H%M%S') start = reservation.start.astimezone( timezone.get_current_timezone()).strftime('%Y%m%dT%H%M%S') end = reservation.end.astimezone( timezone.get_current_timezone()).strftime('%Y%m%dT%H%M%S') lines = [ 'BEGIN:VCALENDAR\n', 'VERSION:2.0\n', method, 'BEGIN:VEVENT\n', uid, sequence, priority, f'DTSTAMP:{now}\n', f'DTSTART:{start}\n', f'DTEND:{end}\n', f'SUMMARY:[NEMO] {reservation.tool.name} Reservation\n', status, 'END:VEVENT\n', 'END:VCALENDAR\n' ] ics = io.StringIO('') ics.writelines(lines) ics.seek(0) filename = 'cancelled_nemo_reservation.ics' if cancelled else 'nemo_reservation.ics' return create_email_attachment(ics, filename)
def send_task_updated_email(task, url, task_images: List[TaskImages] = None): try: if not hasattr(settings, 'LAB_MANAGERS'): return attachments = None if task_images: attachments = [ create_email_attachment(task_image.image, task_image.image.name) for task_image in task_images ] task.refresh_from_db() if task.cancelled: task_user = task.resolver task_status = 'cancelled' elif task.resolved: task_user = task.resolver task_status = 'resolved' else: task_user = task.last_updated_by task_status = 'updated' message = f""" A task for the {task.tool} was just modified by {task_user}. <br/><br/> The latest update is at the bottom of the description. The entirety of the task status follows: <br/><br/> Task problem description:<br/> {linebreaksbr(task.problem_description)} <br/><br/> Task progress description:<br/> {linebreaksbr(task.progress_description)} <br/><br/> Task resolution description:<br/> {linebreaksbr(task.resolution_description)} <br/><br/> Visit {url} to view the tool control page for the task.<br/> """ send_mail(subject=f'{task.tool} task {task_status}', content=message, from_email=settings.SERVER_EMAIL, to=settings.LAB_MANAGERS, attachments=attachments, email_category=EmailCategory.TASKS) except Exception as error: site_title = get_customization('site_title') error_message = f"{site_title} was unable to send the task updated email. The error message that was received is: " + str( error) tasks_logger.exception(error_message)
def send_new_task_emails(request, task: Task, task_images: List[TaskImages]): message = get_media_file_contents('new_task_email.html') attachments = None if task_images: attachments = [ create_email_attachment(task_image.image, task_image.image.name) for task_image in task_images ] # Send an email to the appropriate staff that a new task has been created: if message: dictionary = { 'template_color': bootstrap_primary_color('danger') if task.force_shutdown else bootstrap_primary_color('warning'), 'user': request.user, 'task': task, 'tool': task.tool, 'tool_control_absolute_url': request.build_absolute_uri(task.tool.get_absolute_url()) } subject = ('SAFETY HAZARD: ' if task.safety_hazard else '') + task.tool.name + (' shutdown' if task.force_shutdown else ' problem') message = Template(message).render(Context(dictionary)) managers = [] if hasattr(settings, 'LAB_MANAGERS'): managers = settings.LAB_MANAGERS recipients = tuple([ r for r in [ task.tool.primary_owner.email, *task.tool.backup_owners.all().values_list('email', flat=True), task.tool.notification_email_address, *managers ] if r ]) send_mail(subject=subject, content=message, from_email=request.user.email, to=recipients, attachments=attachments, email_category=EmailCategory.TASKS) # Send an email to all users (excluding staff) qualified on the tool: user_office_email = get_customization('user_office_email_address') message = get_media_file_contents('new_task_email.html') if user_office_email and message: users = User.objects.filter(qualifications__id=task.tool.id, is_staff=False, is_active=True) dictionary = { 'template_color': bootstrap_primary_color('danger') if task.force_shutdown else bootstrap_primary_color('warning'), 'user': request.user, 'task': task, 'tool': task.tool, 'tool_control_absolute_url': request.build_absolute_uri(task.tool.get_absolute_url()), 'labmember': True } subject = task.tool.name + (' shutdown' if task.force_shutdown else ' problem') users = [x.email for x in users] rendered_message = Template(message).render(Context(dictionary)) #try: email = EmailMultiAlternatives(subject, from_email=user_office_email, bcc=set(users)) email.attach_alternative(rendered_message, 'text/html') email.send()