From 45ebaae72710b7d76f3a8cd67e3229796b866ad1 Mon Sep 17 00:00:00 2001 From: Horilla Date: Wed, 20 Aug 2025 16:53:35 +0530 Subject: [PATCH] [FIX] GEOFENCING: Geofence setup create update issue --- geofencing/models.py | 17 +++++++++-------- geofencing/serializers.py | 18 ++++++++++-------- geofencing/views.py | 12 +++--------- 3 files changed, 22 insertions(+), 25 deletions(-) diff --git a/geofencing/models.py b/geofencing/models.py index 6a12bd5a0..16ca0eede 100644 --- a/geofencing/models.py +++ b/geofencing/models.py @@ -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() diff --git a/geofencing/serializers.py b/geofencing/serializers.py index 2e2255788..0b3b1fc46 100644 --- a/geofencing/serializers.py +++ b/geofencing/serializers.py @@ -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 diff --git a/geofencing/views.py b/geofencing/views.py index 49dedde45..5a842b81a 100644 --- a/geofencing/views.py +++ b/geofencing/views.py @@ -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()