diff --git a/products/views.py b/products/views.py index 6aab657..9d24ba2 100644 --- a/products/views.py +++ b/products/views.py @@ -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() diff --git a/templates/purchase_contact_confirmation.html b/templates/purchase_contact_confirmation.html new file mode 100644 index 0000000..eef225c --- /dev/null +++ b/templates/purchase_contact_confirmation.html @@ -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 diff --git a/templates/purchase_contact_verification.html b/templates/purchase_contact_verification.html deleted file mode 100644 index e69de29..0000000 diff --git a/templates/purchase_notification.html b/templates/purchase_notification.html index e69de29..8b485c1 100644 --- a/templates/purchase_notification.html +++ b/templates/purchase_notification.html @@ -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