Merge branch 'development' of https://bitbucket.org/enreda/back-latienda into diego

This commit is contained in:
Diego Calvo
2021-02-11 11:53:55 +01:00
19 changed files with 11085 additions and 38 deletions

View File

@@ -26,7 +26,7 @@ class Company(models.Model):
platform = models.CharField('Plataforma de tienda online', choices=PLATFORMS, max_length=25, null=True, blank=True)
email = models.EmailField('Email', null=True, blank=True)
logo = models.ImageField('Logo', upload_to='logos/', null=True, blank=True)
city = models.ForeignKey('geo.City', null=True, blank=True, on_delete=models.DO_NOTHING)
city = models.CharField('Municipio', max_length=1000, null=True, blank=True)
address = models.CharField('Dirección', max_length=1000, null=True, blank=True)
geo = models.PointField('Coordenadas', null=True, blank=True )
phone = models.CharField('Teléfono', max_length=25, null=True, blank=True)

View File

@@ -1,8 +1,9 @@
import logging
from django.shortcuts import render
from django.shortcuts import render, get_object_or_404
from django.core.mail import EmailMessage
from django.template.loader import render_to_string
from django.contrib.gis.geoip2 import GeoIP2
# Create your views here.
from rest_framework import viewsets
@@ -10,6 +11,9 @@ from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticatedOrReadOnly, IsAuthenticated
from rest_framework.decorators import api_view, permission_classes, action
from ipware import get_client_ip
from stats.models import StatsLog
from companies.models import Company
from companies.serializers import CompanySerializer
@@ -29,31 +33,92 @@ class CompanyViewSet(viewsets.ModelViewSet):
"""
Send email to company.creator
"""
try:
queryset = self.get_custom_queryset(request)
instance = queryset.filter(pk=kwargs['pk']).first()
if instance:
# IP stuff
client_ip, is_routable = get_client_ip(request)
g = GeoIP2()
queryset = self.get_custom_queryset(request)
instance = queryset.filter(pk=kwargs['pk']).first()
if instance:
data = json.loads(request.body)
# send email to manager
message = render_to_string('company_contact.html', {
'user': request.user,
'data': data,
})
email = EmailMessage(subject, message, to=[instance.creator.email])
email.send()
logging.info(f"Email sent to {instance.creator.email} as manager of {instance.name}")
# deserialize payload
data = json.loads(request.body)
if request.user.is_authenticated:
# send email to manager
company_message = render_to_string('company_contact.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,
}
else:
# for unauthenticated users
company_message = render_to_string('company_contact.html', {
'company': instance,
'email': data['email'],
'full_name': data['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': data['full_name'],
})
# send email to company
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
email = EmailMessage(subject, user_message, to=[data['email']])
email.send()
logging.info(f"Contact confirmation email sent to anonymous user {data['email']}")
# statslog data to register interaction
stats_data = {
'action_object': instance,
'user': request.user,
'anonymous': False,
'ip_address': client_ip,
'geo': g.geos(client_ip),
'contact': True,
'shop': instance.shop,
}
# create statslog instance to register interaction
StatsLog.objects.create(**stats_data)
# send confirmation email to user
message = render_to_string('confirm_company_contact.html', {
'user': request.user,
})
email = EmailMessage(subject, message, to=[request.user.email])
email.send()
logging.info(f"Confirmation email sent to {request.user.email}")
return Response(data=data)
else:
return Response({"errors":{"details": f"No instance of company with id {kwargs['pk']}",}})
return Response(data=data)
else:
return Response({"errors":{"details": f"No instance of company with id {kwargs['pk']}",}})
except Exception as e:
return Response({"errors":{"details": str(e),}}, status=500)
@api_view(['GET',])