From d65f9d2388cdff2e8cf0ca37b089a9af846306c2 Mon Sep 17 00:00:00 2001 From: Sam Date: Tue, 9 Mar 2021 12:27:15 +0000 Subject: [PATCH] added purchase_email endpoint to products views --- products/views.py | 61 ++++++++++++++++++++ templates/purchase_contact_verification.html | 0 templates/purchase_notification.html | 0 3 files changed, 61 insertions(+) create mode 100644 templates/purchase_contact_verification.html create mode 100644 templates/purchase_notification.html diff --git a/products/views.py b/products/views.py index 8fc511d..6aab657 100644 --- a/products/views.py +++ b/products/views.py @@ -1,6 +1,7 @@ import logging import csv import datetime +import json from django.db.models import Q from django.core import serializers @@ -242,3 +243,63 @@ class CategoryTagAutocomplete(autocomplete.Select2QuerySetView): qs = qs.filter(name__icontains=self.q) return qs # [x.label for x in qs] + + +@api_view(['POST']) +def purchase_email(request): + """Notify coop manager and user about item purchase + """ + + data = json.loads(request.body) + # check data + try: + for param in ('email', 'telephone', 'company', 'product', 'comment'): + assert(param in data.keys()) + except: + return Response({"error": "Required parameters for anonymous user: email, telephone"}, status=status.HTTP_406_NOT_ACCEPTABLE) + + if request.user.is_anonymous: + email = data.get('email') + else: + 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'], + }) + 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}") + stats_data = { + 'action_object': instance, + 'user': None, + 'anonymous': True, + 'ip_address': client_ip, + 'geo': g.geos(client_ip), + 'contact': True, + 'shop': instance.shop, + } + + # email user + + # response + return Response() diff --git a/templates/purchase_contact_verification.html b/templates/purchase_contact_verification.html new file mode 100644 index 0000000..e69de29 diff --git a/templates/purchase_notification.html b/templates/purchase_notification.html new file mode 100644 index 0000000..e69de29