El SP de momento lo único que hace es un SELECT TOP 100 de una tabla X con los cuatro campos que son
id, nombre, descripcion, id_problema_padre
Este es el archivo de
models.py
Código Python:
Ver originalfrom django.db import models
from django.db import connection
class QueryExecution(models.Model):
#Initializate the SP fields name
nombre = models.CharField(max_length=150)
descripcion = models.TextField()
id_problema_padre = models.IntegerField()
@staticmethod
def search(request):
cur = connection.cursor()
cur.callproc('todo_neg', request,)
results = cur.fetchall()
cur.close()
return [QueryExecution(*row) for row in results]
Este es el
views.py
Código Python:
Ver originalfrom 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 ws.serializers import WSSerializer
from models import QueryExecution
class JSONResponce(HttpResponse):
def __init__(self, data, **kwargs):
content = JSONRenderer().render(data)
kwargs['content_type'] = 'application/json'
super(JSONResponce, self).__init__(content, **kwargs)
@csrf_exempt
def ws_list(request):
if request.method == 'GET':
QResult = QueryExecution.objects.all()
serializer = WSSerializer(QResult, many=True)
return JSONResponce(serializer.data)
elif request.method == 'POST':
data = JSONParser().parse(request)
serializer = WSSerializer(data=data)
if serializer.is_valid():
serializer.save()
return JSONResponce(serializer.data, status=201)
else:
return JSONResponce(serializer.data, status=400)
@csrf_exempt
def ws_detail(request, pk):
try:
ws = QueryExecution.objects.get(pk=pk)
except QueryExecution.DoesNotExist:
return HttpResponse(status=404)
if request.method == 'GET':
serializer = WSSerializer(ws)
return JSONResponce(serializer.data)
elif request.method == 'PUT':
data = JSONParser().parse(request)
serializer = WSSerializer(ws, data=data)
if serializer.is_valid():
serializer.save()
return JSONResponce(serializer.data)
else:
return JSONResponce(serializer.errors, status=400)
elif request.method == 'DELETE':
ws.delete()
return HttpResponse(status=204)
Este es el
serializers.py
Código Python:
Ver originalfrom rest_framework import serializers
from ws.models import QueryExecution
class WSSerializer(serializers.ModelSerializer):
class Meta:
model = QueryExecution
fields = ('id', 'nombre', 'descripcion', 'id_problema_padre')