added functionality to Serializer validate method
This commit is contained in:
@@ -1,14 +1,18 @@
|
||||
from django.shortcuts import render
|
||||
from django.contrib.auth import logout
|
||||
from django.views.generic.list import ListView
|
||||
from django.views.generic.detail import DetailView
|
||||
|
||||
from rest_framework.authentication import TokenAuthentication
|
||||
from rest_framework.views import APIView
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from rest_framework import status, permissions
|
||||
|
||||
from .models import sistemas_por_cliente
|
||||
|
||||
from django.contrib.auth.models import User
|
||||
from .models import sistemas_por_cliente, DeviceHistory,Device
|
||||
from .serializers import DeviceSerializer
|
||||
from .permissions import HasAuthorizationHeader
|
||||
from django.utils import timezone
|
||||
import re
|
||||
|
||||
class SistemasXCliente_ListView(ListView):
|
||||
model = sistemas_por_cliente
|
||||
@@ -19,13 +23,76 @@ class SistemasXCliente_DetailView(DetailView):
|
||||
model = sistemas_por_cliente
|
||||
template_name= 'Sistemas/Xclientes/detail.html'
|
||||
|
||||
#registrar Equipo
|
||||
class Registrar_PC(APIView):
|
||||
from .verificacion_licencias import NumLicencias
|
||||
class RegisterDeviceView(APIView):
|
||||
permissions_classes = (permissions.AllowAny,)
|
||||
|
||||
def post(self,request, format=None):
|
||||
data = request.data
|
||||
return Response(data)
|
||||
def post(self,request):
|
||||
try:
|
||||
# client = request.data.get('client')
|
||||
# device_name = request.data.get('device_name')
|
||||
# ip_address = request.data.get('ip_address')
|
||||
# print('-----Not Token, so its register User ',request.data.get('token'))
|
||||
# if self.Check_unique_username(client,device_name,ip_address):
|
||||
# #or Device.objects.filter(username=username_check).exists():
|
||||
# return Response({'Error':'El Dispositivo ya se encuentra registrado','isError':True}, status=status.HTTP_200_OK)
|
||||
|
||||
def get(self,request):
|
||||
pass
|
||||
serializer = DeviceSerializer(data=request.data,context={'request':request})
|
||||
if serializer.is_valid():
|
||||
serializer.save()
|
||||
return Response(serializer.data, status=status.HTTP_200_OK)
|
||||
else:
|
||||
print('serializer.errors',serializer.errors)
|
||||
return Response({'Error':f'{serializer.errors}','isError':True}, status=status.HTTP_200_OK)
|
||||
except Exception as e:
|
||||
return Response(
|
||||
{'1)Error':f'{e}','isError':True}
|
||||
, status=status.HTTP_200_OK
|
||||
)
|
||||
|
||||
def Check_unique_username(self,client, device_name, ip_address):
|
||||
username_ = f"Device_{client}_{device_name}_{ip_address}"
|
||||
username_ = re.sub(r'\W+', '', username_)
|
||||
if User.objects.filter(username=username_).exists():
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
|
||||
class AuthenticateDeviceView(APIView):
|
||||
authentication_classes= [TokenAuthentication]
|
||||
permissions_classes=[IsAuthenticated]
|
||||
|
||||
def get(self, request):
|
||||
try:
|
||||
obj, created = DeviceHistory.objects.get_or_create(
|
||||
device=request.user.device,
|
||||
ip_address=request.META.get('REMOTE_ADDR'),
|
||||
)
|
||||
obj.last_authentication=timezone.now()
|
||||
obj.save()
|
||||
|
||||
device_data=DeviceSerializer(request.user.device).data
|
||||
|
||||
if device_data.serializer.is_valid:
|
||||
print('ACCEPT',device_data.serializer.data)
|
||||
return Response(device_data.serializer.data, status=status.HTTP_200_OK)
|
||||
else:
|
||||
print('ERROR',device_data.serializer.errors)
|
||||
return Response(
|
||||
{'Error':f'{device_data.serializer.errors}','isError':True}
|
||||
, status=status.HTTP_200_OK
|
||||
)
|
||||
except Exception as e:
|
||||
return Response(
|
||||
{'Error':f'{e}','isError':True}
|
||||
, status=status.HTTP_200_OK
|
||||
)
|
||||
|
||||
class LogoutView(APIView):
|
||||
authentication_classes = (TokenAuthentication,)
|
||||
permission_classes = (IsAuthenticated,HasAuthorizationHeader,)
|
||||
|
||||
def post(self, request):
|
||||
logout(request)
|
||||
return Response({'OK':'Dispositivo desautenticado'},status=200)
|
||||
Reference in New Issue
Block a user