Skip to content

Commit cfe4ce9

Browse files
committed
reference #74 and adds bitmasking for some of the return values
1 parent 992a85e commit cfe4ce9

File tree

1 file changed

+53
-54
lines changed

1 file changed

+53
-54
lines changed

pysimplesql/pysimplesql.py

+53-54
Original file line numberDiff line numberDiff line change
@@ -60,27 +60,33 @@
6060
EVENT_SAVE_DB=11
6161
EVENT_EDIT_PROTECT_DB=12
6262

63-
# --------------------
64-
# PROMPT_SAVE Returns
65-
# --------------------
66-
PROMPT_DISCARDED = 0
67-
PROMPT_PROCEED = 1
68-
PROMPT_NONE = 2
69-
70-
# ------------------------
71-
# RECORD SAVE RETURN TYPES
72-
# ------------------------
73-
SAVE_FAIL = 0 # Save failed due to callback
74-
SAVE_SUCCESS = 1 # Save was successful
75-
SAVE_NONE =2 # There was nothing to save
76-
77-
# --------------------
78-
# SEARCH RETURN VALUES
79-
# --------------------
80-
SEARCH_FAILED = 0 # No result was found
81-
SEARCH_RETURNED = 1 # A result was found
82-
SEARCH_ABORTED = 2 # The search was aborted, likely during a callback
83-
SEARCH_ENDED = 3 # We have reached the end of the search
63+
# ----------------
64+
# GENERIC BITMASKS
65+
# ----------------
66+
# Can be used with other bitmask values
67+
SHOW_MESSAGE = 4096
68+
69+
# ---------------------------
70+
# PROMPT_SAVE RETURN BITMASKS
71+
# ---------------------------
72+
PROMPT_SAVE_DISCARDED = 1
73+
PROMPT_SAVE_PROCEED = 2
74+
PROMPT_SAVE_NONE = 4
75+
76+
# ---------------------------
77+
# RECORD SAVE RETURN BITMASKS
78+
# ---------------------------
79+
SAVE_FAIL = 1 # Save failed due to callback
80+
SAVE_SUCCESS = 2 # Save was successful
81+
SAVE_NONE = 4 # There was nothing to save
82+
83+
# ----------------------
84+
# SEARCH RETURN BITMASKS
85+
# ----------------------
86+
SEARCH_FAILED = 1 # No result was found
87+
SEARCH_RETURNED = 2 # A result was found
88+
SEARCH_ABORTED = 4 # The search was aborted, likely during a callback
89+
SEARCH_ENDED = 8 # We have reached the end of the search
8490

8591

8692
def strip(string:str) -> str:
@@ -535,7 +541,7 @@ def records_changed(self, recursive=True, column_name:str=None) -> bool:
535541
return dirty
536542

537543

