improvement to email_manager action as per documentation
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
import logging
|
import logging
|
||||||
|
|
||||||
from django.shortcuts import render
|
from django.shortcuts import render, get_object_or_404
|
||||||
from django.core.mail import EmailMessage
|
from django.core.mail import EmailMessage
|
||||||
from django.template.loader import render_to_string
|
from django.template.loader import render_to_string
|
||||||
|
|
||||||
@@ -29,37 +29,67 @@ class CompanyViewSet(viewsets.ModelViewSet):
|
|||||||
"""
|
"""
|
||||||
Send email to company.creator
|
Send email to company.creator
|
||||||
"""
|
"""
|
||||||
# TODO: check access for anonymous users, and customize response
|
try:
|
||||||
|
|
||||||
queryset = self.get_custom_queryset(request)
|
queryset = self.get_custom_queryset(request)
|
||||||
instance = queryset.filter(pk=kwargs['pk']).first()
|
instance = queryset.filter(pk=kwargs['pk']).first()
|
||||||
if instance:
|
if instance:
|
||||||
data = json.loads(request.body)
|
data = json.loads(request.body)
|
||||||
|
if request.user.is_authenticated:
|
||||||
# send email to manager
|
# send email to manager
|
||||||
message = render_to_string('company_contact.html', {
|
company_message = render_to_string('company_contact.html', {
|
||||||
'company': instance,
|
'company': instance,
|
||||||
'user': request.user,
|
'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,
|
'data': data,
|
||||||
})
|
})
|
||||||
email = EmailMessage(subject, message, to=[instance.creator.email])
|
# send email to company
|
||||||
|
subject = "Contacto de usuario"
|
||||||
|
email = EmailMessage(subject, company_message, to=[instance.creator.email])
|
||||||
email.send()
|
email.send()
|
||||||
logging.info(f"Email sent to {instance.creator.email} as manager of {instance.name}")
|
logging.info(f"Email sent to {instance.creator.email} as manager of {instance.name}")
|
||||||
|
|
||||||
# send confirmation email to user
|
# send confirmation email to user
|
||||||
message = render_to_string('confirm_company_contact.html', {
|
subject = 'Confirmación de contacto'
|
||||||
'company': instance,
|
|
||||||
'user': request.user,
|
|
||||||
'data': data,
|
|
||||||
})
|
|
||||||
email = EmailMessage(subject, message, to=[request.user.email])
|
email = EmailMessage(subject, message, to=[request.user.email])
|
||||||
email.send()
|
email.send()
|
||||||
logging.info(f"contact Confirmation email sent to {request.user.email}")
|
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
|
# TODO: create statslog instance to rgister interaction
|
||||||
|
|
||||||
return Response(data=data)
|
return Response(data=data)
|
||||||
else:
|
else:
|
||||||
return Response({"errors":{"details": f"No instance of company with id {kwargs['pk']}",}})
|
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',])
|
@api_view(['GET',])
|
||||||
|
|||||||
@@ -1,5 +1,2 @@
|
|||||||
Hola {{user.full_name}}.
|
Hola {{user.full_name}}.
|
||||||
Hemos enviado un email a {{company.company_name}} sobre tu petición.
|
Hemos enviado un email a {{company.company_name}} sobre tu petición.
|
||||||
|
|
||||||
Información envidada:
|
|
||||||
{{data}}
|
|
||||||
|
|||||||
Reference in New Issue
Block a user