Created
May 17, 2024 21:13
-
-
Save soncco/c1de92a2d46d84fbecd25208987ae4a7 to your computer and use it in GitHub Desktop.
Crear expediente
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.shortcuts import render | |
from django.http import HttpResponseRedirect | |
from rest_framework.decorators import api_view | |
from rest_framework.response import Response | |
from rest_framework import status | |
from datetime import datetime | |
from tramite.models import Expediente, DestinoDocumento, EstadoDestinoDocumento | |
def index(request): | |
return HttpResponseRedirect('/admin') | |
@api_view(('POST',)) | |
def crear_tramite(request): | |
if request.method == 'POST': | |
expediente = Expediente( | |
numero='123', | |
fecha=datetime.now() | |
) | |
expediente.save() | |
personas = request.data.get_list('personas', []) | |
document_serializer = DocumentoSerializer(data=request.data) | |
if document_serializer.is_valid(): | |
document = document_serializer.save() | |
document.remitente = request.user | |
document.expediente = expediente | |
document.save() | |
for persona in personas: | |
destino_documento = DestinoDocumento( | |
documento=document, | |
destinatario=persona | |
) | |
destino_documento.save() | |
estado_destino_documento = EstadoDestinoDocumento( | |
destino_documento=destino_documento, | |
fecha_hora=datetime.now() | |
) | |
estado_destino_documento.save() | |
return Response(document_serializer.data, status=status.HTTP_201_CREATED) | |
else: | |
return Response(document_serializer.errors, status=status.HTTP_400_BAD_REQUEST) | |
else: | |
return Response({'error': 'Método no permitido'}, status=status.HTTP_405_METHOD_NOT_ALLOWED) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment