rbmd_django/rbmd/views.py
2017-09-01 15:05:35 +03:00

77 lines
2.2 KiB
Python

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.http import HttpResponse, JsonResponse
from django.shortcuts import render
import json
import requests
def action(name, method):
with open('conf.json') as conf:
url = json.load(conf)["api"] + '/' + name
if method == 'get':
try: res = requests.get(url).content
except: res = 'connection can\'t be established'
elif method == 'post':
try: res = requests.post(url).content
except: res = 'connection can\'t be established'
return res
'''
def status():
with open('conf.json') as conf:
status = json.load(conf)["api"] + '/status'
try: res = requests.get(status).content
except: res = 'connection can\'t be established'
return res
'''
def get_status(request):
my_status = action('status', 'get')
return JsonResponse(my_status, safe=False)
def mount(request):
if request.method == 'POST':
data = request.POST.dict()
'''
with open('conf.json') as conf:
url = json.load(conf)["api"] + '/mount'
res = requests.post(url, json.dumps(data))
print res
'''
res = action('mount', 'post')
return JsonResponse(res, safe=False)
def unmount(request):
with open('conf.json') as conf:
url = json.load(conf)["api"] + '/umount'
data = request.GET.dict()
try: res = requests.post(url, json.dumps(data)).content
except: res = 'connection can\'t be established'
return JsonResponse(res, safe=False)
def resolve():
with open('conf.json') as conf:
url = json.load(conf)["api"] + '/resolve'
data = request.GET.dict()
res = requests.post(metrics, json.dumps(data))
return JsonResponse(res.content, safe=False)
def get_metrics():
with open('conf.json') as conf:
metrics = json.load(conf)["api"] + '/metrics'
try: res = requests.get(metrics).content
except: res = 'connection can\'t be established'
return res
def panel(request):
with open('conf.json') as conf:
ws = json.load(conf)["ws"]
metrics = json.loads(get_metrics())
my_status = status()
return render(request, 'rbmd/index.html', {'ws' : ws, 'metrics' : metrics, 'status' : my_status})