# Create your views here.
import os
import time
import cx_Oracle
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from rest_framework.renderers import JSONRenderer
from rest_framework.parsers import JSONParser
from rest_framework import status
from rest_framework.decorators import api_view
from rest_framework.response import Response
from workflow_transaccion.models import Workflow_Transaccion
from workflow.models import Workflow
from transacciones.models import Transaccion
from workflow_transaccion.serializers import *
class JSONResponse(HttpResponse):
    """
    An HttpResponse that renders it's content into JSON.
    """
    def __init__(self, data, **kwargs):
        content = JSONRenderer().render(data)
        kwargs['content_type'] = 'application/json'
        super(JSONResponse, self).__init__(content, **kwargs)

#---Metodo para Modelo
@csrf_exempt
def workflowtransaccion_list(request):
    if request.method == 'GET':
        actividades = Workflow_Transaccion.objects.all()
        serializer = WorkflowTransaccionSerializer(actividades, many=True)
        response={}
        total={}
        response['status']=0
        response['totalRows']=len(actividades)
        response['startRow']=0
        response['endRow']=len(actividades)-1
        response['data']=serializer.data
        total['response']=response
        return JSONResponse(total)
    elif request.method == 'POST':
        actividades = Workflow_Transaccion.objects.all()
        serializer = WorkflowTransaccionSerializer(actividades, many=True)
        response={}
        total={}
        response['status']=0
        response['totalRows']=len(actividades)
        response['startRow']=0
        response['endRow']=len(actividades)-1
        response['data']=serializer.data
        total['response']=response
        return JSONResponse(total)

@csrf_exempt
def workflowtransaccion_ingresa(request):
    if request.method == 'GET':
        id_workflow = request.GET.get('id_workflow')
        id_transaccion = request.GET.get('id_transaccion')
        workflow=Workflow.objects.get(id_workflow=id_workflow)
        transaccion=Transaccion.objects.get(id_transaccion=id_transaccion)
        a=Workflow_Transaccion(id_workflow=workflow,id_transaccion=transaccion) 
        a.save() 
        conexiones = Workflow_Transaccion.objects.get(id_workflow_transaccion=a.id_workflow_transaccion)
        res={}
        res["id_workflow_transaccion"]=conexiones.id_workflow_transaccion
        res["id_workflow"]=id_workflow
        res["id_transaccion"]=id_transaccion
        response={}
        total={}
        response['status']=0
        response['totalRows']=1
        response['startRow']=0
        response['endRow']=0
        response['data']=res
        total['response']=response
        return JSONResponse(total)
    if request.method == 'POST':
        id_workflow = request.POST.get('id_workflow')
        id_transaccion = request.POST.get('id_transaccion')
        workflow=Workflow.objects.get(id_workflow=id_workflow)
        transaccion=Transaccion.objects.get(id_transaccion=id_transaccion)
        a=Workflow_Transaccion(id_workflow=workflow,id_transaccion=transaccion) 
        a.save() 
        conexiones = Workflow_Transaccion.objects.get(id_workflow_transaccion=a.id_workflow_transaccion)
        res={}
        res["id_workflow_transaccion"]=conexiones.id_workflow_transaccion
        res["id_workflow"]=id_workflow
        res["id_transaccion"]=id_transaccion
        response={}
        total={}
        response['status']=0
        response['totalRows']=1
        response['startRow']=0
        response['endRow']=0
        response['data']=res
        total['response']=response
        return JSONResponse(total)

@csrf_exempt
def workflowtransaccion_actualiza(request):
    if request.method == 'GET':
        id_workflow_transaccion = request.GET.get('id_workflow_transaccion')
        id_workflow = request.GET.get('id_workflow')
        id_transaccion = request.GET.get('id_transaccion')
        workflow=Workflow.objects.get(id_workflow=id_workflow)
        transaccion=Transaccion.objects.get(id_transaccion=id_transaccion)
        a = Workflow_Transaccion.objects.get(id_workflow_transaccion=id_workflow_transaccion)
        a.id_workflow = workflow
        a.id_transaccion = transaccion
        a.save() 
        res={}
        res["id_workflow_transaccion"]=id_workflow_transaccion
        res["id_workflow"]=id_workflow
        res["id_transaccion"]=id_transaccion
        response={}
        total={}
        response['status']=0
        response['totalRows']=1
        response['startRow']=0
        response['endRow']=0
        response['data']=res
        total['response']=response
        return JSONResponse(total)
    if request.method == 'POST':
        id_workflow_transaccion = request.POST.get('id_workflow_transaccion')
        id_workflow = request.POST.get('id_workflow')
        id_transaccion = request.POST.get('id_transaccion')
        workflow=Workflow.objects.get(id_workflow=id_workflow)
        transaccion=Transaccion.objects.get(id_transaccion=id_transaccion)
        a = Workflow_Transaccion.objects.get(id_workflow_transaccion=id_workflow_transaccion)
        a.id_workflow = workflow
        a.id_transaccion = transaccion
        a.save() 
        res={}
        res["id_workflow_transaccion"]=id_workflow_transaccion
        res["id_workflow"]=id_workflow
        res["id_transaccion"]=id_transaccion
        response={}
        total={}
        response['status']=0
        response['totalRows']=1
        response['startRow']=0
        response['endRow']=0
        response['data']=res
        total['response']=response
        return JSONResponse(total)

@csrf_exempt
def workflowtransaccion_elimina(request):
    if request.method == 'GET':
        id_workflow_transaccion = request.GET.get('id_workflow_transaccion')
        a = Workflow_Transaccion.objects.get(id_workflow_transaccion=id_workflow_transaccion)
        a.delete()
        borro={}
        devuelve={}
        borro['id_workflow_transaccion']=id_workflow_transaccion
        devuelve['response']=borro
        return JSONResponse(devuelve)

    if request.method == 'POST':
        id_workflow_transaccion = request.POST.get('id_workflow_transaccion')
        a = Workflow_Transaccion.objects.get(id_workflow_transaccion=id_workflow_transaccion)
        a.delete()
        borro={}
        devuelve={}
        borro['id_workflow_transaccion']=id_workflow_transaccion
        devuelve['response']=borro
        return JSONResponse(devuelve)