[973924]: / qiita_pet / handlers / resources.py

Download this file

135 lines (119 with data), 5.0 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
from tornado.gen import coroutine, Task
from tornado.web import authenticated, HTTPError
import json
import ast
from .base_handlers import BaseHandler
from qiita_core.qiita_settings import r_client
from qiita_core.util import execute_as_transaction
commands = 'resources:commands'
default_col_name = "samples * columns"
class ResourcesHandler(BaseHandler):
def check_admin(self):
if self.current_user.level != "admin":
raise HTTPError(403, reason="%s does not have access to portal "
"editing!" % self.current_user.id)
@execute_as_transaction
def _get_resources(self, cname, sname, version, col_name, callback):
resources = {}
vals = [
('img_mem', r_client.get),
('img_time', r_client.get),
('time', r_client.get),
('title_mem', r_client.get),
('title_time', r_client.get)
]
for k, f in vals:
redis_key = 'resources$#%s$#%s$#%s$#%s:%s' % (cname, sname,
version, col_name, k)
resources[k] = f(redis_key)
callback(resources)
@execute_as_transaction
def _get_commands(self, callback):
res = r_client.get(commands)
callback(res)
@authenticated
@coroutine
@execute_as_transaction
def get(self):
self.check_admin()
commands = yield Task(self._get_commands)
commands_str = commands.decode('utf-8')
commands_dict = ast.literal_eval(commands_str)
commands_json = json.dumps(commands_dict)
self.render('resources.html',
img_mem=None, img_time=None,
time=None,
mk=None, ma=None, mb=None,
mmodel=None, mreal=None,
mcalc=None, mfail=None,
tk=None, ta=None, tb=None,
tmodel=None, treal=None,
tcalc=None, tfail=None,
commands=commands_json,
initial_load=True)
@authenticated
@coroutine
@execute_as_transaction
def post(self):
try:
data = json.loads(self.request.body)
software = data.get('software')
version = data.get('version')
command = data.get('command')
resources = yield Task(self._get_resources, command, software,
version, default_col_name)
mcof, mmodel, mreal, mcalc, mfail = list(
map(lambda x: x.split(b": ")[1].strip().decode('utf-8'),
resources['title_mem'].split(b"\n")))
tcof, tmodel, treal, tcalc, tfail = list(
map(lambda x: x.split(b": ")[1].strip().decode('utf-8'),
resources['title_time'].split(b"\n")))
mk, ma, mb = mcof.split("||")
tk, ta, tb = tcof.split("||")
response_data = {
"status": "success",
"img_mem": resources[
'img_mem'].decode('utf-8') if isinstance(
resources['img_mem'], bytes) else resources['img_mem'],
"img_time": resources[
'img_time'].decode('utf-8') if isinstance(
resources['img_time'], bytes) else resources['img_time'],
"time": resources[
'time'].decode('utf-8') if isinstance(
resources['time'], bytes) else resources['time'],
"mk": mk, "ma": ma, "mb": mb,
"tk": tk, "ta": ta, "tb": tb,
"mmodel": mmodel, "mreal": mreal,
"mcalc": mcalc, "mfail": mfail,
"tcof": tcof,
"tmodel": tmodel, "treal": treal,
"tcalc": tcalc, "tfail": tfail,
"initial_load": False
}
self.write(json.dumps(response_data) + "\n")
except json.JSONDecodeError:
self.set_status(400)
self.finish({"error": "Invalid JSON data"})
except Exception as e:
import traceback
print(traceback.format_exc())
if resources['title_mem'] is None:
response_data = {
"status": "no_data",
"img_mem": None,
"img_time": None,
"time": None,
"mk": None, "ma": None, "mb": None,
"tk": None, "ta": None, "tb": None,
"mmodel": None, "mreal": None,
"mcalc": None, "mfail": None,
"tcof": None,
"tmodel": None, "treal": None,
"tcalc": None, "tfail": None,
"initial_load": False
}
self.set_status(200)
self.write(json.dumps(response_data) + "\n")
else:
self.set_status(500)
self.finish({"error": str(e)})