Switch to unified view

a b/qiita_pet/handlers/portal.py
1
# -----------------------------------------------------------------------------
2
# Copyright (c) 2014--, The Qiita Development Team.
3
#
4
# Distributed under the terms of the BSD 3-clause License.
5
#
6
# The full license is in the file LICENSE, distributed with this software.
7
# -----------------------------------------------------------------------------
8
9
import warnings
10
from json import dumps
11
from copy import deepcopy
12
13
from tornado.web import authenticated, HTTPError
14
15
from qiita_core.util import execute_as_transaction
16
from qiita_db.study import Study
17
from qiita_db.portal import Portal
18
from qiita_db.exceptions import QiitaDBError
19
from .base_handlers import BaseHandler
20
21
22
class PortalEditBase(BaseHandler):
23
    study_cols = ['study_id', 'study_title', 'study_alias']
24
25
    def check_admin(self):
26
        if self.current_user.level != "admin":
27
            raise HTTPError(403, reason="%s does not have access to portal "
28
                            "editing!" % self.current_user.id)
29
30
    @execute_as_transaction
31
    def get_info(self, portal="QIITA"):
32
        # Add the portals and, optionally, checkbox to the information
33
        studies = [s.id for s in Portal(portal).get_studies()]
34
        if not studies:
35
            return []
36
37
        study_info = Study.get_info(studies, info_cols=self.study_cols)
38
        info = []
39
        for s in study_info:
40
            # Make sure in correct order
41
            hold = dict(s)
42
            hold['portals'] = ', '.join(sorted(Study(s['study_id'])._portals))
43
            info.append(hold)
44
        return info
45
46
47
class StudyPortalHandler(PortalEditBase):
48
    @authenticated
49
    @execute_as_transaction
50
    def get(self):
51
        self.check_admin()
52
        info = self.get_info()
53
        portals = Portal.list_portals()
54
        headers = deepcopy(self.study_cols)
55
        headers.insert(0, "portals")
56
        self.render('portals_edit.html', headers=headers, info=info,
57
                    portals=portals, submit_url="/admin/portals/studies/")
58
59
    @authenticated
60
    @execute_as_transaction
61
    def post(self):
62
        self.check_admin()
63
        portal = self.get_argument('portal')
64
        studies = map(int, self.get_arguments('selected'))
65
        action = self.get_argument('action')
66
67
        try:
68
            portal = Portal(portal)
69
        except Exception:
70
            raise HTTPError(400, reason="Not valid portal: %s" % portal)
71
        try:
72
            with warnings.catch_warnings(record=True) as warns:
73
                if action == "Add":
74
                    portal.add_studies(studies)
75
                elif action == "Remove":
76
                    portal.remove_studies(studies)
77
                else:
78
                    raise HTTPError(400, reason="Unknown action: %s" % action)
79
        except QiitaDBError as e:
80
            self.write(action.upper() + " ERROR:<br/>" + str(e))
81
            return
82
83
        msg = '; '.join([str(w.message) for w in warns])
84
        self.write(action + " completed successfully<br/>" + msg)
85
86
87
class StudyPortalAJAXHandler(PortalEditBase):
88
    @authenticated
89
    @execute_as_transaction
90
    def get(self):
91
        self.check_admin()
92
        portal = self.get_argument('view-portal')
93
        echo = self.get_argument('sEcho')
94
        info = self.get_info(portal=portal)
95
        # build the table json
96
        results = {
97
            "sEcho": echo,
98
            "iTotalRecords": len(info),
99
            "iTotalDisplayRecords": len(info),
100
            "aaData": info
101
        }
102
103
        # return the json in compact form to save transmit size
104
        self.write(dumps(results, separators=(',', ':')))