2023-12-05 10:34:47 -03:00
|
|
|
|
|
|
|
from django.db import transaction
|
2024-01-06 12:25:12 -03:00
|
|
|
from django.http import HttpResponse, JsonResponse
|
2024-01-27 00:26:58 -03:00
|
|
|
from django.contrib.auth.hashers import make_password
|
2023-12-05 10:34:47 -03:00
|
|
|
from rest_framework import viewsets
|
|
|
|
from rest_framework.response import Response
|
2024-01-06 12:25:12 -03:00
|
|
|
from rest_framework.decorators import action
|
2023-12-05 10:34:47 -03:00
|
|
|
|
2024-02-25 00:08:59 -03:00
|
|
|
from api import models, schemas, serializers
|
|
|
|
from logger.views import save_log_usuario
|
|
|
|
|
2023-12-05 10:34:47 -03:00
|
|
|
import json
|
|
|
|
import logging
|
|
|
|
|
|
|
|
class UsuarioViewSet(viewsets.ModelViewSet):
|
|
|
|
queryset = models.Usuario.objects.all()
|
|
|
|
serializer_class = serializers.UsuarioSerializer
|
|
|
|
schema = schemas.UsuarioSchema()
|
|
|
|
|
|
|
|
def retrieve(self, request, pk=None):
|
|
|
|
data = super().retrieve(request, pk)
|
|
|
|
return data
|
|
|
|
|
|
|
|
def create(self, request):
|
|
|
|
try:
|
2024-02-25 00:08:59 -03:00
|
|
|
login = self.request.jwt_info['login']
|
|
|
|
usuario_session = models.Usuario.objects.filter(login=login)
|
|
|
|
|
2023-12-05 10:34:47 -03:00
|
|
|
with transaction.atomic():
|
|
|
|
input = json.loads(request.body)
|
|
|
|
|
|
|
|
persona = models.Persona.objects.filter(rut = input['rut']).first()
|
|
|
|
id_tipo_tratamiento = input.get('id_tipo_tratamiento',None)
|
2024-02-25 00:08:59 -03:00
|
|
|
|
|
|
|
tipo_tratamiento = None
|
|
|
|
if id_tipo_tratamiento:
|
|
|
|
tipo_tratamiento = models.TipoTratamientoPersona.objects.filter(id_tipo_tratamiento = id_tipo_tratamiento).first()
|
2023-12-05 10:34:47 -03:00
|
|
|
|
|
|
|
if not persona:
|
|
|
|
persona = models.Persona(
|
|
|
|
rut = input['rut'],
|
|
|
|
dv = input['dv'],
|
|
|
|
nombres = input['nombres'],
|
|
|
|
apellido_a = input.get('apellido_a',None),
|
|
|
|
apellido_b = input.get('apellido_b',None),
|
|
|
|
email = input.get('email',None),
|
2024-02-25 00:08:59 -03:00
|
|
|
id_tipo_tratamiento = tipo_tratamiento
|
2023-12-05 10:34:47 -03:00
|
|
|
)
|
|
|
|
persona.save()
|
|
|
|
else:
|
|
|
|
persona.nombres = input['nombres']
|
|
|
|
persona.apellido_a = input.get('apellido_a',None)
|
|
|
|
persona.apellido_b = input.get('apellido_b',None)
|
|
|
|
persona.email = input.get('email',None)
|
2024-02-25 00:08:59 -03:00
|
|
|
persona.id_tipo_tratamiento = tipo_tratamiento
|
2023-12-05 10:34:47 -03:00
|
|
|
persona.save()
|
|
|
|
|
2024-01-12 20:11:07 -03:00
|
|
|
rol = models.Rol.objects.filter(id_rol=input.get('id_rol',None)).first()
|
|
|
|
if rol == None:
|
|
|
|
raise ValueError('Rol no encontrado')
|
|
|
|
|
2023-12-05 10:34:47 -03:00
|
|
|
usuario = models.Usuario(
|
|
|
|
rut = persona,
|
|
|
|
login = input['login'],
|
2024-01-12 20:11:07 -03:00
|
|
|
id_rol = rol,
|
2023-12-05 10:34:47 -03:00
|
|
|
vigente = input.get('vigente', False),
|
|
|
|
)
|
|
|
|
usuario.save()
|
|
|
|
|
2024-02-25 00:08:59 -03:00
|
|
|
save_log_usuario(accion_tabla='create', rut_tabla=persona.rut, rut_usuario_ejecutor=usuario_session.rut.rut)
|
|
|
|
|
2023-12-05 10:34:47 -03:00
|
|
|
if input['clave']:
|
|
|
|
logging.error('Modificar clave de usuario')
|
2024-01-27 00:26:58 -03:00
|
|
|
usuario.clave = make_password(input['clave'])
|
|
|
|
usuario.save()
|
2023-12-05 10:34:47 -03:00
|
|
|
|
|
|
|
return Response({
|
|
|
|
'rut': persona.rut,
|
|
|
|
'dv': persona.dv,
|
|
|
|
'nombres': persona.nombres,
|
|
|
|
'apellido_a': persona.apellido_a,
|
|
|
|
'apellido_b': persona.apellido_b,
|
|
|
|
'email': persona.email,
|
|
|
|
'login': usuario.login,
|
|
|
|
'vigente': usuario.vigente,
|
|
|
|
})
|
|
|
|
|
|
|
|
except ValueError as e:
|
|
|
|
transaction.rollback()
|
|
|
|
return HttpResponse(str(e), status = 400)
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
transaction.rollback()
|
|
|
|
print(e)
|
|
|
|
return HttpResponse(str(e), status = 500)
|
|
|
|
|
2024-02-25 00:08:59 -03:00
|
|
|
|
|
|
|
def partial_update(self, request, *args, **kwargs):
|
2023-12-05 10:34:47 -03:00
|
|
|
input = json.loads(request.body)
|
|
|
|
try:
|
|
|
|
pk = input['rut']
|
2024-02-25 00:08:59 -03:00
|
|
|
login = self.request.jwt_info['login']
|
|
|
|
usuario_session = models.Usuario.objects.filter(login=login).first()
|
|
|
|
|
2023-12-05 10:34:47 -03:00
|
|
|
with transaction.atomic():
|
|
|
|
|
|
|
|
# validaciones se realiza a nivel del model
|
|
|
|
persona = models.Persona.objects.filter(rut = pk).first()
|
2024-01-06 12:25:12 -03:00
|
|
|
rol = models.Rol.objects.filter(id_rol = input.get('id_rol')).first()
|
2023-12-05 10:34:47 -03:00
|
|
|
|
|
|
|
usuario = models.Usuario.objects.filter(rut = pk).first()
|
|
|
|
usuario.vigente = input.get('vigente', False)
|
2024-01-06 12:25:12 -03:00
|
|
|
usuario.superuser = input.get('superuser', False)
|
|
|
|
usuario.id_rol = rol
|
2023-12-05 10:34:47 -03:00
|
|
|
usuario.save()
|
|
|
|
|
2024-01-06 12:25:12 -03:00
|
|
|
if 'clave' in input:
|
2023-12-05 10:34:47 -03:00
|
|
|
logging.error('Modificar clave de usuario')
|
2024-01-27 00:26:58 -03:00
|
|
|
usuario.clave = make_password(input['clave'])
|
|
|
|
usuario.save()
|
2024-02-25 00:08:59 -03:00
|
|
|
|
|
|
|
save_log_usuario(accion_tabla='update', rut_tabla=persona.rut, rut_usuario_ejecutor=usuario_session.rut.rut)
|
2023-12-05 10:34:47 -03:00
|
|
|
|
|
|
|
return Response({
|
|
|
|
'rut': persona.rut,
|
|
|
|
'dv': persona.dv,
|
|
|
|
'nombres': persona.nombres,
|
|
|
|
'apellido_a': persona.apellido_a,
|
|
|
|
'apellido_b': persona.apellido_b,
|
|
|
|
'email': persona.email,
|
|
|
|
'login': usuario.login,
|
|
|
|
'vigente': usuario.vigente,
|
|
|
|
})
|
|
|
|
|
|
|
|
except ValueError as e:
|
|
|
|
transaction.rollback()
|
|
|
|
return HttpResponse(str(e), status = 400)
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
transaction.rollback()
|
2024-01-06 12:25:12 -03:00
|
|
|
logging.error(e)
|
2023-12-05 10:34:47 -03:00
|
|
|
return HttpResponse(str(e), status = 500)
|
2024-01-06 12:25:12 -03:00
|
|
|
|
2024-02-25 00:08:59 -03:00
|
|
|
|
2024-01-06 12:25:12 -03:00
|
|
|
@action(detail=False, methods=['post'])
|
2024-01-12 20:11:07 -03:00
|
|
|
def permisos(self, request):
|
2024-01-07 21:26:29 -03:00
|
|
|
input = {}
|
|
|
|
if request.body > b'':
|
|
|
|
input = json.loads(request.body)
|
2024-01-07 13:16:13 -03:00
|
|
|
|
2024-01-07 21:26:29 -03:00
|
|
|
login = request.jwt_info['login']
|
2024-01-07 13:16:13 -03:00
|
|
|
usuario = models.Usuario.objects.filter(login=login).first()
|
2024-01-12 20:11:07 -03:00
|
|
|
escritura = usuario.superuser == True
|
2024-01-07 21:26:29 -03:00
|
|
|
|
|
|
|
if 'path' in input:
|
|
|
|
path = input.get('path')
|
|
|
|
aplicacion = models.Aplicacion.objects.filter(path_app=path).first()
|
2024-01-08 21:08:31 -03:00
|
|
|
|
|
|
|
if escritura == False:
|
|
|
|
rol_aplicacion = models.RolAplicacion.objects.filter(id_rol=usuario.id_rol, id_aplicacion=aplicacion.id_aplicacion).first()
|
|
|
|
escritura = rol_aplicacion.solo_visualizar == False
|
|
|
|
|
2024-01-07 21:26:29 -03:00
|
|
|
return JsonResponse({
|
|
|
|
'path_app': path,
|
|
|
|
'nombre_app': aplicacion.nombre_app,
|
2024-01-08 21:08:31 -03:00
|
|
|
'escritura': escritura
|
2024-01-07 21:26:29 -03:00
|
|
|
})
|
|
|
|
else:
|
|
|
|
registros = models.RolAplicacion.objects.filter(id_rol=usuario.id_rol)
|
|
|
|
aplicaciones = []
|
|
|
|
for registro in registros:
|
|
|
|
if registro.id_aplicacion.vigente:
|
|
|
|
rol_app = models.RolAplicacion.objects.filter(id_rol=registro.id_rol, id_aplicacion=registro.id_aplicacion).first()
|
|
|
|
|
|
|
|
aplicaciones.append({
|
|
|
|
'path_app': registro.id_aplicacion.path_app,
|
|
|
|
'nombre_app': registro.id_aplicacion.nombre_app,
|
|
|
|
})
|
|
|
|
|
2024-01-12 20:11:07 -03:00
|
|
|
return JsonResponse({ "aplicaciones": aplicaciones })
|