Add mysql database stuff
[public/pyceo-broken.git] / ceo / urwid / databases.py
1 import urwid
2 from ceo import members, mysql
3 from ceo.urwid import search
4 from ceo.urwid.widgets import *
5 from ceo.urwid.window import *
6
7 class IntroPage(WizardPanel):
8     def init_widgets(self):
9         self.widgets = [
10             urwid.Text("MySQL databases"),
11             urwid.Divider(),
12             urwid.Text("Members and hosted clubs may have one MySQL database each. You may "
13                        "create a database for an account if: \n"
14                        "\n"
15                        "- It is your personal account,\n"
16                        "- It is a club account, and you are in the club group, or\n"
17                        "- You are on the CSC systems committee\n"
18                        "\n"
19                        "You may also use this to reset your database password."
20                        )
21         ]
22     def focusable(self):
23         return False
24
25 class UserPage(WizardPanel):
26     def init_widgets(self):
27         self.userid = LdapWordEdit(csclub_uri, csclub_base, 'uid',
28             "Username: ")
29
30         self.widgets = [
31             urwid.Text("Member Information"),
32             urwid.Divider(),
33             urwid.Text("Enter the user which will own the new database."),
34             urwid.Divider(),
35             self.userid,
36         ]
37     def check(self):
38         self.state['userid'] = self.userid.get_edit_text()
39         self.state['member'] = None
40         if self.state['userid']:
41             self.state['member'] = members.get(self.userid.get_edit_text())
42         if not self.state['member']:
43             set_status("Member not found")
44             self.focus_widget(self.userid)
45             return True
46
47 class EndPage(WizardPanel):
48     def init_widgets(self):
49         self.headtext = urwid.Text("")
50         self.midtext = urwid.Text("")
51
52         self.widgets = [
53             self.headtext,
54             urwid.Divider(),
55             self.midtext,
56         ]
57     def focusable(self):
58         return False
59     def activate(self):
60         problem = None
61         try:
62             password = mysql.create_mysql(self.state['userid'])
63             self.headtext.set_text("MySQL database created")
64             self.midtext.set_text("Connection Information: \n"
65                                   "\n"
66                                   "Database: %s\n"
67                                   "Username: %s\n"
68                                   "Hostname: localhost\n"
69                                   "Password: %s\n"
70                                   "\n"
71                                   "Note: Databases are only accessible from caffeine\n"
72                                   % (self.state['userid'], self.state['userid'], password))
73         except mysql.MySQLException, e:
74             self.headtext.set_text("Failed to create MySQL database")
75             self.midtext.set_text("We failed to create the database. The error was:\n\n%s" % e)
76
77     def check(self):
78         pop_window()