You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
36 lines
1.3 KiB
36 lines
1.3 KiB
from rest_framework.mixins import CreateModelMixin
|
|
from rest_framework.permissions import IsAuthenticated
|
|
from rest_framework.generics import GenericAPIView
|
|
from rest_framework.response import Response
|
|
from rest_framework import status
|
|
|
|
from apps.account.models import LocationHistory
|
|
from apps.account.serializers import LocationHistorySerializer
|
|
|
|
|
|
class LocationHistoryView(GenericAPIView, CreateModelMixin):
|
|
permission_classes = [IsAuthenticated]
|
|
serializer_class = LocationHistorySerializer
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
ip = self.get_client_ip()
|
|
data = request.data.copy()
|
|
data['ip'] = ip
|
|
serializer = self.get_serializer(data=data)
|
|
|
|
if serializer.is_valid():
|
|
serializer.save(user=request.user)
|
|
return Response(serializer.data, status=status.HTTP_201_CREATED)
|
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
def get_queryset(self):
|
|
return LocationHistory.objects.filter(user=self.request.user)
|
|
|
|
def get_client_ip(self):
|
|
# Retrieve the client's IP address from the request headers
|
|
x_forwarded_for = self.request.META.get('HTTP_X_FORWARDED_FOR')
|
|
if x_forwarded_for:
|
|
ip = x_forwarded_for.split(',')[0]
|
|
else:
|
|
ip = self.request.META.get('REMOTE_ADDR')
|
|
return ip
|