se agrega pagination, ordering
parent
de4e1c88e6
commit
afcd290000
|
@ -10,11 +10,19 @@ class ApiMiddleware:
|
||||||
self.get_response = get_response
|
self.get_response = get_response
|
||||||
|
|
||||||
def __call__(self, request):
|
def __call__(self, request):
|
||||||
if not request.headers.get('Authorization') and request.path == '/api/auth/' and request.method == 'POST':
|
# se omite esta regla en documentacion
|
||||||
# cuando se quiere obtener el token, se omite esta regla
|
if not request.headers.get('Authorization') and request.path[0:6] == '/docs/':
|
||||||
response = self.get_response(request)
|
response = self.get_response(request)
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
# se omite esta regla en login
|
||||||
|
if request.path == '/api/auth/' and request.method == 'POST':
|
||||||
|
response = self.get_response(request)
|
||||||
|
return response
|
||||||
|
|
||||||
|
if not request.headers.get('Authorization'):
|
||||||
|
return HttpResponse('Debe indicar el token de autorización', status = 400)
|
||||||
|
|
||||||
authorization = request.headers.get('Authorization').split(' ')
|
authorization = request.headers.get('Authorization').split(' ')
|
||||||
token = authorization[1]
|
token = authorization[1]
|
||||||
|
|
||||||
|
|
|
@ -13,11 +13,10 @@ class PersonaSerializer(serializers.ModelSerializer):
|
||||||
fields = '__all__'
|
fields = '__all__'
|
||||||
|
|
||||||
class UsuarioSerializer(serializers.ModelSerializer):
|
class UsuarioSerializer(serializers.ModelSerializer):
|
||||||
# persona = serializers.PrimaryKeyRelatedField(queryset=Persona.objects.all(), source='rut')
|
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = Usuario
|
model = Usuario
|
||||||
fields = ('login','vigente','rut')
|
fields = ('login','vigente','rut')
|
||||||
|
|
||||||
# def get_persona(self, usuario):
|
class AuthSerializer(serializers.Serializer):
|
||||||
# return usuario.persona
|
username = serializers.CharField(required=True)
|
||||||
|
password = serializers.CharField(required=True, style={'input_type':'password'})
|
|
@ -1,15 +1,17 @@
|
||||||
from rest_framework import viewsets
|
from rest_framework import viewsets
|
||||||
from rest_framework.decorators import action
|
from rest_framework.decorators import action, api_view, schema
|
||||||
|
from rest_framework.schemas import AutoSchema
|
||||||
from django.views.decorators.csrf import csrf_exempt
|
from django.views.decorators.csrf import csrf_exempt
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse
|
||||||
from django.http import JsonResponse
|
from django.http import JsonResponse
|
||||||
from .models import Usuario, Persona
|
from .models import Usuario, Persona
|
||||||
from .models import Aplicacion
|
from .models import Aplicacion
|
||||||
from .serializers import UsuarioSerializer, PersonaSerializer
|
from .serializers import UsuarioSerializer, PersonaSerializer
|
||||||
from .serializers import AplicacionSerializer
|
from .serializers import AplicacionSerializer, AuthSerializer
|
||||||
import json
|
import json
|
||||||
import jwt
|
import jwt
|
||||||
import datetime
|
import datetime
|
||||||
|
import coreapi
|
||||||
from decouple import config
|
from decouple import config
|
||||||
|
|
||||||
private_key = config('SECRET_JWT')
|
private_key = config('SECRET_JWT')
|
||||||
|
@ -18,6 +20,7 @@ private_key = config('SECRET_JWT')
|
||||||
class AplicacionViewSet(viewsets.ModelViewSet):
|
class AplicacionViewSet(viewsets.ModelViewSet):
|
||||||
queryset = Aplicacion.objects.all()
|
queryset = Aplicacion.objects.all()
|
||||||
serializer_class = AplicacionSerializer
|
serializer_class = AplicacionSerializer
|
||||||
|
ordering_fields = '__all__'
|
||||||
|
|
||||||
class PersonaViewSet(viewsets.ModelViewSet):
|
class PersonaViewSet(viewsets.ModelViewSet):
|
||||||
queryset = Persona.objects.all()
|
queryset = Persona.objects.all()
|
||||||
|
@ -27,9 +30,24 @@ class UsuarioViewSet(viewsets.ModelViewSet):
|
||||||
queryset = Usuario.objects.all()
|
queryset = Usuario.objects.all()
|
||||||
serializer_class = UsuarioSerializer
|
serializer_class = UsuarioSerializer
|
||||||
|
|
||||||
|
|
||||||
|
class CustomAuthSchema(AutoSchema):
|
||||||
|
manual_fields = []
|
||||||
|
|
||||||
|
def get_manual_fields(self, path, method):
|
||||||
|
custom_fields = []
|
||||||
|
if method.lower() == 'post':
|
||||||
|
custom_fields = [
|
||||||
|
coreapi.Field('username', required=True, location='form', description='Usuario'),
|
||||||
|
coreapi.Field('password', required=True, location='form', description='Contraseña'),
|
||||||
|
]
|
||||||
|
return self._manual_fields + custom_fields
|
||||||
|
|
||||||
# Views jwt
|
# Views jwt
|
||||||
@csrf_exempt
|
@csrf_exempt
|
||||||
@action(detail=False, methods=['post','get'])
|
@action(detail=False, methods=['post','get'])
|
||||||
|
@api_view(['GET','POST'])
|
||||||
|
@schema(CustomAuthSchema())
|
||||||
def jwt_login(request):
|
def jwt_login(request):
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
# validar username y password
|
# validar username y password
|
||||||
|
|
|
@ -136,7 +136,12 @@ STATIC_URL = 'static/'
|
||||||
|
|
||||||
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
|
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
|
||||||
|
|
||||||
REST_FRAMEWORK = { 'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema' }
|
REST_FRAMEWORK = {
|
||||||
|
'DEFAULT_SCHEMA_CLASS': 'rest_framework.schemas.coreapi.AutoSchema',
|
||||||
|
'DEFAULT_PAGINATION_CLASS': 'rest_framework.pagination.LimitOffsetPagination',
|
||||||
|
'DEFAULT_FILTER_BACKENDS': ['rest_framework.filters.OrderingFilter'],
|
||||||
|
# 'PAGE_SIZE': 15,
|
||||||
|
}
|
||||||
CORS_ORIGIN_ALLOW_ALL = False
|
CORS_ORIGIN_ALLOW_ALL = False
|
||||||
CORS_ORIGIN_WHITELIST = [
|
CORS_ORIGIN_WHITELIST = [
|
||||||
"http://localhost:3000",
|
"http://localhost:3000",
|
||||||
|
|
|
@ -14,4 +14,8 @@ Content-Type: application/json
|
||||||
|
|
||||||
###
|
###
|
||||||
GET {{server}}/auth/
|
GET {{server}}/auth/
|
||||||
Authorization: Bearer {{token}}
|
Authorization: Bearer {{token}}
|
||||||
|
|
||||||
|
###
|
||||||
|
GET {{server}}/aplicaciones/?ordering=-id_aplicacion
|
||||||
|
Authorization: Bearer {{token}}
|
||||||
|
|
Loading…
Reference in New Issue