37
37
EVENT_SAVE_DB = 11
38
38
EVENT_EDIT_PROTECT_DB = 12
39
39
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
40
46
41
47
# Hack for fixing false table events that are generated when the
42
48
# table.update() method is called. Call this after each call to update()!
@@ -624,7 +630,7 @@ def insert_record(self, field='', value=''):
624
630
self .db .update_elements ()
625
631
self .db .window .refresh ()
626
632
627
- def save_record (self , message = None , update_elements = True ):
633
+ def save_record (self , display_message = True , update_elements = True ):
628
634
"""
629
635
Save the currently selected record
630
636
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):
634
640
"""
635
641
# Ensure that there is actually something to save
636
642
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
638
645
639
646
640
647
# callback
641
648
if 'before_save' in self .callbacks .keys ():
642
649
if self .callbacks ['before_save' ]()== False :
643
650
logger .info ("We are not saving!" )
644
651
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
646
654
647
655
values = []
648
656
# We are updating a record
@@ -674,25 +682,25 @@ def save_record(self, message=None, update_elements=True):
674
682
if 'after_save' in self .callbacks .keys ():
675
683
if not self .callbacks ['after_save' ](self .db , self .db .window ):
676
684
self .con .rollback ()
677
- else :
678
- self .con .commit ()
679
- else :
680
- self .con .commit ()
685
+ return SAVE_FAIL
681
686
682
- # Lets refresh our data
687
+ # If we ,ade it here, we can commit the changes
688
+ self .con .commit ()
683
689
690
+ # Lets refresh our data
684
691
pk = self .get_current_pk ()
685
692
self .requery (update_elements )
686
693
self .set_by_pk (pk ,update_elements ,False )
687
694
#self.requery_dependents()
688
- if update_elements :
689
- self .db .update_elements (self .table )
695
+ if update_elements :self .db .update_elements (self .table )
690
696
logger .info (f'Record Saved!' )
697
+ if display_message : sg .popup ('Updates saved successfully!' )
698
+ return SAVE_SUCCESS
691
699
else :
692
700
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
+
696
704
def delete_record (self , cascade = True ):
697
705
"""
698
706
Delete the currently selected record
@@ -877,13 +885,13 @@ def __init__(self, db_path=None, win=None, sql_script=None, sqlite3_database=Non
877
885
self .auto_bind (win )
878
886
879
887
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
886
889
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
887
895
self .con .close ()
888
896
889
897
# Override the [] operator to retrieve queries by key
@@ -1243,18 +1251,30 @@ def save_records(self, cascade_only=False):
1243
1251
last_index = len (self .tables ) - 1
1244
1252
1245
1253
successes = 0
1254
+ failures = 0
1255
+ no_actions = 0
1246
1256
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?
1253
1257
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 :
1255
1262
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
+
1257
1275
self .update_elements ()
1276
+
1277
+
1258
1278
def update_elements (self , table = '' ): # table type: str
1259
1279
# TODO Fix bug where listbox first element is ghost selected
1260
1280
# TODO: Dosctring
0 commit comments