root.py 9.78 KB
Newer Older
1
2
3
# -*- coding: utf-8 -*-

# imports
4
5
import vispa
from vispa.controller import AbstractController
6
from vispa.controller.ajax import AjaxController
murban's avatar
murban committed
7
from vispa.controller.filesystem import FSController
8
from vispa.controller.bus import BusController
Marcel's avatar
Marcel committed
9
from vispa.controller.error import ErrorController
10
from vispa.models.user import User
11
from vispa.models.workspace import Workspace
12
from vispa.models.preference import VispaPreference, ExtensionPreference
13
from vispa.models.shortcuts import VispaShortcuts, ExtensionShortcuts
14
import cherrypy
Gero Müller's avatar
Gero Müller committed
15
import logging
16
import os
17
import json as JSON
18

Gero Müller's avatar
Gero Müller committed
19
logger = logging.getLogger(__name__)
20

21

22
class RootController(AbstractController):
23

24
    def __init__(self):
25
        """
26
27
        The Constructor. Members from other classes
        are added as pages for cherrypy URL mapping.
28
        """
29
30
31
32
33
34
35
36
37
        AbstractController.__init__(self)
        self.ajax = AjaxController()
        self.fs = FSController()
        # self.error = ErrorController()
        self.extensions = AbstractController()
        self.bus = BusController()
        self.common_js = []
        self.common_css = []

38
39
40
41
42
43
44
45
    @cherrypy.expose
    @cherrypy.tools.workspace(on=False)
    @cherrypy.tools.signature()
    def sigtest(self, o, l, i, s):
        for elem in (o, l, i, s):
            print "%s (%s)" % (elem, type(elem))
        return "geht"

46
47
48
49
50
51
52
53
    def add_common_js(self, filepath):
        self.common_js.append(filepath)

    def add_common_css(self, filepath):
        self.common_css.append(filepath)

    def mount_extension_controller(self, mountpoint, controller):
        if hasattr(self.extensions, mountpoint):
54
            logger.warning("Controller mountpoint already exists: %s" % mountpoint)
55
        else:
56
            logger.info("Mounting controller '%s'" % os.path.join(os.sep, mountpoint))
57
            setattr(self.extensions, mountpoint, controller)
58

59
    def requested_path(self, delete=True):
60
61
62
        path = ""
        if "requested_path" in cherrypy.session.keys():
            path += cherrypy.session["requested_path"]
63
            if delete:
64
65
66
67
                del cherrypy.session["requested_path"]
        if "query_string" in cherrypy.session.keys():
            if cherrypy.session["query_string"]:
                path += "?" + cherrypy.session["query_string"]
68
                if delete:
69
                    del cherrypy.session["query_string"]
Marcel's avatar
Marcel committed
70
71
        return path

72
73
74
75
    def get_preferences(self, db, user_id, parse_json=False):
        preferences = {}
        preferences["vispa_preferences"] = VispaPreference.get_data_by_user_id(db, user_id)
        preferences["extension_preferences"] = ExtensionPreference.get_data_by_user_id(db, user_id)
76
77
        preferences["vispa_shortcuts"] = VispaShortcuts.get_data_by_user_id(db, user_id)
        preferences["extension_shortcuts"] = ExtensionShortcuts.get_data_by_user_id(db, user_id)
78
79
80
81
82
83
84
85
86
87
88
89
        # parse?
        if not parse_json:
            return preferences
        else:
            parsed_preferences = {}
            for key, value in preferences.items():
                parsed_preference = {}
                for key2, value2 in value.items():
                    parsed_preference[key2] = JSON.loads(value2)
                parsed_preferences[key] = JSON.dumps(parsed_preference)
            return parsed_preferences

90
    def workspace_data(self, workspace_id=None, keys=None):
91
92
        keys = keys or ["id", "user_id", "name", "host", "login", "key", "basedir", "command", "created"]
        user_id = self.get("user_id")
