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

Download this file

105 lines (88 with data), 3.6 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
# -----------------------------------------------------------------------------
# Copyright (c) 2014--, The Qiita Development Team.
#
# Distributed under the terms of the BSD 3-clause License.
#
# The full license is in the file LICENSE, distributed with this software.
# -----------------------------------------------------------------------------
import warnings
from json import dumps
from copy import deepcopy
from tornado.web import authenticated, HTTPError
from qiita_core.util import execute_as_transaction
from qiita_db.study import Study
from qiita_db.portal import Portal
from qiita_db.exceptions import QiitaDBError
from .base_handlers import BaseHandler
class PortalEditBase(BaseHandler):
study_cols = ['study_id', 'study_title', 'study_alias']
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_info(self, portal="QIITA"):
# Add the portals and, optionally, checkbox to the information
studies = [s.id for s in Portal(portal).get_studies()]
if not studies:
return []
study_info = Study.get_info(studies, info_cols=self.study_cols)
info = []
for s in study_info:
# Make sure in correct order
hold = dict(s)
hold['portals'] = ', '.join(sorted(Study(s['study_id'])._portals))
info.append(hold)
return info
class StudyPortalHandler(PortalEditBase):
@authenticated
@execute_as_transaction
def get(self):
self.check_admin()
info = self.get_info()
portals = Portal.list_portals()
headers = deepcopy(self.study_cols)
headers.insert(0, "portals")
self.render('portals_edit.html', headers=headers, info=info,
portals=portals, submit_url="/admin/portals/studies/")
@authenticated
@execute_as_transaction
def post(self):
self.check_admin()
portal = self.get_argument('portal')
studies = map(int, self.get_arguments('selected'))
action = self.get_argument('action')
try:
portal = Portal(portal)
except Exception:
raise HTTPError(400, reason="Not valid portal: %s" % portal)
try:
with warnings.catch_warnings(record=True) as warns:
if action == "Add":
portal.add_studies(studies)
elif action == "Remove":
portal.remove_studies(studies)
else:
raise HTTPError(400, reason="Unknown action: %s" % action)
except QiitaDBError as e:
self.write(action.upper() + " ERROR:<br/>" + str(e))
return
msg = '; '.join([str(w.message) for w in warns])
self.write(action + " completed successfully<br/>" + msg)
class StudyPortalAJAXHandler(PortalEditBase):
@authenticated
@execute_as_transaction
def get(self):
self.check_admin()
portal = self.get_argument('view-portal')
echo = self.get_argument('sEcho')
info = self.get_info(portal=portal)
# build the table json
results = {
"sEcho": echo,
"iTotalRecords": len(info),
"iTotalDisplayRecords": len(info),
"aaData": info
}
# return the json in compact form to save transmit size
self.write(dumps(results, separators=(',', ':')))