diff options
author | Philip Sargent <philip.sargent@klebos.com> | 2021-04-13 00:11:08 +0100 |
---|---|---|
committer | Philip Sargent <philip.sargent@klebos.com> | 2021-04-13 00:11:08 +0100 |
commit | 2a1710596a48ce0dbd2b4aa5f510f0c79bd57b99 (patch) | |
tree | 9c93df7272f53ce66e3ff6b0340b972dcb75ec5f /core/utils.py | |
parent | b602f3ae13ef8c620c19ea3e56257f0693e0a1b0 (diff) | |
download | troggle-2a1710596a48ce0dbd2b4aa5f510f0c79bd57b99.tar.gz troggle-2a1710596a48ce0dbd2b4aa5f510f0c79bd57b99.tar.bz2 troggle-2a1710596a48ce0dbd2b4aa5f510f0c79bd57b99.zip |
moving save_carefully()
Diffstat (limited to 'core/utils.py')
-rw-r--r-- | core/utils.py | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/core/utils.py b/core/utils.py index 4dcee68..deca124 100644 --- a/core/utils.py +++ b/core/utils.py @@ -60,6 +60,48 @@ def chaosmonkey(n): # print("CHAOS strikes !", file=sys.stderr) return True + +def save_carefully(objectType, lookupAttribs={}, nonLookupAttribs={}): + """Looks up instance using lookupAttribs and carries out the following: + -if instance does not exist in DB: add instance to DB, return (new instance, True) + -if instance exists in DB and was modified using Troggle: do nothing, return (existing instance, False) + -if instance exists in DB and was not modified using Troggle: overwrite instance, return (instance, False) + + The checking is accomplished using Django's get_or_create and the new_since_parsing boolean field + defined in core.models.TroggleModel. + + """ + try: + instance, created = objectType.objects.get_or_create(defaults=nonLookupAttribs, **lookupAttribs) + except: + print(" !! - SAVE CAREFULLY ===================", objectType) + print(" !! - -- objects.get_or_create()") + print(" !! - lookupAttribs:{}\n !! - nonLookupAttribs:{}".format(lookupAttribs,nonLookupAttribs)) + raise + if not created and not instance.new_since_parsing: + for k, v in list(nonLookupAttribs.items()): #overwrite the existing attributes from the logbook text (except date and title) + setattr(instance, k, v) + try: + instance.save() + except: + print(" !! - SAVE CAREFULLY ===================", objectType) + print(" !! - -- instance.save()") + print(" !! - lookupAttribs:{}\n !! - nonLookupAttribs:{}".format(lookupAttribs,nonLookupAttribs)) + raise + try: + msg = str(instance) + except: + msg = "FAULT getting __str__ for instance with lookupattribs: {}:".format(lookupAttribs) + if created: + logging.info(str(instance) + ' was just added to the database for the first time. \n') + + if not created and instance.new_since_parsing: + logging.info(str(instance) + " has been modified using Troggle, so the current script left it as is. \n") + + if not created and not instance.new_since_parsing: + logging.info(str(instance) + " existed in the database unchanged since last parse. It was overwritten by the current script. \n") + return (instance, created) + # def get_related_by_wikilinks(wiki_text): # found=re.findall(settings.QM_PATTERN,wiki_text) # res=[] |