Expand|Select|Wrap|Line Numbers
- #Boa:Dialog:DBConnectDialog
- import wx
- ##"""Given a set if login specs, create a dbServer instance and
- ## ensure that there is a valid, open connection to the database.
- ## If not, set the dbConnect to None.
- ## spec: dict(UserName=name, Password=pswd, ServerName=host)
- ## Allow names to be set with Default Holder. Consiter Dictionary
- ## or even a tuple? (name, pswd, host)"""
- import sys
- import wxdbtools as db
- def create(parent, loginspec):
- return DBConnectDialog(parent, loginspec)
- [wxID_DBCONNECTDIALOG, wxID_DBCONNECTDIALOGCANCELBUTTON, wxID_DBCONNECTDIALOGCONNECTBUTTON,
- wxID_DBCONNECTDIALOGOKBUTTON, wxID_DBCONNECTDIALOGPROMPTSTATICTEXT,
- wxID_DBCONNECTDIALOGPWDTEXTCTRL, wxID_DBCONNECTDIALOGREMEMBERCHECKBOX,
- wxID_DBCONNECTDIALOGSERVERNAMETEXTCTRL, wxID_DBCONNECTDIALOGSTATICTEXT1,
- wxID_DBCONNECTDIALOGSTATICTEXT2, wxID_DBCONNECTDIALOGSTATICTEXT3,
- wxID_DBCONNECTDIALOGSTATICTEXT4, wxID_DBCONNECTDIALOGSTATICTEXT6,
- wxID_DBCONNECTDIALOGSTATUSTEXTCTRL, wxID_DBCONNECTDIALOGUSERNAMETEXTCTRL,
- ] = [wx.NewId() for _init_ctrls in range(15)]
- class DBConnectDialog(wx.Dialog):
- def _init_ctrls(self, prnt):
- # generated method, don't edit
- wx.Dialog.__init__(self, id=wxID_DBCONNECTDIALOG, name='DBConnectDialog', parent=prnt,
- pos=wx.Point(44, 35), size=wx.Size(475, 373),
- style=wx.STAY_ON_TOP | wx.DIALOG_MODAL | wx.TAB_TRAVERSAL | wx.SUNKEN_BORDER | wx.DEFAULT_DIALOG_STYLE,
- title='Database Connection Dialog')
- self.SetClientSize(wx.Size(467, 346))
- self.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.NORMAL, False,'MS Shell Dlg 2'))
- self.SetThemeEnabled(True)
- self.Bind(wx.EVT_CLOSE, self.OnDBConnectDialogClose)
- self.Bind(wx.EVT_ACTIVATE, self.OnDBConnectDialogActivate)
- self.staticText1 = wx.StaticText(id=wxID_DBCONNECTDIALOGSTATICTEXT1, label='User Name',
- name='staticText1', parent=self, pos=wx.Point(16, 40), size=wx.Size(63, 16),
- style=0)
- self.staticText1.SetFont(wx.Font(10, wx.SWISS, wx.NORMAL, wx.NORMAL, False,
- 'MS Shell Dlg 2'))
- self.staticText2 = wx.StaticText(id=wxID_DBCONNECTDIALOGSTATICTEXT2, label='Password',
- name='staticText2', parent=self, pos=wx.Point(16, 88), size=wx.Size(55, 16),
- style=0)
- self.usernameTextCtrl = wx.TextCtrl(id=wxID_DBCONNECTDIALOGUSERNAMETEXTCTRL,
- name='usernameTextCtrl', parent=self, pos=wx.Point(16, 56), size=wx.Size(208, 24),
- style=0, value='')
- self.servernameTextCtrl = wx.TextCtrl(id=wxID_DBCONNECTDIALOGSERVERNAMETEXTCTRL,
- name='servernameTextCtrl', parent=self, pos=wx.Point(240, 56), size=wx.Size(208,
- 24), style=0, value='')
- self.staticText3 = wx.StaticText(id=wxID_DBCONNECTDIALOGSTATICTEXT3,
- label='Server Name or IP Address', name='staticText3', parent=self,
- pos=wx.Point(240, 40), size=wx.Size(156, 16), style=0)
- self.pwdTextCtrl = wx.TextCtrl(id=wxID_DBCONNECTDIALOGPWDTEXTCTRL, name='pwdTextCtrl',
- parent=self, pos=wx.Point(16, 104), size=wx.Size(168, 24), style=wx.TE_PASSWORD,
- value='')
- self.rememberCheckBox = wx.CheckBox(id=wxID_DBCONNECTDIALOGREMEMBERCHECKBOX,
- label='Remember Password', name='rememberCheckBox', parent=self, pos=wx.Point(16,
- 136), size=wx.Size(160, 16), style=0)
- self.rememberCheckBox.SetValue(True)
- self.staticText4 = wx.StaticText(id=wxID_DBCONNECTDIALOGSTATICTEXT4,
- label='Connection Status Log', name='staticText4', parent=self, pos=wx.Point(168,
- 160), size=wx.Size(127, 16), style=0)
- self.connectButton = wx.Button(id=wxID_DBCONNECTDIALOGCONNECTBUTTON, label='Connect',
- name='connectButton', parent=self, pos=wx.Point(200, 104), size=wx.Size(74, 26),
- style=0)
- self.connectButton.Bind(wx.EVT_BUTTON, self.OnConnectButton,
- id=wxID_DBCONNECTDIALOGCONNECTBUTTON)
- self.cancelButton = wx.Button(id=wxID_DBCONNECTDIALOGCANCELBUTTON, label='Cancel',
- name='cancelButton', parent=self, pos=wx.Point(376, 104), size=wx.Size(74, 26),
- style=0)
- self.cancelButton.Bind(wx.EVT_BUTTON, self.OnCancelButton,
- id=wxID_DBCONNECTDIALOGCANCELBUTTON)
- self.statusTextCtrl = wx.TextCtrl(id=wxID_DBCONNECTDIALOGSTATUSTEXTCTRL,
- name='statusTextCtrl', parent=self, pos=wx.Point(16, 176), size=wx.Size(432, 160),
- style=wx.TE_READONLY | wx.TE_MULTILINE, value='')
- self.statusTextCtrl.SetFont(wx.Font(8, wx.SWISS, wx.NORMAL, wx.NORMAL, False,
- 'MS Shell Dlg 2'))
- self.staticText6 = wx.StaticText(id=wxID_DBCONNECTDIALOGSTATICTEXT6, label='(NOT SECURE)',
- name='staticText6', parent=self, pos=wx.Point(16, 152), size=wx.Size(85, 16),
- style=0)
- self.OkButton = wx.Button(id=wxID_DBCONNECTDIALOGOKBUTTON, label='OK', name='OkButton',
- parent=self, pos=wx.Point(288, 104), size=wx.Size(75, 26), style=0)
- self.OkButton.Bind(wx.EVT_BUTTON, self.OnOkButton, id=wxID_DBCONNECTDIALOGOKBUTTON)
- self.promptStaticText = wx.StaticText(id=wxID_DBCONNECTDIALOGPROMPTSTATICTEXT,
- label='Change your database login:', name='promptStaticText', parent=self,
- pos=wx.Point(16, 8), size=wx.Size(242, 23), style=0)
- self.promptStaticText.SetFont(wx.Font(14, wx.SWISS, wx.NORMAL, wx.NORMAL, False,
- 'MS Shell Dlg 2'))
- def __init__(self, parent, defaultHolder):
- self._init_ctrls(parent)
- self.EntryDict = {'UserName':self.usernameTextCtrl,
- 'Password':self.pwdTextCtrl,
- 'ServerName':self.servernameTextCtrl}
- self.dbServer = db.DBServer(self) # the sever always has a disply.
- self.dbConnect = None
- self.defaultHolder = defaultHolder
- self.SetWidgetData()
- def InitLogin(self):
- user, password, host = self.GetWidgetData()
- self.dbConnect = self.dbServer.Login(host, user, password)
- def GetDBServer(self): # Creator calls here to get the server (may not have a connection)
- return self.dbServer
- def IsConnected(self): # Creator calls here to get the status of the connection
- return self.dbServer.IsOpen(self.dbConnect)
- def SetWidgetData(self):
- for var in self.defaultHolder.GetVariables():
- try:
- self.EntryDict[var['name']].SetValue(var['value'])
- except KeyError:
- pass
- def SetPromptText(self, text):
- self.promptStaticText.SetLabel(text)
- def GetWidgetData(self):
- user = self.usernameTextCtrl.GetValue()
- password = self.pwdTextCtrl.GetValue()
- host = self.servernameTextCtrl.GetValue()
- return user, password, host
- def write(self, message):
- """Requires a newline at the end of the string."""
- self.statusTextCtrl.AppendText(message)
- def ConnectToDB(self, user, password, host, database=""):
- """The dbConnect retains the state returned by the server login()"""
- self.dbConnect = self.dbServer.Login(host, user, password)
- if self.dbConnect:
- if database:
- self.dbServer.Execute("CREATE DATABASE IF NOT EXISTS %s" % database)
- self.dbServer.Execute("USE %s" % database)
- self.OkButton.Enable()
- self.OkButton.SetDefault()
- return True
- else:
- wx.Bell()
- self.OkButton.Disable()
- self.usernameTextCtrl.SetSelection(-1, -1)
- self.usernameTextCtrl.SetFocus()
- def OnConnectButton(self, event):
- if self.dbServer.IsOpen(self.dbConnect):
- self.dbConnect.close()
- user, password, host = self.GetWidgetData()
- if self.ConnectToDB(user, password, host):
- self.defaultHolder.SetVariables(UserName=user, Password=password,
- ServerName=host)
- self.defaultHolder.UpdateConfig()
- def OnCancelButton(self, event):
- self.EndModal(wx.ID_CANCEL)
- def OnOkButton(self, event):
- self.EndModal(wx.ID_OK)
- def OnDBConnectDialogClose(self, event):
- self.EndModal(wx.ID_CLOSE)
- def OnDBConnectDialogActivate(self, event):
- """Set the look to be just right on opening."""
- if event.GetActive():
- self.OkButton.Disable()
- self.connectButton.SetDefault()
- pos = self.statusTextCtrl.GetLastPosition()
- # self.statusTextCtrl.SetInsertionPoint(pos)
- self.statusTextCtrl.ShowPosition(pos)
- self.usernameTextCtrl.SetSelection(-1, -1)
- self.usernameTextCtrl.SetFocus()
- event.Skip()
- if __name__ == "__main__":
- import DefaultHolder as DH
- app = wx.PySimpleApp()
- answer = wx.ID_OK
- dbDefaultHolder = DH.DefaultValueHolder("HETAP Pro 2.00", "Setup")
- dbDefaultHolder.SetVariables(UserName="barton", Password="1coolSQL",
- ServerName="genesis")
- dbDefaultHolder.InitFromConfig()
- frame = create(None, dbDefaultHolder)
- ## frame.ShowModal()
- while answer == wx.ID_OK:
- frame.ShowModal()
- answer = wx.MessageBox("Do it again? ", "go again?", wx.OK| wx.CANCEL |wx.ICON_QUESTION, frame)
- print answer, wx.ID_CANCEL
- ## if answer == wx.ID_CANCEL:
- ## break
- frame.Destroy()
- app.MainLoop()