MapperExtensions seem to be a deprecated interface to the same
functionality that I am using with events
http://docs.sqlalchemy.org/en/latest/orm/interfaces.html .
I guess my fundamental problem is that I don't see a straightforward way to
get a reference to a model instance in an "after_insert" or "after_update"
event, after the changes have actually been committed to the database.
Updates aren't as big a problem as inserts. For inserts, you can't easily
enqueue the id of the model, because that ID hasn't been assigned yet.
I don't want to have to worry about firing off an enqueued task in every
place I modify a model. I think it is better to encapsulate that logic in
one place.
On Wednesday, April 11, 2012 2:07:07 AM UTC-4, jo wrote:
>
> 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 [email protected].
> > To unsubscribe from this group, send email to
> > [email protected].
> > 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 view this discussion on the web visit
https://groups.google.com/d/msg/sqlalchemy/-/aouoaymUXLUJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.