[FIX] GEOFENCING: Geofence setup create update issue

This commit is contained in:
Horilla
2025-08-20 16:53:35 +05:30
parent abd7db227e
commit 45ebaae727
3 changed files with 22 additions and 25 deletions

View File

@@ -28,14 +28,15 @@ class GeoFencing(models.Model):
geolocator = Nominatim(
user_agent="geo_checker_unique"
) # Unique user-agent is important
try:
location = geolocator.reverse(
(self.latitude, self.longitude), exactly_one=True
)
if not location:
raise ValidationError("Invalid location coordinates.")
except Exception as e:
raise ValidationError(f"Geolocation error: {e}")
if self.start:
try:
location = geolocator.reverse(
(self.latitude, self.longitude), exactly_one=True
)
if not location:
raise ValidationError("Invalid location coordinates.")
except Exception as e:
raise ValidationError(f"Geolocation error: {e}")
return super().clean()

View File

@@ -11,14 +11,16 @@ class GeoFencingSetupSerializer(serializers.ModelSerializer):
def validate(self, data):
geolocator = Nominatim(user_agent="geo_checker") # Use a unique user-agent
try:
latitude = data.get("latitude")
longitude = data.get("longitude")
location = geolocator.reverse((latitude, longitude), exactly_one=True)
if not location:
raise serializers.ValidationError("Invalid Location")
except Exception as e:
raise serializers.ValidationError(e)
start = data.get("start")
if start:
try:
latitude = data.get("latitude")
longitude = data.get("longitude")
location = geolocator.reverse((latitude, longitude), exactly_one=True)
if not location:
raise serializers.ValidationError("Invalid Location")
except Exception as e:
raise serializers.ValidationError(e)
return data

View File

@@ -27,7 +27,7 @@ class GeoFencingSetupGetPostAPIView(APIView):
)
def get(self, request):
company = request.user.employee_get.get_company()
location = get_object_or_404(GeoFencing, pk=company.id)
location = get_object_or_404(GeoFencing, company_id=company.id)
serializer = GeoFencingSetupSerializer(location)
return Response(serializer.data, status=status.HTTP_200_OK)
@@ -53,18 +53,12 @@ class GeoFencingSetupGetPostAPIView(APIView):
class GeoFencingSetupPutDeleteAPIView(APIView):
permission_classes = [IsAuthenticated]
def get_location(self, pk):
try:
return GeoFencing.objects.get(pk=pk)
except Exception as e:
raise serializers.ValidationError(e)
@method_decorator(
permission_required("geofencing.change_geofencing", raise_exception=True),
name="dispatch",
)
def put(self, request, pk):
location = self.get_location(pk)
location = get_object_or_404(GeoFencing, pk=pk)
company = request.user.employee_get.get_company()
if request.user.is_superuser or company == location.company_id:
serializer = GeoFencingSetupSerializer(
@@ -81,7 +75,7 @@ class GeoFencingSetupPutDeleteAPIView(APIView):
name="dispatch",
)
def delete(self, request, pk):
location = self.get_location(pk)
location = get_object_or_404(GeoFencing, pk=pk)
company = request.user.employee_get.get_company()
if request.user.is_superuser or company == location.company_id:
location.delete()