added purchase_email endpoint to products views
This commit is contained in:
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user