Pylint fixes
This commit is contained in:
@@ -1,7 +1,8 @@
|
||||
from django.contrib import admin
|
||||
|
||||
from .models import *
|
||||
|
||||
# Register your models here.
|
||||
|
||||
admin.site.register(FaceDetection)
|
||||
admin.site.register(EmployeeFaceDetection)
|
||||
admin.site.register(EmployeeFaceDetection)
|
||||
|
||||
@@ -2,14 +2,15 @@ from django.apps import AppConfig
|
||||
|
||||
|
||||
class FacedetectionConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'facedetection'
|
||||
default_auto_field = "django.db.models.BigAutoField"
|
||||
name = "facedetection"
|
||||
|
||||
def ready(self):
|
||||
from django.urls import include, path
|
||||
|
||||
from horilla.urls import urlpatterns
|
||||
|
||||
urlpatterns.append(
|
||||
path("api/facedetection/", include("facedetection.urls")),
|
||||
)
|
||||
super().ready()
|
||||
super().ready()
|
||||
|
||||
@@ -2,19 +2,16 @@ from django.db import models
|
||||
|
||||
# Create your models here.
|
||||
|
||||
|
||||
class FaceDetection(models.Model):
|
||||
company_id = models.OneToOneField("base.Company", related_name="face_detection", on_delete=models.CASCADE)
|
||||
company_id = models.OneToOneField(
|
||||
"base.Company", related_name="face_detection", on_delete=models.CASCADE
|
||||
)
|
||||
start = models.BooleanField(default=False)
|
||||
|
||||
|
||||
class EmployeeFaceDetection(models.Model):
|
||||
employee_id = models.OneToOneField("employee.Employee", related_name="face_detection", on_delete=models.CASCADE)
|
||||
employee_id = models.OneToOneField(
|
||||
"employee.Employee", related_name="face_detection", on_delete=models.CASCADE
|
||||
)
|
||||
image = models.ImageField()
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
from rest_framework import serializers
|
||||
|
||||
from .models import *
|
||||
|
||||
|
||||
class EmployeeFaceDetectionSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = EmployeeFaceDetection
|
||||
fields = '__all__'
|
||||
fields = "__all__"
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
from django.urls import path
|
||||
|
||||
from .views import *
|
||||
|
||||
|
||||
urlpatterns = [
|
||||
path("setup/", FaceDetectionGetPostAPIView.as_view())
|
||||
]
|
||||
urlpatterns = [path("setup/", FaceDetectionGetPostAPIView.as_view())]
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
from django.http import QueryDict
|
||||
from django.shortcuts import render
|
||||
from rest_framework.views import APIView
|
||||
from .serializers import *
|
||||
from rest_framework.response import Response
|
||||
from django.utils.decorators import method_decorator
|
||||
from django.views.decorators.csrf import csrf_exempt
|
||||
from rest_framework import status
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from django.views.decorators.csrf import csrf_exempt
|
||||
from django.utils.decorators import method_decorator
|
||||
from django.http import QueryDict
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.views import APIView
|
||||
|
||||
from .serializers import *
|
||||
|
||||
|
||||
class FaceDetectionGetPostAPIView(APIView):
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
from django.contrib import admin
|
||||
|
||||
from .models import GeoFencing
|
||||
|
||||
# Register your models here.
|
||||
|
||||
admin.site.register(GeoFencing)
|
||||
|
||||
@@ -2,11 +2,12 @@ from django.apps import AppConfig
|
||||
|
||||
|
||||
class GeofencingConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'geofencing'
|
||||
default_auto_field = "django.db.models.BigAutoField"
|
||||
name = "geofencing"
|
||||
|
||||
def ready(self):
|
||||
from django.urls import include, path
|
||||
|
||||
from horilla.urls import urlpatterns
|
||||
|
||||
urlpatterns.append(
|
||||
|
||||
@@ -1,31 +1,39 @@
|
||||
from django.db import models
|
||||
from base.models import Company
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.db import models
|
||||
from geopy.geocoders import Nominatim
|
||||
|
||||
from base.models import Company
|
||||
|
||||
|
||||
class GeoFencing(models.Model):
|
||||
latitude = models.FloatField(max_length=100)
|
||||
latitude = models.FloatField(max_length=100)
|
||||
longitude = models.FloatField(max_length=100)
|
||||
radius_in_meters = models.IntegerField()
|
||||
company_id = models.OneToOneField("base.Company", related_name="geo_fencing", on_delete=models.CASCADE, blank=True, null=True)
|
||||
company_id = models.OneToOneField(
|
||||
"base.Company",
|
||||
related_name="geo_fencing",
|
||||
on_delete=models.CASCADE,
|
||||
blank=True,
|
||||
null=True,
|
||||
)
|
||||
start = models.BooleanField(default=False)
|
||||
|
||||
|
||||
def clean(self):
|
||||
geolocator = Nominatim(user_agent="geo_checker") # Use a unique user-agent
|
||||
try:
|
||||
location = geolocator.reverse((self.latitude, self.longitude), exactly_one=True)
|
||||
location = geolocator.reverse(
|
||||
(self.latitude, self.longitude), exactly_one=True
|
||||
)
|
||||
if location:
|
||||
pass
|
||||
else:
|
||||
raise ValidationError("Invalid Location")
|
||||
except Exception as e:
|
||||
raise ValidationError(e)
|
||||
return super().clean()
|
||||
|
||||
raise ValidationError(e)
|
||||
return super().clean()
|
||||
|
||||
def save_base(self, raw = ..., force_insert = ..., force_update = ..., using = ..., update_fields = ...):
|
||||
def save_base(
|
||||
self, raw=..., force_insert=..., force_update=..., using=..., update_fields=...
|
||||
):
|
||||
self.clean()
|
||||
return super().save_base(raw, force_insert, force_update, using, update_fields)
|
||||
return super().save_base(raw, force_insert, force_update, using, update_fields)
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
from rest_framework import serializers
|
||||
from .models import GeoFencing
|
||||
from geopy.geocoders import Nominatim
|
||||
from rest_framework import serializers
|
||||
|
||||
from .models import GeoFencing
|
||||
|
||||
|
||||
class GeoFencingSetupSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = GeoFencing
|
||||
fields = '__all__'
|
||||
fields = "__all__"
|
||||
|
||||
def validate(self, data):
|
||||
geolocator = Nominatim(user_agent="geo_checker") # Use a unique user-agent
|
||||
@@ -23,5 +25,4 @@ class GeoFencingSetupSerializer(serializers.ModelSerializer):
|
||||
class EmployeeLocationSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = GeoFencing
|
||||
fields = ['latitude', 'longitude']
|
||||
|
||||
fields = ["latitude", "longitude"]
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
from django.urls import path
|
||||
from .views import *
|
||||
|
||||
from .views import *
|
||||
|
||||
urlpatterns = [
|
||||
path("setup/", GeoFencingSetupGetPostAPIView.as_view()),
|
||||
|
||||
@@ -1,22 +1,23 @@
|
||||
from django.contrib.auth.decorators import permission_required
|
||||
from django.http import QueryDict
|
||||
from django.utils.decorators import method_decorator
|
||||
from geopy.distance import geodesic
|
||||
from rest_framework.views import APIView
|
||||
from rest_framework.response import Response
|
||||
from rest_framework import status
|
||||
from rest_framework.pagination import PageNumberPagination
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.views import APIView
|
||||
|
||||
from .models import GeoFencing
|
||||
from .serializers import *
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from rest_framework.pagination import PageNumberPagination
|
||||
from django.contrib.auth.decorators import permission_required
|
||||
from django.utils.decorators import method_decorator
|
||||
from django.http import QueryDict
|
||||
|
||||
|
||||
|
||||
class GeoFencingSetupGetPostAPIView(APIView):
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
@method_decorator(
|
||||
permission_required("geofencing.view_geofencing", raise_exception=True), name="dispatch"
|
||||
permission_required("geofencing.view_geofencing", raise_exception=True),
|
||||
name="dispatch",
|
||||
)
|
||||
def get(self, request):
|
||||
if request.user.is_superuser:
|
||||
@@ -29,9 +30,9 @@ class GeoFencingSetupGetPostAPIView(APIView):
|
||||
serializer = GeoFencingSetupSerializer(page, many=True)
|
||||
return Response(serializer.data, status=status.HTTP_200_OK)
|
||||
|
||||
|
||||
@method_decorator(
|
||||
permission_required("geofencing.add_geofencing", raise_exception=True), name="dispatch"
|
||||
permission_required("geofencing.add_geofencing", raise_exception=True),
|
||||
name="dispatch",
|
||||
)
|
||||
def post(self, request):
|
||||
data = request.data
|
||||
@@ -40,53 +41,56 @@ class GeoFencingSetupGetPostAPIView(APIView):
|
||||
data = data.dict()
|
||||
company = request.user.employee_get.get_company()
|
||||
if company:
|
||||
data["company_id"] = company.id
|
||||
data["company_id"] = company.id
|
||||
serializer = GeoFencingSetupSerializer(data=request.data)
|
||||
if serializer.is_valid():
|
||||
serializer.save()
|
||||
return Response(serializer.data, status=status.HTTP_201_CREATED)
|
||||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
|
||||
|
||||
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"
|
||||
permission_required("geofencing.change_geofencing", raise_exception=True),
|
||||
name="dispatch",
|
||||
)
|
||||
def put(self, request, pk):
|
||||
location = self.get_location(pk)
|
||||
company = request.user.employee_get.get_company()
|
||||
if request.user.is_superuser or company == location.company_id:
|
||||
serializer = GeoFencingSetupSerializer(location, data=request.data, partial=True)
|
||||
serializer = GeoFencingSetupSerializer(
|
||||
location, data=request.data, partial=True
|
||||
)
|
||||
if serializer.is_valid():
|
||||
serializer.save()
|
||||
return Response(serializer.data, status=status.HTTP_200_OK)
|
||||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
||||
raise serializers.ValidationError("Access Denied..")
|
||||
|
||||
|
||||
@method_decorator(
|
||||
permission_required("geofencing.delete_geofencing", raise_exception=True), name="dispatch"
|
||||
permission_required("geofencing.delete_geofencing", raise_exception=True),
|
||||
name="dispatch",
|
||||
)
|
||||
def delete(self, request, pk):
|
||||
location = self.get_location(pk)
|
||||
company = request.user.employee_get.get_company()
|
||||
if request.user.is_superuser or company == location.company_id:
|
||||
location.delete()
|
||||
return Response({"message": "GeoFencing location deleted successfully"}, status=status.HTTP_204_NO_CONTENT)
|
||||
return Response(
|
||||
{"message": "GeoFencing location deleted successfully"},
|
||||
status=status.HTTP_204_NO_CONTENT,
|
||||
)
|
||||
raise serializers.ValidationError("Access Denied..")
|
||||
|
||||
|
||||
|
||||
class GeoFencingEmployeeLocationCheckAPIView(APIView):
|
||||
permission_classes = [IsAuthenticated]
|
||||
|
||||
@@ -100,21 +104,28 @@ class GeoFencingEmployeeLocationCheckAPIView(APIView):
|
||||
def get_company_location(self, request):
|
||||
company = self.get_company(request)
|
||||
try:
|
||||
location = GeoFencing.objects.get(company_id=company)
|
||||
location = GeoFencing.objects.get(company_id=company)
|
||||
return location
|
||||
except Exception as e:
|
||||
raise serializers.ValidationError(e)
|
||||
raise serializers.ValidationError(e)
|
||||
|
||||
def post(self, request):
|
||||
serializer = EmployeeLocationSerializer(data=request.data)
|
||||
if serializer.is_valid():
|
||||
company_location = self.get_company_location(request)
|
||||
geofence_center = (company_location.latitude, company_location.longitude)
|
||||
employee_location = (request.data.get("latitude"), request.data.get("longitude"))
|
||||
employee_location = (
|
||||
request.data.get("latitude"),
|
||||
request.data.get("longitude"),
|
||||
)
|
||||
distance = geodesic(geofence_center, employee_location).meters
|
||||
if distance <= company_location.radius_in_meters:
|
||||
return Response({"message": "Inside the geofence"}, status=status.HTTP_200_OK)
|
||||
return Response({"message": "Outside the geofence"}, status=status.HTTP_400_BAD_REQUEST)
|
||||
return Response(
|
||||
{"message": "Inside the geofence"}, status=status.HTTP_200_OK
|
||||
)
|
||||
return Response(
|
||||
{"message": "Outside the geofence"}, status=status.HTTP_400_BAD_REQUEST
|
||||
)
|
||||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
|
||||
@@ -125,4 +136,4 @@ class GeoFencingSetUpPermissionCheck(APIView):
|
||||
geo_fencing = GeoFencingSetupGetPostAPIView()
|
||||
if geo_fencing.get(request).status_code == 200:
|
||||
return Response(status=200)
|
||||
return Response(status=400)
|
||||
return Response(status=400)
|
||||
|
||||
Reference in New Issue
Block a user