This is the way I solved the problem... (how to backup a table row into another table before delete or update it) ...if it can help:

from sqlalchemy.orm import MapperExtension
class History(MapperExtension):
def __init__(self):
MapperExtension.__init__(self)
self.methods = ('before_update','before_delete')

def clone(self, mapper, connection, instance, action):
tablename=mapper.mapped_table.name
tablename_bak=tablename+'_bak'
rec = connection.execute(select([tbl[tablename]], tbl[tablename].c.id == getattr( instance, '%s_id'%tablename))).fetchone()
dd=dict()
modified=False
for k,v in rec.items():
dd[ k.lower() ] = v
if action=='D': #if delete request...
connection.execute(tbl[tablename_bak].insert(values=dd))
elif action=='U': #if update request...
for k in instance.c: #check for differences, to save it only if it was modified...
if getattr( instance,str(k).replace('.','_')) != dd[str(k).split('.')[1]]:
modified=True
break
if modified is True:
connection.execute(tbl[tablename_bak].insert(values=dd))
return

def before_update(self, mapper, connection, instance):
return self.clone(mapper, connection, instance, 'U')

def before_delete(self, mapper, connection, instance):
return self.clone(mapper, connection, instance, 'D')


I call it in this way:

from ... import History
mapper(Anagrafica,
tbl['anagrafica'],
column_prefix = 'anagrafica_',
extension=History(),
)


Paddy Mullen wrote:
I have been trying to create a nice decorator for tasks that are methods of models. I want the tasks to run after specific conditions (after_update/insert, with predicates).

I was able to set this up through a series of event hooks starting with "mapper_configured".

The problem I have run into is, listening for "after_insert" on a subclass extending DeclarativeBase, only results in calls after insert has been called for, but not after it has been executed. I can listen on the engine to "after_execute", which does seem to give a callback after commit/flush has actually been called, but at this I have no declarativeBase references to the objects that have been inserted, only to the raw sql.

Am I missing something?

Here are the example files
https://github.com/paddymul/sqlalchemy_garden/blob/master/lib/deferred_task.py - the library

https://github.com/paddymul/sqlalchemy_garden/blob/master/schemas/deferred_schema.py - an example usage

Thanks,
Paddy
--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To view this discussion on the web visit https://groups.google.com/d/msg/sqlalchemy/-/IpLW9LroG6IJ.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.


--
Jose Soares _/_/ Sferacarta Net Via Bazzanese 69 _/_/ _/_/_/
40033 Casalecchio di Reno             _/_/   _/_/  _/_/
Bologna - Italy                      _/_/   _/_/  _/_/
Ph  +39051591054              _/_/  _/_/   _/_/  _/_/
fax +390516131537            _/_/  _/_/   _/_/  _/_/
web:www.sferacarta.com        _/_/_/       _/_/_/

Le informazioni contenute nella presente mail ed in ogni eventuale file 
allegato sono riservate e, comunque, destinate esclusivamente alla persona o 
ente sopraindicati, ai sensi del decreto legislativo 30 giugno 2003, n. 196. La 
diffusione, distribuzione e/o copiatura della mail trasmessa, da parte di 
qualsiasi soggetto diverso dal destinatario, sono vietate. La correttezza, 
l’integrità e la sicurezza della presente mail non possono essere garantite. Se 
avete ricevuto questa mail per errore, Vi preghiamo di contattarci 
immediatamente e di eliminarla. Grazie.

This communication is intended only for use by the addressee, pursuant to 
legislative decree 30 June 2003, n. 196. It may contain confidential or 
privileged information. You should not copy or use it to disclose its contents 
to any other person. Transmission cannot be guaranteed to be error-free, 
complete and secure. If you are not the intended recipient and receive this 
communication unintentionally, please inform us immediately and then delete 
this message from your system. Thank you.

--
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to