You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm currently working on a very complicated project with many database-heavy operations and, most importantly, event listeners.
One such operation is logging every change of a model's attributes in before_update and adding that log to the database:
deflog_awb_change(mapper, connection, target):
fromappimportignored_awb_fieldsuser_id=Nonetry:
user_id=get_jwt_identity()
exceptRuntimeError:
print("Log awb change called outside app context")
returniftarget.skip_logortarget.id<1:
returnlogs= []
fromapp.models.awbimportAwbLogifnotuser_id:
returnstate=inspect(target)
forattrinstate.attrs:
ifattr.keyinignored_awb_fieldsornotattr.history.has_changes():
continueoldval=attr.history.deleted[0] ifattr.history.deletedelseNonenewval=attr.history.added[-1] ifattr.history.addedelseNoneifoldval==newval:
continueifattr.keyinawb_filesand (notoldvalornotnewval):
ifnotoldvalandnewval:
log=AwbLog(
awb_id=target.id,
text=f"fișierul {attr.key} a fost încărcat",
user_id=user_id
)
logs.append(log)
elifoldvalandnotnewval:
log=AwbLog(
awb_id=target.id,
text=f"fișierul {attr.key} a fost șters",
user_id=user_id
)
logs.append(log)
continueelifnotisinstance(newval, list) andnottype_inspect.isclass(newval) andnotisinstance(newval, datetime) andnotisinstance(newval, bool):
text=""ifnotoldval:
text=f"{attr.key} a fost adăugat cu valoarea {newval}"elifnotnewval:
text=f"{attr.key} a fost șters"elifoldval!=newval:
text=f"{attr.key} schimbat din {oldval} în {newval}"ifattr.keynotinawb_fileselsef"fișierul {attr.key} a fost schimbat"log=AwbLog(
awb_id=target.id,
text=text,
user_id=user_id
)
logs.append(log)
ifattr.key=='status':
ifnewval=='free from customs':
fromapp.admin.borderouimportadd_to_borderoufromapp.admin.messageimportfn_send_free_from_customsadd_to_borderou(target, type='bord-out')
fn_send_free_from_customs(target, user_id, db.session)
db.session.bulk_save_objects(logs)
Now, this all worked fine before needing another change, which is associating multiple objects to one, basically creating a sort of "hierarchy", where if one object's attribute changes, so does the same attribute for all objects in the hierarchy, keeping them all identical.
I have tried to make a function that would do this very thing, but it's causing a lot of unexpected issues and it's making me go back to the drawing board, because I have no idea what the best approach would be to update all attributes for the members in the hierarchy.
The desired result would be something like: member updates -> updates all members -> log change for current member -> log change for all members -> save the changes to the db.
Weirdly enough, setattr didn't seem to update the value of any member, but assigning the value straight up like child.status = target.status seemed to track those changes.
What would be the best approach to logging the changes and updating the hierarchy while still causing as little flushes?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
I'm currently working on a very complicated project with many database-heavy operations and, most importantly, event listeners.
One such operation is logging every change of a model's attributes in before_update and adding that log to the database:
Now, this all worked fine before needing another change, which is associating multiple objects to one, basically creating a sort of "hierarchy", where if one object's attribute changes, so does the same attribute for all objects in the hierarchy, keeping them all identical.
I have tried to make a function that would do this very thing, but it's causing a lot of unexpected issues and it's making me go back to the drawing board, because I have no idea what the best approach would be to update all attributes for the members in the hierarchy.
The desired result would be something like: member updates -> updates all members -> log change for current member -> log change for all members -> save the changes to the db.
Below is the aforementioned function:
Weirdly enough, setattr didn't seem to update the value of any member, but assigning the value straight up like
child.status = target.statusseemed to track those changes.What would be the best approach to logging the changes and updating the hierarchy while still causing as little flushes?
Beta Was this translation helpful? Give feedback.
All reactions