forked from hubdaba/cherrypy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.py
105 lines (81 loc) · 2.95 KB
/
root.py
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
import cherrypy
import MySQLdb as mdb
import wall
import html_page
from auth import AuthController, require, member_of, name_is
SESSION_KEY = '_cp_username'
class RestrictedArea:
# all methods in this controller (and subcontrollers) is
# open only to members of the admin group
_cp_config = {
'auth.require': [member_of('admin')]
}
@cherrypy.expose
def index(self):
return """This is the admin only area."""
class Root:
_cp_config = {
'tools.sessions.on': True,
'tools.auth.on': True
}
auth = AuthController()
restricted = RestrictedArea()
@cherrypy.expose
@require()
def index(self):
return """
This page only requires a valid login.
<a href="auth/logout">LOUGOUT</a>
"""
def get_postform(self, msg="post", from_page="/", username=None):
wall1 = wall.wall(username)
wall1.body = \
"""<form method="post" action="/post">
<input type="hidden" name="from_page" value="%(from_page)s" />
%(msg)s<br />
Post: <input type="text" name="message" /> <br />
<input type="submit" value="Post" />
""" %locals()
wall1.pull(username)
return wall1.return_html()
def post_to_database(self, message, username):
con = mdb.connect('localhost', 'testuser', 'test623', 'testdb');
with con:
cur = con.cursor()
query = "INSERT INTO Message(Username, Post) VALUES('%(username)s', '%(message)s')" %locals()
cur.execute(query)
return None
return u"database error"
@cherrypy.expose
@require()
def post(self, message=None, from_page="/"):
username = cherrypy.session.get(SESSION_KEY)
if message == None:
return self.get_postform(from_page=from_page, username=username)
error_msg = self.post_to_database(message, username)
if error_msg:
return self.get_postform(self, error_msg, from_page, username)
else:
raise cherrypy.HTTPRedirect("/post")
@cherrypy.expose
def gethint(self, q):
print "hello"
print q
return "no suggestion"
@cherrypy.expose
def open(self):
home_page = html_page.html_page("html_src/showhint")
return home_page.get_html()
@cherrypy.expose
@require(name_is("joe"))
def only_for_joe(self):
return """Hello Joe - this page is available to you only"""
# This is only available if the user name is joe _and_ he's in group admin
@cherrypy.expose
@require(name_is("joe"))
@require(member_of("admin")) # equivalent: @require(name_is("joe"), member_of("admin"))
def only_for_joe_admin(self):
return """Hello Joe Admin - this page is available to you only"""
if __name__ == '__main__':
cherrypy.server.socket_port = 8081
cherrypy.quickstart(Root())