rbmd_django/rbmd/views.py
2017-09-01 17:55:39 +03:00

64 lines
1.6 KiB
Python

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.http import JsonResponse
from django.shortcuts import render
from django.contrib.auth.decorators import login_required
import json
import requests
def action(name, method, data=None):
with open('conf.json') as conf:
url = json.load(conf)["api"] + '/' + name
if method == 'get':
print 'start'
try: res = requests.get(url).content
except: res = 'connection can\'t be established'
print 'end'
elif method == 'post':
print data
try: res = requests.post(url, data).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()
res = action('mount', 'post', json.dumps(data))
return JsonResponse(res, safe=False)
def unmount(request):
data = request.GET.dict()
res = action('umount', 'post', json.dumps(data))
return JsonResponse(res, safe=False)
def resolve(request):
data = request.GET.dict()
res = action('resolve', 'post', json.dumps(data))
print 'res' + res
return JsonResponse(res, safe=False)
@login_required(login_url="/admin/login/?next=/")
def panel(request):
with open('conf.json') as conf:
ws = json.load(conf)["ws"]
print '1'
metrics = json.loads(action('metrics', 'get'))
print '2'
my_status = action('status', 'get')
print '3'
return render(request, 'rbmd/index.html', {'ws' : ws, 'metrics' : metrics, 'status' : my_status})