/
addModRecord.py
167 lines (137 loc) · 5.7 KB
/
addModRecord.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
"""
AUTHOR : Robert James Patterson
DATE : 06/03/19
SYNOPSIS : Work thru files for the 'Mouse vs. Python' MVC/CRUD tutorial. This is the dialog
that will allow the end user to add and modify records
"""
import commonDlgs
import controller
import wx
class AddModRecDialog(wx.Dialog):
"""
This is the dialog used to Add / Modify records.
"""
def __init__(self, row=None, title="Add", addRecord=True):
wx.Dialog.__init__(self, None, title="%s Record" % title)
self.addRecord = addRecord
self.selectedRow = row
# Determin if we are creating the dialog to add a new record or if it is to modify an
# exisiting record by seeing if a data record was passed into the class or not.
if row:
bTitle = self.selectedRow.title
fName = self.selectedRow.first_name
lName = self.selectedRow.last_name
isbn = self.selectedRow.isbn
publisher = self.selectedRow.publisher
else:
bTitle = fName = lName = isbn = publisher = ""
# Setup the wx dialog
size = (80, -1)
font = wx.Font(18, wx.SWISS, wx.NORMAL, wx.BOLD)
# Create the sizers
mainSizer = wx.BoxSizer(wx.VERTICAL)
authorSizer = wx.BoxSizer(wx.HORIZONTAL)
btnSizer = wx.BoxSizer(wx.HORIZONTAL)
# Create the dialog header
lblNewRec = wx.StaticText(self, label=('%s' % title))
lblNewRec.SetFont(font)
mainSizer.Add(lblNewRec, 0, wx.CENTER)
font = wx.Font(12, wx.SWISS, wx.NORMAL, wx.BOLD)
lblTitle = wx.StaticText(self, label="Title:", size=size)
lblTitle.SetFont(font)
self.txtTitle = wx.TextCtrl(self, value=bTitle)
mainSizer.Add(self.rowBuilder([lblTitle, self.txtTitle]), 0, wx.EXPAND)
lblAuthor = wx.StaticText(self, label="Author:", size=size)
lblAuthor.SetFont(font)
authorSizer.Add(lblAuthor, 0, wx.ALL, 5)
self.txtFirstName = wx.TextCtrl(self, value=fName)
authorSizer.Add(self.txtFirstName, 1, wx.EXPAND|wx.ALL, 5)
self.txtLastName = wx.TextCtrl(self, value=lName)
authorSizer.Add(self.txtLastName, 1, wx.EXPAND|wx.ALL, 5)
mainSizer.Add(authorSizer, 0, wx.EXPAND)
lblIsbn = wx.StaticText(self, label="ISBN:", size=size)
lblIsbn.SetFont(font)
self.txtIsbn = wx.TextCtrl(self, value=isbn)
mainSizer.Add(self.rowBuilder([lblIsbn, self.txtIsbn]), 0, wx.EXPAND)
lblPublisher = wx.StaticText(self, label="Publisher:", size=size)
lblPublisher.SetFont(font)
self.txtPublisher = wx.TextCtrl(self, value=publisher)
mainSizer.Add(self.rowBuilder([lblPublisher, self.txtPublisher]), 0, wx.EXPAND)
btnOk = wx.Button(self, label="%s" % title)
btnOk.Bind(wx.EVT_BUTTON, self.onRecord)
btnSizer.Add(btnOk, 0, wx.ALL, 5)
btnCancel = wx.Button(self, label="Close")
btnCancel.Bind(wx.EVT_BUTTON, self.onClose)
btnSizer.Add(btnCancel, 0, wx.ALL, 5)
mainSizer.Add(btnSizer, 0, wx.CENTER)
self.SetSizer(mainSizer)
mainSizer.Fit(self)
def getData(self):
"""
Method to get the values from the text boxes and store them in dictionay objects.
"""
dictAuthor = {}
dictBook = {}
fName = self.txtFirstName.GetValue()
lName = self.txtLastName.GetValue()
title = self.txtTitle.GetValue()
isbn = self.txtIsbn.GetValue()
publisher = self.txtPublisher.GetValue()
if fName == "" or title == "":
commonDlgs.showMessageDlg("Author and Title are required!", "Error!")
return
if "-" in isbn:
isbn = isbn.replace("-", "_")
dictAuthor['first_name'] = fName
dictAuthor['last_name'] = lName
dictBook['title'] = title
dictBook['isbn'] = isbn
dictBook['publisher'] = publisher
return dictAuthor, dictBook
def onAdd(self):
"""
Method to add a record to the database.
"""
# Get the text input fields from the form.
dictAuthor, dictBook = self.getData()
# Create a dictonary of dictonaries.
data = ({'author': dictAuthor, 'book': dictBook})
controller.addRecord(data)
# display a dialog to notify the end user that the record has been added.
commonDlgs.showMessageDlg("Book Added!", "Success!", wx.ICON_INFORMATION)
# clear the text fields to add another record.
for child in self.GetChildren():
if isinstance(child, wx.TextCtrl):
child.SetValue("")
def onEdit(self):
"""
Method to edit data from the database
"""
# Get the text input fields from the form.
dictAuthor, dictBook = self.getData()
# Create a dictonary of dictonaries.
data = ({'author': dictAuthor, 'book': dictBook})
controller.editRecord(self.selectedRow.id, data)
# display a dialog to notify the end user that the record has been updated.
commonDlgs.showMessageDlg("Edited Successfully!", "Success!", wx.ICON_INFORMATION)
self.Destroy()
def onRecord(self, event):
if self.addRecord:
self.onAdd()
else:
self.onEdit()
self.txtTitle.SetFocus()
def onClose(self, event):
"""
Cancel the dialog.
"""
self.Destroy()
def rowBuilder(self, widgets):
"""
Utility to build label/textbox rows to be added to the sizer
"""
sizer = wx.BoxSizer(wx.HORIZONTAL)
lbl, txt = widgets
sizer.Add(lbl, 0, wx.ALL, 5)
sizer.Add(txt, 1, wx.EXPAND|wx.ALL, 5)
return sizer