more json endpoints
[kismet-logviewer.git] / logviewer / devices / views.py
1 from django.shortcuts import render
2 from django.http import HttpResponse, HttpRequest
3 import os
4 import sqlite3
5 import time
6 import json
7 import pprint
8
9 from django.views.decorators.csrf import csrf_exempt
10
11 def load_db(query):
12     dir_list = os.listdir("logs/")
13     connection = sqlite3.connect("logs/"+dir_list[0])
14     #connection.row_factory = lambda cursor, row: row[0]
15     cursor = connection.cursor()
16     rows = cursor.execute(query).fetchall()
17     return(rows)
18
19 @csrf_exempt
20 def index(request,devicename):
21     print("-------------")
22     print(request.path)
23     print(devicename)
24     print("-------------")
25     if request.path[0:8] == "/devices":
26         print("FIXED")
27         dev=list(load_db("select cast(device as text) from devices where devkey = \""+devicename+"\""))
28         (dev_info,) = dev[0]
29         #dev_string = "{ \"recordsTotal\": "+str(dev_count)+", \"data\": ["
30         #dev_list = list(load_db("select cast(device as text) from devices limit 50"))
31         #for device in dev_list:
32         #    (dev,) = device
33         #    dev_string = dev_string + dev + ","
34         #dev_string = dev_string[:-1]
35         #dev_string = dev_string + "],\"draw\": 5,\"recordsFiltered\": "+str(dev_count)+"}"
36         return HttpResponse(dev_info, content_type='text/json')
37     elif request.path[0:11] == "/datasource":
38         datasource=list(load_db("select cast(json as text) from datasources where uuid = \""+str(devicename)+"\""))
39         (json_result,) = datasource[0]
40         return HttpResponse(json_result, content_type='text/json')
41