def requestImaging(request, machine_request_id, auto_approve=False): """ Processes image request, sends an email to the user and a sperate email to [email protected] Returns a response. """ # TODO: This could also be: # machine_request.instance.created_by.username # And we could add another field 'new_image_owner'.. machine_request = MachineRequest.objects.get(id=machine_request_id) user = machine_request.new_machine_owner subject = 'Atmosphere Imaging Request - %s' % user.username context = { "user": user, "approved": auto_approve, "request": machine_request } body = render_to_string("core/email/imaging_request.html", context=Context(context)) # Send staff url if not approved if not auto_approve: namespace = "api:public_apis:cloud-admin-imaging-request-detail" base_url = reverse(namespace, args=(machine_request_id,)) context["view"] = base_url context["approve"] = "%s/approve" % base_url context["deny"] = "%s/deny" % base_url staff_body = render_to_string("core/email/imaging_request_staff.html", context=Context(context)) email_success = email_admin(request, subject, staff_body, cc_user=False) return email_from_admin(user.username, subject, body)
def requestImaging(request, machine_request_id, auto_approve=False): """ Processes image request, sends an email to the user and a sperate email to [email protected] Returns a response. """ # TODO: This could also be: # machine_request.instance.created_by.username # And we could add another field 'new_image_owner'.. machine_request = MachineRequest.objects.get(id=machine_request_id) user = machine_request.new_machine_owner subject = 'Atmosphere Imaging Request - %s' % user.username context = { "user": user, "approved": auto_approve, "request": machine_request } body = render_to_string("core/email/imaging_request.html", context=Context(context)) # Send staff url if not approved if not auto_approve: namespace = "api:v1:cloud-admin-imaging-request-detail" base_url = reverse(namespace, args=(machine_request_id,)) context["view"] = base_url context["approve"] = "%s/approve" % base_url context["deny"] = "%s/deny" % base_url staff_body = render_to_string("core/email/imaging_request_staff.html", context=Context(context)) email_success = email_admin(request, subject, staff_body, cc_user=False) return email_from_admin(user.username, subject, body)
def test_email_from_admin(self): """ Assert that emails from admin to user correctly set from, to, and cc fields """ UserFactory( username="******", first_name="First", last_name="Last", email="*****@*****.**" ) with mock.patch('core.tasks.EmailMessage') as MockMessage, \ override_settings(ATMO_DAEMON=('AtmoAdmin', '*****@*****.**')): email_from_admin("user0", 'Subject', 'Body') kwargs = MockMessage.call_args[1] MockMessage.assert_called_with( to=['First Last <*****@*****.**>'], from_email='AtmoAdmin <*****@*****.**>', cc=['AtmoAdmin <*****@*****.**>'], subject=kwargs['subject'], body=kwargs['body'], )
def requestImaging(request, machine_request_id, auto_approve=False): """ Processes image request, sends an email to the user and a sperate email to [email protected] Returns a response. """ #TODO: This could also be: #machine_request.instance.created_by.username #And we could add another field 'new_image_owner'.. machine_request = MachineRequest.objects.get(id=machine_request_id) user = machine_request.new_machine_owner username = user.username full_name = user.get_full_name() if auto_approve: message_header = "Your image request has been approved!"\ " The imaging process will begin shortly.\n" else: view_link = '%s/api/v1/request_image/%s' \ % (settings.SERVER_URL, machine_request_id) approve_link = '%s/api/v1/request_image/%s/approve' \ % (settings.SERVER_URL, machine_request_id) deny_link = '%s/api/v1/request_image/%s/deny' \ % (settings.SERVER_URL, machine_request_id) staff_header = """ ATTN Staff Users: Authenticate to atmosphere, then select any of these URLs to access the approriate action. Are you unable to click on these links? Let one of the Admins know and we will make sure your account is marked as 'staff'. View Request: %s Auto-Approve Request: %s Auto-Deny Request: %s --- """ % (view_link, approve_link, deny_link) message_header = "Your Image Request has been received."\ " Upon staff approval, we will send you an e-mail to let you"\ " know that the the imaging process will begin shortly.\n" #Add appropriate header.. message = message_header + """ When the imaging process has completed, you will receive an email with details about the new image. Your Image Request: Username : %s Full Name: %s Instance ID:%s --- Installed software:%s System Files changed:%s Image visiblity:%s Shared with (Private only):%s --- New Image name:%s New Image description:%s New Image tags:%s """ % (username, full_name, machine_request.instance.provider_alias, machine_request.installed_software, machine_request.iplant_sys_files, machine_request.new_machine_visibility, machine_request.access_list, machine_request.new_machine_name, machine_request.new_machine_description, machine_request.new_machine_tags) subject = 'Atmosphere Imaging Request - %s' % username #First e-mail is 'clean' email_success = email_from_admin(username, subject, message) #Second e-mail contains API urls if not auto_approve: message = "%s\n%s" % (staff_header, message) email_success = email_admin(request, subject, message, cc_user=False) return email_success
def requestImaging(request, machine_request_id, auto_approve=False): """ Processes image request, sends an email to the user and a sperate email to [email protected] Returns a response. """ #TODO: This could also be: #machine_request.instance.created_by.username #And we could add another field 'new_image_owner'.. machine_request = MachineRequest.objects.get(id=machine_request_id) username = machine_request.new_machine_owner.username if auto_approve: message_header = "Your image request has been approved!"\ " The imaging process will begin shortly.\n" else: view_link = '%s/api/v1/request_image/%s' \ % (settings.SERVER_URL, machine_request_id) approve_link = '%s/api/v1/request_image/%s/approve' \ % (settings.SERVER_URL, machine_request_id) deny_link = '%s/api/v1/request_image/%s/deny' \ % (settings.SERVER_URL, machine_request_id) staff_header = """ ATTN Staff Users: Authenticate to atmosphere, then select any of these URLs to access the approriate action. Are you unable to click on these links? Let one of the Admins know and we will make sure your account is marked as 'staff'. View Request: %s Auto-Approve Request: %s Auto-Deny Request: %s --- """ % (view_link, approve_link, deny_link) message_header = "Your Image Request has been received."\ " Upon staff approval, we will send you an e-mail to let you"\ " know that the the imaging process will begin shortly.\n" #Add appropriate header.. message = message_header + """ When the imaging process has completed, you will receive an email with details about the new image. Your Image Request: Username : %s Instance ID:%s --- Installed software:%s System Files changed:%s Image visiblity:%s Shared with (Private only):%s --- New Image name:%s New Image description:%s New Image tags:%s """ % (username, machine_request.instance.provider_alias, machine_request. installed_software, machine_request.iplant_sys_files, machine_request.new_machine_visibility, machine_request.access_list, machine_request.new_machine_name, machine_request. new_machine_description, machine_request.new_machine_tags) subject = 'Atmosphere Imaging Request - %s' % username #First e-mail is 'clean' email_success = email_from_admin(username, subject, message) #Second e-mail contains API urls if not auto_approve: message = "%s\n%s" % (staff_header, message) email_success = email_admin(request, subject, message, cc_user=False) return email_success