# 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 conexiones.models import Conexion
from conexiones.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)

#----Modelo para Conexiones
@csrf_exempt
def conexion_list(request):
    if request.method == 'GET':
        actividades = Conexion.objects.all()
        serializer = ConexionSerializer(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 = Conexion.objects.all()
        serializer = ConexionSerializer(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 conexion_ingresa(request):
    if request.method == 'GET':
        descripcion = request.GET.get('descripcion')
        if descripcion is None:
            descripcion = None
        sid = request.GET.get('sid')
        username  = request.GET.get('username')
        password  = request.GET.get('password')
        puerto  = request.GET.get('puerto')
        host  = request.GET.get('host')
        driver = request.GET.get('driver')
        esquema = request.GET.get('esquema')
        service_name = request.GET.get('service_name')

        a = Conexion(descripcion=descripcion, sid=sid, username=username, password=password, puerto=puerto, host=host, driver=driver, esquema=esquema, service_name=service_name)
        a.save() 
        conexiones = Conexion.objects.get(id_conexion=a.id_conexion)
        serializer = ConexionSerializer(conexiones)
        response = {}
        total = {}
        response['status'] = 0
        response['totalRows'] = 1
        response['startRow'] = 0
        response['endRow'] = 0
        response['data'] = serializer.data
        total['response'] = response
        return JSONResponse(total)

    if request.method == 'POST':
        descripcion = request.POST.get('descripcion')
        if descripcion is None:
            descripcion = None
        sid = request.POST.get('sid')
        username  = request.POST.get('username')
        password  = request.POST.get('password')
        puerto  = request.POST.get('puerto')
        host  = request.POST.get('host')
        driver = request.POST.get('driver')
        esquema = request.POST.get('esquema')
        service_name = request.POST.get('service_name')

        a = Conexion(descripcion=descripcion, sid=sid, username=username, password=password, puerto=puerto, host=host, driver=driver, esquema=esquema, service_name=service_name)
        a.save() 
        conexiones = Conexion.objects.get(id_conexion=a.id_conexion)
        serializer = ConexionSerializer(conexiones)
        response = {}
        total = {}
        response['status'] = 0
        response['totalRows'] = 1
        response['startRow'] = 0
        response['endRow'] = 0
        response['data'] = serializer.data
        total['response'] = response
        return JSONResponse(total)

@csrf_exempt
def conexion_actualiza(request):
    if request.method == 'GET':
        id_conexion = request.GET.get('id_conexion')
        descripcion = request.GET.get('descripcion')
        if descripcion is None:
            descripcion = None
        sid = request.GET.get('sid')
        username  = request.GET.get('username')
        password  = request.GET.get('password')
        puerto  = request.GET.get('puerto')
        host  = request.GET.get('host')
        driver = request.GET.get('driver')
        esquema = request.GET.get('esquema')
        service_name = request.GET.get('service_name')

        conexiones = Conexion.objects.get(id_conexion=id_conexion)
        conexiones.descripcion = descripcion
        conexiones.sid = sid
        conexiones.username = username
        conexiones.password = password
        conexiones.puerto = puerto
        conexiones.host = host
        conexiones.driver = driver
        conexiones.esquema = esquema
        conexiones.service_name = service_name

        conexiones.save()
        serializer = ConexionSerializer(conexiones)
        response = {}
        total = {}
        response['status'] = 0
        response['totalRows'] = 1
        response['startRow'] = 0
        response['endRow'] = 0
        response['data'] = serializer.data
        total['response'] = response
        return JSONResponse(total)

    if request.method == 'POST':
        id_conexion = request.POST.get('id_conexion')
        descripcion = request.POST.get('descripcion')
        if descripcion is None:
            descripcion=None
        sid = request.POST.get('sid')
        username  = request.POST.get('username')
        password  = request.POST.get('password')
        puerto  = request.POST.get('puerto')
        host  = request.POST.get('host')
        driver = request.POST.get('driver')
        esquema = request.POST.get('esquema')
        service_name = request.POST.get('service_name')

        conexiones = Conexion.objects.get(id_conexion=id_conexion)
        conexiones.descripcion = descripcion
        conexiones.sid = sid
        conexiones.username = username
        conexiones.password = password
        conexiones.puerto = puerto
        conexiones.host = host
        conexiones.driver = driver
        conexiones.esquema = esquema
        conexiones.service_name = service_name

        conexiones.save()
        serializer = ConexionSerializer(conexiones)
        response = {}
        total = {}
        response['status'] = 0
        response['totalRows'] = 1
        response['startRow'] = 0
        response['endRow'] = 0
        response['data'] = serializer.data
        total['response'] = response
        return JSONResponse(total)

@csrf_exempt
def conexion_elimina(request):
    if request.method == 'GET':
        id_conexion = request.GET.get('id_conexion')
        transaccion = Conexion.objects.get(id_conexion=id_conexion)
        transaccion.delete()
        borro={}
        devuelve={}
        borro['id_conexion']=id_conexion
        devuelve['response']=borro
        return JSONResponse(devuelve)
    if request.method == 'POST':
        id_conexion = request.POST.get('id_conexion')
        transaccion = Conexion.objects.get(id_conexion=id_conexion)
        transaccion.delete()
        borro = {}
        devuelve = {}
        borro['id_conexion'] = id_conexion
        devuelve['response'] = borro
        return JSONResponse(devuelve)

