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.db.models import Q
|
||||||
from django.core import serializers
|
from django.core import serializers
|
||||||
|
from django.contrib.auth import get_user_model
|
||||||
|
|
||||||
# Create your views here.
|
# Create your views here.
|
||||||
from rest_framework import status
|
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_serializers import TaggitSerializer
|
||||||
from utils.tag_filters import ProductTagFilter, ProductOrderFilter
|
from utils.tag_filters import ProductTagFilter, ProductOrderFilter
|
||||||
|
|
||||||
|
User = get_user_model()
|
||||||
|
|
||||||
logging.basicConfig(
|
logging.basicConfig(
|
||||||
filename='logs/product-load.log',
|
filename='logs/product-load.log',
|
||||||
@@ -264,31 +266,41 @@ def purchase_email(request):
|
|||||||
email = request.user.email
|
email = request.user.email
|
||||||
telephone = data.get('telephone')
|
telephone = data.get('telephone')
|
||||||
|
|
||||||
# prepare email messages
|
# get company
|
||||||
company_message = render_to_string('purchase_notification.html', {
|
company = Company.objects.filter(id=data['company']).first()
|
||||||
'company': instance,
|
if not company:
|
||||||
'email': request.user.email,
|
return Response({"error": "Invalid value for company"}, status=status.HTTP_406_NOT_ACCEPTABLE)
|
||||||
'full_name': request.user.full_name,
|
# get company manager
|
||||||
'quantity': data['quantity'],
|
manager = User.objects.filter(company=company).first()
|
||||||
'phone_number': data.get('phone_number'),
|
if not manager and manager.role != 'COOP_MANAGER':
|
||||||
'comments': data['comments'],
|
return Response({"error": "Company has no managing user"}, status=status.HTTP_406_NOT_ACCEPTABLE)
|
||||||
'product_info': data['product_info'],
|
# 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', {
|
subject = "Contacto de usuario sobre venta"
|
||||||
'company': instance,
|
email = EmailMessage(subject, manager_message, to=[manager.email])
|
||||||
'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()
|
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
|
# 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 = 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"Purchase Contact confirmation email sent to {request.user.email}")
|
||||||
|
|
||||||
|
# create statslog instance to register interaction
|
||||||
stats_data = {
|
stats_data = {
|
||||||
'action_object': instance,
|
'action_object': instance,
|
||||||
'user': None,
|
'user': None,
|
||||||
@@ -298,8 +310,7 @@ def purchase_email(request):
|
|||||||
'contact': True,
|
'contact': True,
|
||||||
'shop': instance.shop,
|
'shop': instance.shop,
|
||||||
}
|
}
|
||||||
|
StatsLog.objects.create(**stats_data)
|
||||||
# email user
|
|
||||||
|
|
||||||
# response
|
# response
|
||||||
return Response()
|
return Response()
|
||||||
|
|||||||
6
templates/purchase_contact_confirmation.html
Normal file
6
templates/purchase_contact_confirmation.html
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
Hola usuario.
|
||||||
|
Hemos envíado correctamente el email al usuario que gestiona {{company}} sobre el producto {{product}}.
|
||||||
|
|
||||||
|
Deberías revibir una respuesta directa en los próximos días.
|
||||||
|
|
||||||
|
LaTiendaCOOP
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
Hola usuario.
|
||||||
|
Te contactamos por tu puesto como gestor de {{company}}.
|
||||||
|
|
||||||
|
El usuario {{user.email}} ha mostrado interés en la compra de {{product}}.
|
||||||
|
|
||||||
|
Ponte en contacto con el usuario tan pronto como te sea posible, y finalizar la venta.
|
||||||
|
|
||||||
|
Teléfono de contacto: {{telephone}}
|
||||||
|
|
||||||
|
LaTiendaCOOP
|
||||||
|
|||||||
Reference in New Issue
Block a user