rbmd_django/rbmd/views.py

62 lines
1.5 KiB
Python
Raw Normal View History

2017-08-31 00:52:20 +03:00
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
2017-09-01 17:42:27 +03:00
from django.http import JsonResponse
2017-08-31 00:52:20 +03:00
from django.shortcuts import render
import json
import requests
2017-09-01 15:05:35 +03:00
2017-09-01 17:42:27 +03:00
def action(name, method, data=None):
2017-09-01 15:05:35 +03:00
with open('conf.json') as conf:
url = json.load(conf)["api"] + '/' + name
if method == 'get':
2017-09-01 17:42:27 +03:00
print 'start'
2017-09-01 15:05:35 +03:00
try: res = requests.get(url).content
except: res = 'connection can\'t be established'
2017-09-01 17:42:27 +03:00
print 'end'
2017-09-01 15:05:35 +03:00
elif method == 'post':
2017-09-01 17:42:27 +03:00
print data
try: res = requests.post(url, data).content
2017-09-01 15:05:35 +03:00
except: res = 'connection can\'t be established'
return res
2017-09-01 17:42:27 +03:00
2017-09-01 14:52:47 +03:00
def get_status(request):
2017-09-01 17:42:27 +03:00
2017-09-01 15:05:35 +03:00
my_status = action('status', 'get')
2017-09-01 14:52:47 +03:00
return JsonResponse(my_status, safe=False)
2017-08-31 00:52:20 +03:00
2017-09-01 14:52:47 +03:00
def mount(request):
2017-09-01 17:42:27 +03:00
2017-09-01 14:52:47 +03:00
if request.method == 'POST':
data = request.POST.dict()
2017-09-01 17:42:27 +03:00
res = action('mount', 'post', json.dumps(data))
2017-09-01 15:05:35 +03:00
return JsonResponse(res, safe=False)
2017-08-31 00:52:20 +03:00
2017-09-01 14:52:47 +03:00
def unmount(request):
2017-09-01 17:42:27 +03:00
2017-09-01 14:52:47 +03:00
data = request.GET.dict()
2017-09-01 17:42:27 +03:00
res = action('umount', 'post', json.dumps(data))
2017-09-01 14:52:47 +03:00
return JsonResponse(res, safe=False)
2017-08-31 00:52:20 +03:00
2017-09-01 17:42:27 +03:00
def resolve(request):
2017-09-01 14:52:47 +03:00
data = request.GET.dict()
2017-09-01 17:42:27 +03:00
res = action('resolve', 'post', json.dumps(data))
print 'res' + res
return JsonResponse(res, safe=False)
2017-08-31 18:10:08 +03:00
def panel(request):
with open('conf.json') as conf:
ws = json.load(conf)["ws"]
2017-09-01 17:42:27 +03:00
print '1'
metrics = json.loads(action('metrics', 'get'))
print '2'
my_status = action('status', 'get')
print '3'
2017-09-01 14:52:47 +03:00
return render(request, 'rbmd/index.html', {'ws' : ws, 'metrics' : metrics, 'status' : my_status})
2017-08-31 18:10:08 +03:00