From dad8c00b842ad7ad66cf79c92d5e070b3aec2c3b Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 10 Feb 2021 11:01:33 +0000 Subject: [PATCH] improvement to email_manager action as per documentation --- companies/views.py | 92 +++++++++++++++++--------- templates/confirm_contact_company.html | 3 - 2 files changed, 61 insertions(+), 34 deletions(-) diff --git a/companies/views.py b/companies/views.py index 17575ff..63f2895 100644 --- a/companies/views.py +++ b/companies/views.py @@ -1,6 +1,6 @@ import logging -from django.shortcuts import render +from django.shortcuts import render, get_object_or_404 from django.core.mail import EmailMessage from django.template.loader import render_to_string @@ -29,37 +29,67 @@ class CompanyViewSet(viewsets.ModelViewSet): """ Send email to company.creator """ - # TODO: check access for anonymous users, and customize response + try: + queryset = self.get_custom_queryset(request) + instance = queryset.filter(pk=kwargs['pk']).first() + if instance: + data = json.loads(request.body) + if request.user.is_authenticated: + # send email to manager + company_message = render_to_string('company_contact.html', { + 'company': instance, + 'email': request.user.email, + 'full_name': request.user.full_name, + 'quantity': data['quantity'], + 'phone_number': data.get('phone_number'), + 'comments': data['comments'], + 'product_info': data['product_info'], + }) + user_message = render_to_string('confirm_company_contact.html', { + 'company': instance, + 'username': request.user.full_name, + 'data': data, + }) + # send email to company + subject = "Contacto de usuario" + email = EmailMessage(subject, company_message, to=[instance.creator.email]) + email.send() + logging.info(f"Email sent to {instance.creator.email} as manager of {instance.name}") + # send confirmation email to user + subject = 'Confirmación de contacto' + email = EmailMessage(subject, message, to=[request.user.email]) + email.send() + logging.info(f"Contact confirmation email sent to {request.user.email}") + else: + # for unauthenticated users + company_message = render_to_string('company_contact.html', { + 'company': instance, + 'email': data['email'], + 'full_name': data['full_name'], + 'quantity': data['quantity'], + 'phone_number': data.get('phone_number'), + 'comments': data['comments'], + 'product_info': data['product_info'], + }) + user_message = render_to_string('confirm_company_contact.html', { + 'company': instance, + 'username': data['full_name'], + }) + # send email to company + email = EmailMessage(subject, company_message, to=[instance.creator.email]) + email.send() + logging.info(f"Email sent to {instance.creator.email} as manager of {instance.name}") + # send confirmation email to user + email = EmailMessage(subject, user_message, to=[data['email']]) + email.send() + logging.info(f"Contact confirmation email sent to anonymous user {data['email']}") + # TODO: create statslog instance to rgister interaction - queryset = self.get_custom_queryset(request) - instance = queryset.filter(pk=kwargs['pk']).first() - if instance: - data = json.loads(request.body) - # send email to manager - message = render_to_string('company_contact.html', { - 'company': instance, - 'user': request.user, - 'data': data, - }) - email = EmailMessage(subject, message, to=[instance.creator.email]) - email.send() - logging.info(f"Email sent to {instance.creator.email} as manager of {instance.name}") - - # send confirmation email to user - message = render_to_string('confirm_company_contact.html', { - 'company': instance, - 'user': request.user, - 'data': data, - }) - email = EmailMessage(subject, message, to=[request.user.email]) - email.send() - logging.info(f"contact Confirmation email sent to {request.user.email}") - - # TODO: create statslog instance to rgister interaction - - return Response(data=data) - else: - return Response({"errors":{"details": f"No instance of company with id {kwargs['pk']}",}}) + return Response(data=data) + else: + return Response({"errors":{"details": f"No instance of company with id {kwargs['pk']}",}}) + except Exception as e: + return Response({"errors":{"details": str(e),}}, status=500) @api_view(['GET',]) diff --git a/templates/confirm_contact_company.html b/templates/confirm_contact_company.html index 8460795..70dcb0e 100644 --- a/templates/confirm_contact_company.html +++ b/templates/confirm_contact_company.html @@ -1,5 +1,2 @@ Hola {{user.full_name}}. Hemos enviado un email a {{company.company_name}} sobre tu petición. - -Información envidada: -{{data}}