538-
def prompt_save(self, autosave=False) -> Union[PROMPT_PROCEED, PROMPT_DISCARDED, PROMPT_NONE]:
544+
def prompt_save(self, autosave=False) -> Union[PROMPT_SAVE_PROCEED, PROMPT_SAVE_DISCARDED, PROMPT_SAVE_NONE]:
539545
"""
540546
Prompts the user if they want to save when changes are detected and the current record is about to change.
541547
:param autosave: True to autosave when changes are found without prompting the user
@@ -546,7 +552,7 @@ def prompt_save(self, autosave=False) -> Union[PROMPT_PROCEED, PROMPT_DISCARDED,
546552
# Return False if there is nothing to check or _prompt_save is False
547553
# TODO: children too?
548554
if self.current_index is None or self.rows == [] or self._prompt_save is False:
549-
return PROMPT_NONE
555+
return PROMPT_SAVE_NONE
550556

551557
# Check if any records have changed
552558
changed = self.records_changed()
@@ -558,12 +564,12 @@ def prompt_save(self, autosave=False) -> Union[PROMPT_PROCEED, PROMPT_DISCARDED,
558564
if save_changes == 'Yes':
559565
# save this record
560566
self.save_record_recursive()
561-
return PROMPT_PROCEED
567+
return PROMPT_SAVE_PROCEED
562568
else:
563569
self.rows.purge_virtual()
564-
return PROMPT_DISCARDED
570+
return PROMPT_SAVE_DISCARDED
565571
else:
566-
return PROMPT_NONE
572+
return PROMPT_SAVE_NONE
567573

568574

569575
def requery(self, select_first=True, filtered=True, update=True, dependents=True):
@@ -940,7 +946,7 @@ def save_record(self, display_message=True, update_elements=True):
940946

941947
if not self.records_changed():
942948
if display_message: sg.popup_quick_message('There were no changes to save!', keep_on_top=True)
943-
return SAVE_NONE
949+
return SAVE_NONE + SHOW_MESSAGE
944950

945951
# check to see if cascading-fk has changed before we update database
946952
fk_changed = False
@@ -955,13 +961,13 @@ def save_record(self, display_message=True, update_elements=True):
955961
if self.transform is not None: self.transform(changed, TFORM_ENCODE)
956962

957963
if not self.driver.save_record(self.table,self.get_current_pk(),self.pk_column,changed):
958-
return SAVE_FAIL
964+
return SAVE_FAIL # Do not show the message in this case, since it's handled in the driver
959965

960966
# callback
961967
if 'after_save' in self.callbacks.keys():
962968
if not self.callbacks['after_save'](self.frm, self.frm.window):
963969
self.driver.rollback()
964-
return SAVE_FAIL
970+
return SAVE_FAIL + SHOW_MESSAGE
965971

966972
# If we made it here, we can commit the changes
967973
self.driver.commit()
@@ -985,7 +991,7 @@ def save_record(self, display_message=True, update_elements=True):
985991
logger.debug(f'Record Saved!')
986992
if display_message: sg.popup_quick_message('Updates saved successfully!',keep_on_top=True)
987993

988-
return SAVE_SUCCESS
994+
return SAVE_SUCCESS + SHOW_MESSAGE
989995

990996

991997
def save_record_recursive(self):
@@ -1707,43 +1713,36 @@ def prompt_save(self, autosave=False) -> int:
17071713
if save_changes != 'Yes':
17081714
# update the elements to erase any GUI changes, since we are choosing not to save
17091715
self.update_elements()
1710-
return PROMPT_DISCARDED # We did have a change, regardless if the user chose not to save
1716+
return PROMPT_SAVE_DISCARDED # We did have a change, regardless if the user chose not to save
17111717
self[q].save_record(update_elements=False) # Don't update elements yet, as there may be more saving to do yet
17121718
self.update_elements() # Now we are safe to update elements
1713-
return PROMPT_PROCEED if user_prompted else PROMPT_NONE
1719+
return PROMPT_SAVE_PROCEED if user_prompted else PROMPT_SAVE_NONE
17141720

17151721

17161722

17171723

17181724
def save_records(self, cascade_only=False):
17191725
logger.debug(f'Saving records in all queries...')
1720-
msg = None
1721-
i = 0
1722-
tables = self.get_cascaded_relationships() if cascade_only else self.queries
1723-
last_index = len(self.queries) - 1
17241726

1725-
successes=0
1726-
failures=0
1727-
no_actions=0
1727+
result = 0
1728+
show_message = True
1729+
tables = self.get_cascaded_relationships() if cascade_only else self.queries
17281730
for t in tables:
17291731
logger.debug(f'Saving records for table {t}...')
1730-
result=self[t].save_record(False,update_elements=False)
1731-
if result==SAVE_FAIL:
1732-
failures+=1
1733-
elif result==SAVE_SUCCESS:
1734-
successes+=1
1735-
elif result==SAVE_NONE:
1736-
no_actions+=1
1737-
logger.debug(f'Successes: {successes}, Failures: {failures}, No Actions: {no_actions}')
1738-
1739-
if failures==0:
1740-
if successes==0:
1741-
sg.popup_quick_message('There was nothing to update.', keep_on_top=True)
1732+
res = self[t].save_record(False,update_elements=False)
1733+
if not res & SHOW_MESSAGE: show_message = False # Only one instance of not showing the message hides all
1734+
result |= res
1735+
1736+
logger.debug(f'Success: {result & SAVE_SUCCESS}, Failure: {result & SAVE_FAIL}, No Action: {result & SAVE_NONE}')
1737+
1738+
if result & SAVE_FAIL:
1739+
if show_message: sg.popup('There was a problem saving some updates.', keep_on_top=True)
1740+
else:
1741+
if result & SAVE_NONE:
1742+
if show_message: sg.popup_quick_message('There was nothing to update.', keep_on_top=True)
17421743
else:
1743-
sg.popup_quick_message('Updates saved successfully!',keep_on_top=True)
1744+
if show_message: sg.popup_quick_message('Updates saved successfully!',keep_on_top=True)
17441745
self.update_elements()
1745-
else:
1746-
sg.popup('There was a problem saving some updates.', keep_on_top=True)
17471746

17481747

17491748

0 commit comments

Comments
 (0)