-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSelectDatabase.py
More file actions
96 lines (83 loc) · 3.21 KB
/
Copy pathSelectDatabase.py
File metadata and controls
96 lines (83 loc) · 3.21 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
from MiscUtils.DataTable import DataTable
from MiscUtils.Funcs import hostName as HostName
from SitePage import SitePage
class SelectDatabase(SitePage):
def writeSideBar(self):
self.writeln('<a href="?showHelp=1" class="SideBarLink">Help</a>')
def writeContent(self):
self.saveFieldsToCookies()
self.writeDBForm(action='BrowseClasses')
self.writeRecentDatabases()
self.writeKnownDatabases()
if self.request().hasField('showHelp'):
self.writeHelp()
def writeDBForm(self, method='get', action=''):
if method:
method = 'method="%s"' % method
if action:
action = 'action="%s"' % action
source = '''\
name,type,comment,value
database,text,"e.g., MySQL"
host,text
user,text
password,password
'''
fields = DataTable()
fields.readString(source)
req = self.request()
wr = self.writeln
self.writeHeading('Enter database connection info:')
wr('<form %(method)s %(action)s>' % locals())
wr('<table>')
for field in fields:
field['value'] = req.value(field['name'], '')
wr('<tr><td>%(name)s:</td><td></td><td>'
'<input type="%(type)s" name="%(name)s" value="%(value)s">'
'</td><td>%(comment)s</td></tr>' % field)
wr('<tr><td colspan="2"> </td><td style="text-align:right">'
'<input type="submit" value="OK"></td><td> </td></tr>')
wr('</table></form>')
def writeRecentDatabases(self):
self.writeHeading('Select a recent database:')
self.writeln('<p>None</p>')
def writeKnownDatabases(self):
self.writeHeading('Select a known database:')
knownDBs = self.setting('KnownDatabases')
hostName = HostName()
if not hostName:
hostName = '_default_'
dbs = knownDBs.get(hostName, []) + knownDBs.get('_all_', [])
if dbs:
for db in dbs:
self.writeDB(db)
else:
self.writeln('<p>None</p>')
def writeDB(self, db):
# Set title
title = '%(database)s on %(host)s' % db
if db.get('user', ''):
title += ' with ' + db['user']
# Build up args for links
args = []
for key in self.dbKeys():
if key in db:
args.append('%s=%s' % (key, db[key]))
args = '&'.join(args)
# If the db connection info specifies a password, then
# the user can click through immediately.
# Otherwise, the click goes back to the same page with
# the fields filled out so that the user can enter the password.
if db.get('password'):
self.write('<p><a href="BrowseClasses?%s">%s</a>'
' (password included)' % (args, title))
else:
self.writeln('<p><a href="?%s">%s</a>'
' (password required)' % (args, title))
def dbKeys(self):
"""Get keys for database connections.
Returns a list of the valid keys that can be used for a
"database connection dictionary". These dictionaries are
found in the config file and in the recent list.
"""
return ['database', 'host', 'user', 'password']