93
94
95
        workspace_data = {}
        workspaces = Workspace.get_user_workspaces(cherrypy.request.db, user_id)
        for workspace in workspaces:
96
            data = workspace.make_dict(keys=keys)
97
            if workspace_id == workspace.id:
98
                return data
Marcel Rieger's avatar
Marcel Rieger committed
99
            workspace_data[int(workspace.id)] = data
100
101
        return workspace_data

102
    @cherrypy.expose
103
104
    @cherrypy.tools.allow(methods=["GET"])
    @cherrypy.tools.stats(page="index")
105
    @cherrypy.tools.render(template="html/sites/index.html")
106
    def index2(self, *args, **kwargs):
Marcel's avatar
Marcel committed
107
108
        db = cherrypy.request.db
        username = cherrypy.request.user.name
109
110
111
        devmode = vispa.config("web", "devmode", True)
        use_websockets = vispa.config("websockets", "enabled", False)
        secure_websockets = vispa.config("websockets", "secure", False)
112
        preferences = self.get_preferences(db, cherrypy.request.user.id, parse_json=True)
113
114
115
116
        client_logging_enabled = vispa.config("web", "client_logging_enabled", True)
        client_logging_level = vispa.config("web", "client_logging_level", "info")
        client_logging_ignore = vispa.config("web", "client_logging_ignore", [])
        workspace_ids = self.get("workspace_ids")
Marcel's avatar
Marcel committed
117
        workspace_data = self.workspace_data()
Marcel Rieger's avatar
Marcel Rieger committed
118
        workspace_data = JSON.dumps(workspace_data)
119
        add_workspaces = vispa.config("workspace", "allow_new_workspaces", True)
120
121
122
123
124
125
126
127
128
129
130
131
        data = {"devmode"          : devmode,
                "username"         : username,
                "common_js"        : self.common_js,
                "common_css"       : self.common_css,
                "use_websockets"   : use_websockets,
                "secure_websockets": secure_websockets,
                "workspace_ids"    : self.convert(workspace_ids, int),
                "workspace_data"   : workspace_data,
                "add_workspaces"   : add_workspaces,
                "logging_enabled"  : client_logging_enabled,
                "logging_level"    : client_logging_level,
                "logging_ignore"   : client_logging_ignore}
132
        data.update(preferences)
Marcel's avatar
Marcel committed
133
134
135
        return data

    @cherrypy.expose
136
    @cherrypy.tools.workspace(on=False)
137
138
    @cherrypy.tools.allow(methods=["GET"])
    @cherrypy.tools.stats(page="bootstrap")
139
140
    @cherrypy.tools.render(template="sites/index.mako")
    def index(self, *args, **kwargs):
141
        db = cherrypy.request.db
Marcel Rieger's avatar
Marcel Rieger committed
142
143
144
145
        username = cherrypy.request.user.name
        dev_mode = vispa.config("web", "dev_mode", True)
        # use_websockets = vispa.config("websockets", "enabled", False)
        # secure_websockets = vispa.config("websockets", "secure", False)
146
        preferences = self.get_preferences(db, cherrypy.request.user.id, parse_json=True)
Marcel Rieger's avatar
Marcel Rieger committed
147
        client_log_level = vispa.config("web", "client_log_level", "info")
148
149
150
151
        workspace_ids = self.convert(self.get("workspace_ids"), int)
        workspace_ids = JSON.dumps(workspace_ids)
        workspace_data = self.workspace_data()
        workspace_data = JSON.dumps(workspace_data)
152
        # add_workspaces = vispa.config("workspace", "allow_new_workspaces", True)
Marcel Rieger's avatar
Marcel Rieger committed
153
154
155
        data = {
            "dev_mode"         : dev_mode,
            "username"         : username,
156
157
            "common_js"        : self.common_js,
            "common_css"       : self.common_css,
Marcel Rieger's avatar
Marcel Rieger committed
158
159
            # "use_websockets"   : use_websockets,
            # "secure_websockets": secure_websockets,
160
161
            "workspace_ids"    : workspace_ids,
            "workspace_data"   : workspace_data,
Marcel Rieger's avatar
Marcel Rieger committed
162
163
            # "add_workspaces"   : add_workspaces,
            "log_level"        : client_log_level,
164
        }
