def __init__(self): self.model = Model() self.main_window = MainWindow() self.main_window.Bind(wx.EVT_CLOSE, self.main_window_exit) self.main_window.Bind(wx.EVT_MENU, self.main_window_add_lib_item, self.main_window.m_add_lib_item) self.main_window.Bind(wx.EVT_MENU, self.main_window_add_patron, self.main_window.m_add_patron) self.main_window.Bind(wx.EVT_MENU, self.main_window_exit, self.main_window.m_exit) self.main_window.Bind(wx.EVT_MENU, self.main_window_about, self.main_window.m_about) self.main_window.ctx.library.list_view.Bind(wx.EVT_KEY_DOWN, self.ctx_library_delete_item) self.main_window.ctx.patrons.list_view.Bind(wx.EVT_KEY_DOWN, self.ctx_patrons_delete_item) self.update_context_views() self.main_window.Show()
class Controller: def __init__(self): self.model = Model() self.main_window = MainWindow() self.main_window.Bind(wx.EVT_CLOSE, self.main_window_exit) self.main_window.Bind(wx.EVT_MENU, self.main_window_add_lib_item, self.main_window.m_add_lib_item) self.main_window.Bind(wx.EVT_MENU, self.main_window_add_patron, self.main_window.m_add_patron) self.main_window.Bind(wx.EVT_MENU, self.main_window_exit, self.main_window.m_exit) self.main_window.Bind(wx.EVT_MENU, self.main_window_about, self.main_window.m_about) self.main_window.ctx.library.list_view.Bind(wx.EVT_KEY_DOWN, self.ctx_library_delete_item) self.main_window.ctx.patrons.list_view.Bind(wx.EVT_KEY_DOWN, self.ctx_patrons_delete_item) self.update_context_views() self.main_window.Show() def main_window_exit(self, evt): self.main_window.terminate() def main_window_add_lib_item(self, evt): patron_data = self.model.get_all_patrons() self.add_lib_item_dlg = LibraryItem(self.main_window, patron_data) self.add_lib_item_dlg.Bind(wx.EVT_BUTTON, self.add_lib_item_ok, self.add_lib_item_dlg.ok) self.add_lib_item_dlg.Bind(wx.EVT_BUTTON, self.add_lib_item_cancel, self.add_lib_item_dlg.cancel) self.add_lib_item_dlg.Show() def main_window_add_patron(self, evt): self.add_patron_dlg = Patron(self.main_window) self.add_patron_dlg.Bind(wx.EVT_BUTTON, self.add_patron_ok, self.add_patron_dlg.ok) self.add_patron_dlg.Bind(wx.EVT_BUTTON, self.add_patron_cancel, self.add_patron_dlg.cancel) self.add_patron_dlg.Show() def main_window_about(self, evt): self.main_window.show_about() def add_lib_item_ok(self, evt): if ( self.add_lib_item_dlg.name.IsEmpty() or self.add_lib_item_dlg.author_first_name.IsEmpty() or self.add_lib_item_dlg.author_last_name.IsEmpty() or self.add_lib_item_dlg.desc.IsEmpty() or self.add_lib_item_dlg.type.IsEmpty() or self.add_lib_item_dlg.checked_out_by.GetValue() == "" ): self.add_lib_item_dlg.report_missing_fields() return item_data = { "name": self.add_lib_item_dlg.name.GetValue(), "author_first": self.add_lib_item_dlg.author_first_name.GetValue(), "author_last": self.add_lib_item_dlg.author_last_name.GetValue(), "desc": self.add_lib_item_dlg.desc.GetValue(), "type": self.add_lib_item_dlg.type.GetValue(), "cob": self.add_lib_item_dlg.checked_out_by.GetValue(), "id": self.model.last_rowid(), } if item_data["cob"] != "None": dt = date.today() item_data["date_out"] = "%s/%s/%s" % (dt.month, dt.day, dt.year) item_data["date_due"] = "%s/%s/%s" % (dt.month, dt.day + 5, dt.year) else: item_data["date_out"] = "N/A" item_data["date_due"] = "N/A" self.model.add_lib_item(item_data) item_data["id"] = self.model.last_rowid() self.main_window.ctx.library.add_entry(item_data) self.add_lib_item_dlg.Destroy() def add_lib_item_cancel(self, evt): self.add_lib_item_dlg.Destroy() def add_patron_ok(self, evt): if self.add_patron_dlg.first_name.IsEmpty() or self.add_patron_dlg.last_name.IsEmpty(): self.add_patron_dlg.report_missing_fields() return email = self.add_patron_dlg.email.GetValue() if not match("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$", email): self.add_patron_dlg.report_bad_email(email) return phone = self.add_patron_dlg.phone.GetValue() if not match( "^(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})(?:\s*(?:#|x\.?|ext\.?|extension)\s*(\d+))?$", phone, ): self.add_patron_dlg.report_bad_phone(phone) return patron_data = { "first_name": self.add_patron_dlg.first_name.GetValue(), "last_name": self.add_patron_dlg.last_name.GetValue(), "phone": self.add_patron_dlg.phone.GetValue(), "email": self.add_patron_dlg.email.GetValue(), "city": self.add_patron_dlg.city.GetValue(), "state": self.add_patron_dlg.state.GetValue(), "street_addr": self.add_patron_dlg.street.GetValue(), "zip": self.add_patron_dlg.zip_code.GetValue(), } self.model.add_patron(patron_data) patron_data["id"] = self.model.last_rowid() self.main_window.ctx.patrons.add_entry(patron_data) self.add_patron_dlg.Destroy() def add_patron_cancel(self, evt): self.add_patron_dlg.Destroy() def update_context_views(self): lib_items = self.model.get_all_lib_items() patrons = self.model.get_all_patrons() for table in patrons: self.main_window.ctx.patrons.add_entry(table) for table in lib_items: self.main_window.ctx.library.add_entry(table) def ctx_library_delete_item(self, evt): if evt.GetKeyCode() == wx.WXK_DELETE: item = self.main_window.ctx.library.list_view.GetFocusedItem() if item == -1: evt.Skip() return name = self.main_window.ctx.library.list_view.GetItemText(item) if self.main_window.ctx.library.confirm_deletion(name) == wx.ID_YES: self.model.del_lib_item(item) self.main_window.ctx.library.list_view.DeleteItem(item) return evt.Skip() else: evt.Skip() def ctx_patrons_delete_item(self, evt): if evt.GetKeyCode() == wx.WXK_DELETE: item = self.main_window.ctx.patrons.list_view.GetFocusedItem() if item == -1: evt.Skip() return fn = self.main_window.ctx.patrons.list_view.GetItem(item, 0).GetText() ln = self.main_window.ctx.patrons.list_view.GetItem(item, 1).GetText() name = "%s %s" % (fn, ln) if self.main_window.ctx.patrons.confirm_deletion(name) == wx.ID_YES: self.model.del_patron(item) self.main_window.ctx.patrons.list_view.DeleteItem(item) return evt.Skip() else: evt.Skip()