-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnatarea.py
310 lines (261 loc) · 10.4 KB
/
natarea.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
# natarea.py
# written by: Thomas A. Grate
# copyright (c) 2017 by Thomas A. Grate, all rights reserved.
#
# for OYD Daily program
# class NatAreas_Table - used to query lists of national areas
# Note: the schema for the NatAreas_Table() is defined in the NatArea() object
class NatAreas_Table(object):
def __init__(self):
self.areas = []
self.limit = None
self.offset = None
# Exectue the SQL query to get Courses from the Database
def _sql_query (self, c, limit=None, offset=None):
# add limit and offset if presented to query
limoff = ''
if self.limit:
limoff = ' limit ' + str(limit)
if self.offset:
limoff += ' offset ' + str(offset)
# execute the query for selected Courses in the database
c.execute('SELECT * FROM areas' + limoff)
row = c.fetchone()
# check if a row was returned
if row:
# store all rows in the list
while row:
self.areas.append(row)
row = c.fetchone()
# return success
return 0
else:
# return ERROR
return 1
# Count
def count(self, db):
""" NatAreas_Table.count() function
Returns number of rows in areas table
Parameters:
db - Database() object
Returns:
number of rows in courses table"""
try:
c = db.cursor
# execute the query for all users in the database
c.execute('SELECT count(*) FROM areas')
row = c.fetchone()
# check if result otherwise return None
if row:
return row[0]
else:
return None
except:
return None
# Query for a range of Students in the Database
def query_range(self, db, limit, offset):
self.areas = []
self.limit = limit
self.offset = offset
return self._sql_query (c=db.cursor, limit=limit, offset=offset)
# Query for All National Areas in the Database
def query_all(self, db):
self.areas = []
self.limit = None
self.offset = None
return self._sql_query (c = db.cursor)
# class NatArea - used to manage available OYD National Areas
# includes the Schema for the National Areas Table - areas
class NatArea (object):
def __init__(self, area_name=None, area_abbrev=None):
"""NatArea Object: __init__ Method
Parameters: (all default to None)
area_name - national area name
area_abbrev - abbreviated name of national area"""
# Dictionary of School Attributes
self.attrs = {'nat_area_id': None, # internal use only, do not change
'area_name': area_name,
'area_abbrev': area_abbrev
}
# Matching dictionary of UI Elements for NatArea Attributes
self.ui2 = {
0: {'item': 'area_name',
'label':'Area Name',
'edit_name':'editAreaName',
'edit_type':'text',
'placeholder':'Area Name',
'select_options': None},
1: {'item': 'area_abbrev',
'label':'Area Abbreviation',
'edit_name':'editAreaAbbrev',
'edit_type':'text',
'placeholder':'Abbreviation',
'select_options':None}
}
# SQL Schema
# Must match the attrs (attributes) above, line for line
self.schema = ['nat_area_id',
'area_name',
'area_abbrev'
]
# create the INSERT schema substituion string
self.schema_insert = ", ".join(self.schema)
# SQL Data Types for the SQL Schema
# Must match the SQL Schema above, line for line
self.types = ['integer primary key',
'text',
'text'
]
# make the CREATE schema substituion string
# used to create the student table
self.schema_create = ''
limit = len(self.schema) - 1
i = 0
for i in range(0, limit):
addstr = self.schema[i] + ' ' + self.types[i] + ', '
self.schema_create += addstr
self.schema_create += self.schema[limit] + ' ' + self.types[limit]
# VALUE substitution string
self.schema_insert_sub = '(' + '?,' * (len(self.schema) - 1) + '?)'
# Method to get a tuuple of all school data
def _get (self):
""" NatArea Object: _get method (private)
Returns a tuple of the NatArea data
Used by sql_insert """
# assuming that dictiionaries are unordered
# retrive the data in oder as a tuple
results = []
for key in self.schema:
results.append(self.attrs[key])
return tuple(results)
# Method to set all NatArea data from a SQL row tuple
def _set (self, sql_data):
""" NatArea Object: _set method (private)
Sets NatArea data in the instance of the object.
Used to set data from a sql query into the object.
Parameters:
sql_data - a 'row' tuple returned from a sql query for a National Area"""
# copy the sql_data, a row, to self.attrs dictionary
# use self.schema instead of self.attrs.keys() to interate
# becaause the self.schema is a list and the order will not change
i = 0
for key in self.schema:
self.attrs[key] = sql_data[i]
i += 1
def _sql_populate (self, c):
""" NatArea Object: _sql_populate method (private)
Populates the instance of NatArea from the database as a new row
Parameters:
c = cursor to database"""
try:
# Test 1, check for the SQL ID
if self.attrs['nat_area_id']:
test = (self.attrs['nat_area_id'], )
c.execute('SELECT * FROM areas WHERE nat_area_id=?', test)
# Test 2, check for NatArea Name
elif self.attrs['area_name']:
test = (self.attrs['area_name'], )
c.execute('SELECT * FROM areas WHERE area_name=?', test)
else:
# if you did't fill in any information to test, why did you call populate?
return 1
except Exception as e:
print (f"ERROR: NatArea()._sql_populate: {e}")
return 1 # return Error
# check if a row is returned
row = c.fetchone()
if row:
self._set(row)
return 0
else:
return 1
def _sql_insert (self, conn, c):
"""NatArea Object: _sql_insert method (private)
Inserts the instance of NatArea into the database as a new row
Parameters:
conn = connection to database
c = cursor to database"""
try:
c.execute('INSERT INTO areas (' + self.schema_insert + \
') VALUES ' + self.schema_insert_sub, self._get())
except:
# Insert failed so return Error
return (1, 'Failed to add New Area!')
# Save (commit) the changes
conn.commit()
# sql_id is auto assigned on insert. So, retrive the sql_id from the db
name = (self.attrs['area_name'], )
c.execute('SELECT nat_area_id FROM areas WHERE area_name=?', name)
row = c.fetchone()
self.attrs['nat_area_id'] = row[0]
return (0, 'Area Successfully Added!')
def _sql_update (self, conn, c):
"""NatArea Object: _sql_update method (private)
Commits all attribute in the instance of NatArea
to the associated existing row in the database
Parameters:
conn = connection to database
c = cursor to database"""
if self.attrs['nat_area_id'] is not None:
try:
# create the label = value string to UPDATE
lvl = ''
for label in self.schema:
lvl += label + ' = "' + str(self.attrs[label]) + '", '
lvl = lvl [:-2]
# update the row
c.execute('UPDATE areas SET ' + lvl + \
' WHERE nat_area_id=' + str(self.attrs['nat_area_id']))
# Save (commit) the changes
conn.commit()
return 0 # return Success
except Exception as e:
print (f"ERROR: NatArea()._sql_update: {e}")
return 1 # return Error
else:
print ("ERROR: NatArea()._sql_update: sql_id not yet set")
return 1 # return Error
def _sql_delete (self, conn, c):
"""NatArea Object: _sql_delete method (private)
Deletes the row in the database assoicated with the
instance of School
Parameters:
conn = connection to database
c = cursor to database"""
# ??? Handle Exceptions Here ???
if self.attrs['nat_area_id'] is not None:
try:
# delete the row
c.execute('DELETE FROM areas WHERE nat_area_id=' + \
str(self.attrs['nat_area_id']))
# Save (commit) the changes
conn.commit()
return 0 # return Success
except Exception as e:
print (f"ERROR: NatArea()._sql_delete: {e}")
return 1 # return Error
else:
print ("ERROR: NatArea()._sql_delete: sql_id not yet set")
return 1 # return Error
def get (self, db):
""" NatArea Object: get method
Populates the instance of NatArea from the database as a new row
Parameters:
db = Database object from which to retrive the cursor"""
return self._sql_populate (db.cursor)
def put (self, db):
"""NatArea Object: put method
Inserts the instance of NatArea into the database as a new row
Parameters:
db = Database object that contains the
connection & cursor to database"""
return self._sql_insert(db.conn, db.cursor)
# commit the data in NatArea to the DB
def update (self, db):
"""Nat Are aObject: update method
Commits all attributes in the instance of NatArea
to the associated existing row in the database
Parameters:
db = Database object that contains the
connection & cursor to database"""
return self._sql_update(db.conn, db.cursor)