165
        data.update(preferences)
Marcel Rieger's avatar
Marcel Rieger committed
166
        return data
167
168

    @cherrypy.expose
Gero Müller's avatar
Gero Müller committed
169
    @cherrypy.tools.workspace(on=False)
170
171
172
    @cherrypy.tools.user(reverse=True, path="/")
    @cherrypy.tools.allow(methods=["GET"])
    @cherrypy.tools.stats(page="login")
173
    @cherrypy.tools.render(template="html/sites/login.html")
Marcel's avatar
Marcel committed
174
    def login(self, *args, **kwargs):
Marcel's avatar
Marcel committed
175
        path = self.requested_path()
176
177
178
179
180
181
182
183
184
185
186
        welcome_text = vispa.config("web", "welcome_text", "")
        login_text = vispa.config("web", "login_text", "")
        registration_text = vispa.config("web", "registration_text", "")
        forgot_text = vispa.config("web", "forgot_text", "")
        use_forgot = vispa.config("web", "use_forgot", False)
        return {"requested_path"   : path,
                "welcome_text"     : welcome_text,
                "login_text"       : login_text,
                "registration_text": registration_text,
                "forgot_text"      : forgot_text,
                "use_forgot"       : use_forgot}
187
188
189

    @cherrypy.expose
    @cherrypy.tools.user()
190
    @cherrypy.tools.workspace(on=False)
191
    def logout(self):
Gero Müller's avatar
Gero Müller committed
192
        vispa.fire_callback("user.logout", cherrypy.request.user)
193
        cherrypy.lib.sessions.expire()
194
        raise cherrypy.HTTPRedirect(vispa.url.dynamic("/"))
195
196
197

    @cherrypy.expose
    @cherrypy.tools.user(reverse=True)
198
    @cherrypy.tools.render(template="html/sites/activate.html")
199
200
201
    def activate(self, hash, *args, **kwargs):
        user = User.activate(cherrypy.request.db, hash)
        if not isinstance(user, User):
202
            return self.success()
203
        else:
204
            return self.fail()
205
206
207

    @cherrypy.expose
    @cherrypy.tools.user(reverse=True)
208
    @cherrypy.tools.render(template="html/sites/forgot.html")
209
210
    def forgot(self, hash, *args, **kwargs):
        user = User.get_by_hash(cherrypy.request.db, hash)
211
        if isinstance(user, User):
212
            return self.success({"hash": hash})
213
        else:
214
            return self.fail()
215

216
    @cherrypy.expose
Gero Müller's avatar
Gero Müller committed
217
    @cherrypy.tools.workspace(on=False)
218
219
    @cherrypy.tools.allow(methods=["GET"])
    @cherrypy.tools.stats(page="workspace")
220
    @cherrypy.tools.render(template="html/sites/workspace.html")
221
    def workspace(self, *args, **kwargs):
Marcel's avatar
Marcel committed
222
        path = self.requested_path()
223
        show_add_form = vispa.config("workspace", "allow_new_workspaces", True)
224
        workspace_data = self.workspace_data()
Marcel Rieger's avatar
Marcel Rieger committed
225
        workspace_data = JSON.dumps(workspace_data)
226
227
228
        return {"requested_path": path,
                "show_add_form" : show_add_form,
                "workspace_data": workspace_data}
229

230
231
    @cherrypy.expose
    @cherrypy.tools.user()
232
    @cherrypy.tools.allow(methods=["GET"])
233
    def resetworkspace(self, *args, **kwargs):
234
235
236
        if "workspace_id" in cherrypy.session.keys():
            del cherrypy.session["workspace_id"]
        raise cherrypy.HTTPRedirect(vispa.url.dynamic("/"))