e3c8c9d486107af95c9b300300a6679f8ed0c94e
[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         dev=list(load_db("select cast(device as text) from devices where devkey = \""+devicename+"\""))
27         (dev_info,) = dev[0]
28         #dev_string = "{ \"recordsTotal\": "+str(dev_count)+", \"data\": ["
29         #dev_list = list(load_db("select cast(device as text) from devices limit 50"))
30         #for device in dev_list:
31         #    (dev,) = device
32         #    dev_string = dev_string + dev + ","
33         #dev_string = dev_string[:-1]
34         #dev_string = dev_string + "],\"draw\": 5,\"recordsFiltered\": "+str(dev_count)+"}"
35         return HttpResponse(dev_info, content_type='text/json')
36     elif request.path[0:11] == "/datasource":
37         datasource=list(load_db("select cast(json as text) from datasources where uuid = \""+str(devicename)+"\""))
38         (json_result,) = datasource[0]
39         return HttpResponse(json_result, content_type='text/json')
40