User-Defined Validations
Imagine you’re building a set
of year-end accounts, repeatedly importing data from several sources
and making manual edits. It all has to be perfect, and it’s
2:00 a.m. You think you have the first half of the year sorted out
and accidentally enter a transaction dated October 1998 instead of
October 1999. It doesn’t show up in the View
you are looking at, so you enter it again and make other corrections
elsewhere based on your erroneous account balances. If you are
unlucky, you could waste hours finding the error and unravelling all
the dependent changes (One author knows this all too well). The
userhooks file also defines a sample
Validator that puts a time lock on the
BookSet; this prevents any changes before a cutoff
date. Here’s the code:
class DateWindowValidator(Validator):
"""An example. Prevents changes on or before a certain date
locking the bookset"""
def __init__(self, aDescription, aDate):
Validator.__init__(self, aDescription)
self.cutoff = aDate
def mayAdd(self, aTransaction):
return (aTransaction.date > self.cutoff)
def mayEdit(self, index, newTransaction):
oldtran = self.BookSet[index]
if oldtran.date <= self.cutoff:
return 0
elif newTransaction.date <= self.cutoff:
return 0
else:
return 1
def mayRemove(self, index):
tran = self.BookSet[index]
return (tran.date > self.cutoff)
# renameAccount will not break anythingMore sophisticated Validators might have beginning and ending time windows, a list of accounts not to touch, or even a user-related ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access