added ip and geoip functionality to email_manager action
This commit is contained in:
@@ -3,6 +3,7 @@ import logging
|
||||
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
|
||||
|
||||
@@ -33,6 +37,11 @@ class CompanyViewSet(viewsets.ModelViewSet):
|
||||
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()
|
||||
|
||||
# deserialize payload
|
||||
data = json.loads(request.body)
|
||||
if request.user.is_authenticated:
|
||||
# send email to manager
|
||||
@@ -60,6 +69,15 @@ class CompanyViewSet(viewsets.ModelViewSet):
|
||||
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', {
|
||||
@@ -83,7 +101,18 @@ class CompanyViewSet(viewsets.ModelViewSet):
|
||||
email = EmailMessage(subject, user_message, to=[data['email']])
|
||||
email.send()
|
||||
logging.info(f"Contact confirmation email sent to anonymous user {data['email']}")
|
||||
# TODO: create statslog instance to rgister interaction
|
||||
# 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)
|
||||
|
||||
return Response(data=data)
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user