Skip to content

Commit 9268d89

Browse files
committed
More work on improving feedback from record saves
1 parent 7712f3f commit 9268d89

File tree

2 files changed

+49
-28
lines changed

2 files changed

+49
-28
lines changed

PySimpleSQL.py

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@
3737
EVENT_SAVE_DB=11
3838
EVENT_EDIT_PROTECT_DB=12
3939

40+
# ------------------------
41+
# RECORD SAVE RETURN TYPES
42+
# ------------------------
43+
SAVE_FAIL=0 # Save failed due to callback
44+
SAVE_SUCCESS=1 # Save was successful
45+
SAVE_NONE=2 # There was nothing to save
4046

4147
# Hack for fixing false table events that are generated when the
4248
# table.update() method is called. Call this after each call to update()!
@@ -624,7 +630,7 @@ def insert_record(self, field='', value=''):
624630
self.db.update_elements()
625631
self.db.window.refresh()
626632

627-
def save_record(self, message=None, update_elements=True):
633+
def save_record(self, display_message=True, update_elements=True):
628634
"""
629635
Save the currently selected record
630636
Saves any changes made via the GUI back to the database. The before_save and after_save @callbacks will call
@@ -634,15 +640,17 @@ def save_record(self, message=None, update_elements=True):
634640
"""
635641
# Ensure that there is actually something to save
636642
if not len(self.rows):
637-
return False
643+
if display_message: sg.popup('There were no updates to save.',keep_on_top=True)
644+
return SAVE_NONE
638645

639646

640647
# callback
641648
if 'before_save' in self.callbacks.keys():
642649
if self.callbacks['before_save']()==False:
643650
logger.info("We are not saving!")
644651
if update_elements: self.db.update_elements(self.table)
645-
return False
652+
if display_message: sg.popup('Updates not saved.', keep_on_top=True)
653+
return SAVE_FAIL
646654

647655
values = []
648656
# We are updating a record
@@ -674,25 +682,25 @@ def save_record(self, message=None, update_elements=True):
674682
if 'after_save' in self.callbacks.keys():
675683
if not self.callbacks['after_save'](self.db, self.db.window):
676684
self.con.rollback()
677-
else:
678-
self.con.commit()
679-
else:
680-
self.con.commit()
685+
return SAVE_FAIL
681686

682-
# Lets refresh our data
687+
# If we ,ade it here, we can commit the changes
688+
self.con.commit()
683689

690+
# Lets refresh our data
684691
pk = self.get_current_pk()
685692
self.requery(update_elements)
686693
self.set_by_pk(pk,update_elements,False)
687694
#self.requery_dependents()
688-
if update_elements:
689-
self.db.update_elements(self.table)
695+
if update_elements:self.db.update_elements(self.table)
690696
logger.info(f'Record Saved!')
697+
if display_message: sg.popup('Updates saved successfully!')
698+
return SAVE_SUCCESS
691699
else:
692700
logger.info('Nothing to save.')
693-
if message is not None:
694-
sg.popup(message, keep_on_top=True)
695-
return True
701+
if display_message: sg.popup('There were no updates to save!')
702+
return SAVE_NONE
703+
696704
def delete_record(self, cascade=True):
697705
"""
698706
Delete the currently selected record
@@ -877,13 +885,13 @@ def __init__(self, db_path=None, win=None, sql_script=None, sqlite3_database=Non
877885
self.auto_bind(win)
878886

879887
def __del__(self):
880-
# optimize the database for long-term benefits
881-
if self.path != ':memory:':
882-
q = 'PRAGMA optimize;'
883-
self.con.execute(q)
884-
885-
# Close the connection
888+
# Only do cleanup if this is not an imported database
886889
if not self.imported_database:
890+
# optimize the database for long-term benefits
891+
if self.path != ':memory:':
892+
q = 'PRAGMA optimize;'
893+
self.con.execute(q)
894+
# Close the connection
887895
self.con.close()
888896

889897
# Override the [] operator to retrieve queries by key
@@ -1243,18 +1251,30 @@ def save_records(self, cascade_only=False):
12431251
last_index = len(self.tables) - 1
12441252

12451253
successes=0
1254+
failures=0
1255+
no_actions=0
12461256
for t in tables:
1247-
if i == last_index:
1248-
if i==successes:
1249-
msg='Updates saved successfully!'
1250-
else:
1251-
msg=None
1252-
# todo: roll back changes?
12531257
logger.info(f'Saving records for table {t}...')
1254-
if self[t].save_record(msg,update_elements=False)==True:
1258+
result=self[t].save_record(False,update_elements=False)
1259+
if result==SAVE_FAIL:
1260+
failures+=1
1261+
elif result==SAVE_SUCCESS:
12551262
successes+=1
1256-
i += 1
1263+
elif result==SAVE_NONE:
1264+
no_actions+=1
1265+
logger.debug(f'Successes: {successes}, Failures: {failures}, No Actions: {no_actions}')
1266+
1267+
if failures==0:
1268+
if successes==0:
1269+
sg.popup('There was nothing to update.', keep_on_top=True)
1270+
else:
1271+
sg.popup('Updates saved successfully!',keep_on_top=True)
1272+
else:
1273+
sg.popup('There was a problem saving some updates.', keep_on_top=True)
1274+
12571275
self.update_elements()
1276+
1277+
12581278
def update_elements(self, table=''): # table type: str
12591279
# TODO Fix bug where listbox first element is ghost selected
12601280
# TODO: Dosctring

examples/address_book.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
# Zip code validation
88
def validate_zip():
9-
if len(win['Addresses.zip'].get())!=5:
9+
zip=win['Addresses.zip'].get()
10+
if len(zip)!=5:
1011
sg.popup('Check your zip code and try again!',title="Zip code validation failed!")
1112
return False
1213
return True

0 commit comments

Comments
 (0)