more work on purchase_email endpoint and basic templates
This commit is contained in:
@@ -5,6 +5,7 @@ import json
|
||||
|
||||
from django.db.models import Q
|
||||
from django.core import serializers
|
||||
from django.contrib.auth import get_user_model
|
||||
|
||||
# Create your views here.
|
||||
from rest_framework import status
|
||||
@@ -28,6 +29,7 @@ from .utils import extract_search_filters, find_related_products_v6, product_loa
|
||||
from utils.tag_serializers import TaggitSerializer
|
||||
from utils.tag_filters import ProductTagFilter, ProductOrderFilter
|
||||
|
||||
User = get_user_model()
|
||||
|
||||
logging.basicConfig(
|
||||
filename='logs/product-load.log',
|
||||
@@ -264,31 +266,41 @@ def purchase_email(request):
|
||||
email = request.user.email
|
||||
telephone = data.get('telephone')
|
||||
|
||||
# prepare email messages
|
||||
company_message = render_to_string('purchase_notification.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'],
|
||||
# get company
|
||||
company = Company.objects.filter(id=data['company']).first()
|
||||
if not company:
|
||||
return Response({"error": "Invalid value for company"}, status=status.HTTP_406_NOT_ACCEPTABLE)
|
||||
# get company manager
|
||||
manager = User.objects.filter(company=company).first()
|
||||
if not manager and manager.role != 'COOP_MANAGER':
|
||||
return Response({"error": "Company has no managing user"}, status=status.HTTP_406_NOT_ACCEPTABLE)
|
||||
# get product
|
||||
product = Product.objects.filter(id=data['product']).first()
|
||||
if not product:
|
||||
return Response({"error": "Invalid value for product"}, status=status.HTTP_406_NOT_ACCEPTABLE)
|
||||
|
||||
# send email to company manager
|
||||
manager_message = render_to_string('purchase_notification.html', {
|
||||
'company': company,
|
||||
'user': request.user,
|
||||
'product': product,
|
||||
'telephone': data['telephone'],
|
||||
})
|
||||
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])
|
||||
subject = "Contacto de usuario sobre venta"
|
||||
email = EmailMessage(subject, manager_message, to=[manager.email])
|
||||
email.send()
|
||||
logging.info(f"Email sent to {instance.creator.email} as manager of {instance.name}")
|
||||
logging.info(f"Email sent to {manager.email} as manager of {company}")
|
||||
# send confirmation email to user
|
||||
subject = 'Confirmación de contacto'
|
||||
user_message = render_to_string('purchase_contact_confirmation.html', {
|
||||
'company': company,
|
||||
'product': product,
|
||||
})
|
||||
subject = 'Confirmación de contacto con vendedor'
|
||||
email = EmailMessage(subject, message, to=[request.user.email])
|
||||
email.send()
|
||||
logging.info(f"Contact confirmation email sent to {request.user.email}")
|
||||
logging.info(f"Purchase Contact confirmation email sent to {request.user.email}")
|
||||
|
||||
# create statslog instance to register interaction
|
||||
stats_data = {
|
||||
'action_object': instance,
|
||||
'user': None,
|
||||
@@ -298,8 +310,7 @@ def purchase_email(request):
|
||||
'contact': True,
|
||||
'shop': instance.shop,
|
||||
}
|
||||
|
||||
# email user
|
||||
StatsLog.objects.create(**stats_data)
|
||||
|
||||
# response
|
||||
return Response()
|
||||
|
||||
Reference in New Issue
Block a user