source: cows_wps/trunk/cows_wps/controllers/admin.py @ 7115

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/cows_wps/trunk/cows_wps/controllers/admin.py@7115
Revision 7115, 3.2 KB checked in by astephen, 10 years ago (diff)

more work on admin interface etc.

Line 
1import logging
2
3from pylons import request, response, session, tmpl_context as c
4from pylons.controllers.util import abort, redirect_to
5
6from cows_wps.model.managers import requestManager
7from cows_wps.model.orm import tables
8
9from cows_wps.renderer.ui_renderer import *
10from cows_wps.controllers import *
11from cows_wps.lib.ui.proc_config import *
12
13log = logging.getLogger(__name__)
14
15
16class AdminController(BaseController):
17
18    def index(self):
19        sess = requestManager.getSession()
20        res = sess.query(tables.admin)[0]
21        admin_u = str(res.admin_name)
22        admin_p = str(res.admin_password)
23
24        admin_name = None
25        admin_password = None
26 
27        if "admin_name" in request.params.keys():
28            admin_name = str(request.params.getone("admin_name"))
29        if "admin_password" in request.params.keys():
30            admin_password = str(request.params.getone("admin_password"))
31
32        err_msg = ""
33
34        if admin_name != None:
35            if admin_name != admin_u or admin_password == None: 
36                err_msg = "Incorrect login details provided. Please try again."
37                return self.login(err_msg = err_msg)
38            else:
39                if admin_password != admin_p:
40                    err_msg = "Incorrect login details provided. Please try again."
41                    return self.login(err_msg = err_msg)
42                else:
43                    return self._successfulLogin()
44        else:
45            return self.login()
46       
47
48    def _successfulLogin(self):
49        response.set_cookie('wps_ui_admin', request.environ["REMOTE_ADDR"], expires = 3600)
50        html = """<p>You have successfully logged in.</p><p>Please try using the <a href="/jobs">jobs page</a> to view and manage everyone's jobs.</p>"""
51        renderer = UIPageRenderer()
52        resp = renderer.render("Administrator page",
53                               [("Administrator", html)],
54                               admin = True)
55        return resp
56
57
58    def login(self, err_msg = ""):
59        """
60        Show admin login page.
61        """
62        login_form = ""
63
64        if err_msg != "":
65            login_form = """<center><div id="bad_login" style="width: 600px; border-width: 0px; background: orange; color: white; font-weight: bold; padding: 3px;">%s</div></center>""" % err_msg
66
67        login_form += """<form action="/admin" onSubmit="return validateTextInput(document.getElementById('admin_name').value, /^[a-zz0-9A-Z]{4,20}$/, 'The user name must be between 4 and 20 letters and/or numbers.');" >
68                <p>Please enter your details</p>
69                <label class="text_width_150"><b>Username: </b></label>
70                <input type="text" name="admin_name" id="username" value="" size="20" /><br />
71                <label class="text_width_150"><b>Password: </b></label>
72                <input type="password" name="admin_password" value="" size="20" />&nbsp;&nbsp;
73                <input type="submit" value="Login" />
74        </form>
75        """
76        renderer = UIPageRenderer()
77        resp = renderer.render("Administrator page", 
78                               [("Login", login_form)])
79        return resp
80       
81    def logout(self):
82        return str(dir(request.params["wps_ui_admin"]))
83        return self.login(err_msg = "You have successfully logged out.")
Note: See TracBrowser for help on using the repository browser.