auditor partidas
This commit is contained in:
@@ -29,8 +29,11 @@ router.register(r'coves', ViewSetCove, basename='Cove')
|
||||
router.register(r'importadores', ImportadorViewSet, basename='Importador')
|
||||
router.register(r'partidas', PartidaViewSet, basename='Partida')
|
||||
|
||||
# Import your viewsets here
|
||||
# Import auditor views
|
||||
from .views_auditor import crear_partidas_organizacion, crear_partidas_pedimento
|
||||
|
||||
urlpatterns = [
|
||||
path('', include(router.urls)),
|
||||
path('auditor/crear-partidas/organizacion/', crear_partidas_organizacion, name='crear-partidas-organizacion'),
|
||||
path('auditor/crear-partidas/pedimento/', crear_partidas_pedimento, name='crear-partidas-pedimento'),
|
||||
]
|
||||
112
api/customs/views_auditor.py
Normal file
112
api/customs/views_auditor.py
Normal file
@@ -0,0 +1,112 @@
|
||||
from rest_framework.decorators import api_view, permission_classes
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from rest_framework.response import Response
|
||||
from rest_framework import status
|
||||
from drf_yasg.utils import swagger_auto_schema
|
||||
from drf_yasg import openapi
|
||||
from core.permissions import IsSuperUser, IsSameOrganizationDeveloper
|
||||
from .tasks.auditoria import crear_partidas, crear_partidas_por_pedimento
|
||||
from api.customs.models import Pedimento
|
||||
|
||||
|
||||
@swagger_auto_schema(
|
||||
method='post',
|
||||
operation_description="Crea partidas para todos los pedimentos de una organización",
|
||||
request_body=openapi.Schema(
|
||||
type=openapi.TYPE_OBJECT,
|
||||
properties={
|
||||
'organizacion_id': openapi.Schema(type=openapi.TYPE_STRING, description='ID de la organización')
|
||||
},
|
||||
required=['organizacion_id']
|
||||
),
|
||||
responses={
|
||||
200: openapi.Response('Tarea iniciada correctamente'),
|
||||
400: openapi.Response('Error en los parámetros'),
|
||||
403: openapi.Response('No tiene permisos suficientes')
|
||||
}
|
||||
)
|
||||
@api_view(['POST'])
|
||||
@permission_classes([IsAuthenticated & (IsSuperUser | IsSameOrganizationDeveloper)])
|
||||
def crear_partidas_organizacion(request):
|
||||
"""
|
||||
Crea partidas para todos los pedimentos de una organización específica.
|
||||
"""
|
||||
organizacion_id = request.data.get('organizacion_id')
|
||||
|
||||
if not organizacion_id:
|
||||
return Response(
|
||||
{'error': 'Debe proporcionar organizacion_id'},
|
||||
status=status.HTTP_400_BAD_REQUEST
|
||||
)
|
||||
|
||||
# Validar permisos
|
||||
user = request.user
|
||||
if not user.is_superuser and str(user.organizacion.id) != organizacion_id:
|
||||
return Response(
|
||||
{'error': 'No tiene permisos para esta organización'},
|
||||
status=status.HTTP_403_FORBIDDEN
|
||||
)
|
||||
|
||||
# Ejecutar la tarea
|
||||
task = crear_partidas.delay(organizacion_id)
|
||||
message = f"Creación de partidas iniciada para la organización {organizacion_id}"
|
||||
|
||||
return Response({
|
||||
'message': message,
|
||||
'task_id': task.id
|
||||
}, status=status.HTTP_200_OK)
|
||||
|
||||
@swagger_auto_schema(
|
||||
method='post',
|
||||
operation_description="Crea partidas para un pedimento específico",
|
||||
request_body=openapi.Schema(
|
||||
type=openapi.TYPE_OBJECT,
|
||||
properties={
|
||||
'pedimento_id': openapi.Schema(type=openapi.TYPE_STRING, description='ID del pedimento')
|
||||
},
|
||||
required=['pedimento_id']
|
||||
),
|
||||
responses={
|
||||
200: openapi.Response('Tarea iniciada correctamente'),
|
||||
400: openapi.Response('Error en los parámetros'),
|
||||
403: openapi.Response('No tiene permisos suficientes'),
|
||||
404: openapi.Response('Pedimento no encontrado')
|
||||
}
|
||||
)
|
||||
@api_view(['POST'])
|
||||
@permission_classes([IsAuthenticated & (IsSuperUser | IsSameOrganizationDeveloper)])
|
||||
def crear_partidas_pedimento(request):
|
||||
"""
|
||||
Crea partidas para un pedimento específico.
|
||||
"""
|
||||
pedimento_id = request.data.get('pedimento_id')
|
||||
|
||||
if not pedimento_id:
|
||||
return Response(
|
||||
{'error': 'Debe proporcionar pedimento_id'},
|
||||
status=status.HTTP_400_BAD_REQUEST
|
||||
)
|
||||
|
||||
# Validar permisos y existencia del pedimento
|
||||
try:
|
||||
pedimento = Pedimento.objects.get(id=pedimento_id)
|
||||
user = request.user
|
||||
if not user.is_superuser and str(pedimento.organizacion.id) != str(user.organizacion.id):
|
||||
return Response(
|
||||
{'error': 'No tiene permisos para este pedimento'},
|
||||
status=status.HTTP_403_FORBIDDEN
|
||||
)
|
||||
except Pedimento.DoesNotExist:
|
||||
return Response(
|
||||
{'error': 'Pedimento no encontrado'},
|
||||
status=status.HTTP_404_NOT_FOUND
|
||||
)
|
||||
|
||||
# Ejecutar la tarea
|
||||
task = crear_partidas_por_pedimento.delay(pedimento_id)
|
||||
message = f"Creación de partidas iniciada para el pedimento {pedimento_id}"
|
||||
|
||||
return Response({
|
||||
'message': message,
|
||||
'task_id': task.id
|
||||
}, status=status.HTTP_200_OK)
|
||||
Reference in New Issue
Block a user