суббота, 9 февраля 2013 г., 18:34:01 UTC+2 пользователь Andreas Jung
написал:
>
> Why don't you update the counter inside the add_post() operation?
>
I'm porting one forum app from django to pyramid, and there it is made
through signals.
https://github.com/slav0nic/DjangoBB/blob/master/djangobb_forum/signals.py
So my first thought was to use SQLAlchemy's events, but it seems they are
not for this kind of tasks.
Updating the counter within the commit phase is broken and also trying
> to update the counter using a different session outside the current
> transaction
> is broken.
>
-aj
>
> bismigalis wrote:
> > I want in my Pyramid app to use SQLAchemy event 'after_insert', to
> > update Topic model with number of posts belong to it.
> >
> > _views.py_
> >
> > def add_post(topic, request):
> > post_form = PostForm(request.POST)
> > if 'submit' in request.POST and post_form.validate():
> > post = Post(body=post_form.body.data)
> > post.user = request.user
> > post.topic = topic
> > DBSession.add(post)
> > request.session.flash(_('Post was added'))
> > transaction.commit()
> > raise
> > HTTPFound(location=request.route_url('topic',id=topic.id))
> > return {'post_form':post_form}
> >
> > _models.py_
> >
> > class Topic(Base):
> > __tablename__ = 'topics'
> > id = Column(Integer, primary_key=True)
> > ...
> > post_count = Column(Integer, default=0)
> > posts = relationship('Post',
> > primaryjoin="Post.topic_id==Topic.id", backref='topic', lazy='dynamic')
> >
> >
> > class Post(Base):
> > __tablename__ = 'posts'
> > id = Column(Integer, primary_key=True)
> > ...
> > topic_id = Column(Integer, ForeignKey('topics.id'))
> >
> >
> > def post_inserted(mapper, conn, post):
> > topic = post.topic
> > topic.post_count = topic.posts.count()
> >
> > event.listen(Post, "after_insert", post_inserted)
> >
> > But I get exeption:
> >
> > Traceback (most recent call last):
> > File
> >
> "/home/user/workspace/myforum/env/lib/python2.6/site-packages/waitress/channel.py",
>
>
> > line 329, in service
> > task.service()
> > File
> >
> "/home/user/workspace/myforum/env/lib/python2.6/site-packages/waitress/task.py",
>
>
> > line 173, in service
> > self.execute()
> > File
> >
> "/home/user/workspace/myforum/env/lib/python2.6/site-packages/waitress/task.py",
>
>
> > line 380, in execute
> > app_iter = self.channel.server.application(env, start_response)
> > File
> >
> "/home/user/workspace/myforum/env/lib/python2.6/site-packages/pyramid/router.py",
>
>
> > line 251, in __call__
> > response = self.invoke_subrequest(request, use_tweens=True)
> > File
> >
> "/home/user/workspace/myforum/env/lib/python2.6/site-packages/pyramid/router.py",
>
>
> > line 227, in invoke_subrequest
> > response = handle_request(request)
> > File
> >
> "/home/user/workspace/myforum/env/lib/python2.6/site-packages/pyramid_tm/__init__.py",
>
>
> > line 107, in tm_tween
> > return response
> > File
> >
> "/home/user/workspace/myforum/env/lib/python2.6/site-packages/transaction/_manager.py",
>
>
> > line 116, in __exit__
> > self.commit()
> > File
> >
> "/home/user/workspace/myforum/env/lib/python2.6/site-packages/transaction/_manager.py",
>
>
> > line 107, in commit
> > return self.get().commit()
> > File
> >
> "/home/user/workspace/myforum/env/lib/python2.6/site-packages/transaction/_transaction.py",
>
>
> > line 354, in commit
> > reraise(t, v, tb)
> > File
> >
> "/home/user/workspace/myforum/env/lib/python2.6/site-packages/transaction/_transaction.py",
>
>
> > line 345, in commit
> > self._commitResources()
> > File
> >
> "/home/user/workspace/myforum/env/lib/python2.6/site-packages/transaction/_transaction.py",
>
>
> > line 493, in _commitResources
> > reraise(t, v, tb)
> > File
> >
> "/home/user/workspace/myforum/env/lib/python2.6/site-packages/transaction/_transaction.py",
>
>
> > line 465, in _commitResources
> > rm.tpc_begin(self)
> > File
> >
> "/home/user/workspace/myforum/env/lib/python2.6/site-packages/zope/sqlalchemy/datamanager.py",
>
>
> > line 86, in tpc_begin
> > self.session.flush()
> > File
> >
> "/home/user/workspace/myforum/env/lib/python2.6/site-packages/sqlalchemy/orm/session.py",
>
>
> > line 1583, in flush
> > self._flush(objects)
> > File
> >
> "/home/user/workspace/myforum/env/lib/python2.6/site-packages/sqlalchemy/orm/session.py",
>
>
> > line 1654, in _flush
> > flush_context.execute()
> > File
> >
> "/home/user/workspace/myforum/env/lib/python2.6/site-packages/sqlalchemy/orm/unitofwork.py",
>
>
> > line 331, in execute
> > rec.execute(self)
> > File
> >
> "/home/user/workspace/myforum/env/lib/python2.6/site-packages/sqlalchemy/orm/unitofwork.py",
>
>
> > line 475, in execute
> > uow
> > File
> >
> "/home/user/workspace/myforum/env/lib/python2.6/site-packages/sqlalchemy/orm/persistence.py",
>
>
> > line 67, in save_obj
> > states_to_insert, states_to_update)
> > File
> >
> "/home/user/workspace/myforum/env/lib/python2.6/site-packages/sqlalchemy/orm/persistence.py",
>
>
> > line 702, in _finalize_insert_update_commands
> > mapper.dispatch.after_insert(mapper, connection, state)
> > File
> >
> "/home/user/workspace/myforum/env/lib/python2.6/site-packages/sqlalchemy/event.py",
>
>
> > line 291, in __call__
> > fn(*args, **kw)
> > File
> >
> "/home/user/workspace/myforum/env/lib/python2.6/site-packages/sqlalchemy/orm/events.py",
>
>
> > line 360, in wrap
> > wrapped_fn(*arg, **kw)
> > File "/home/user/workspace/myforum/cube_forum/models.py", line
> > 165, in post_saved
> > topic.post_count = topic.posts.count()
> > File
> >
> "/home/user/workspace/myforum/env/lib/python2.6/site-packages/sqlalchemy/orm/dynamic.py",
>
>
> > line 249, in count
> > sess = self.__session()
> > File
> >
> "/home/user/workspace/myforum/env/lib/python2.6/site-packages/sqlalchemy/orm/dynamic.py",
>
>
> > line 219, in __session
> > sess.flush()
> > File
> >
> "/home/user/workspace/myforum/env/lib/python2.6/site-packages/sqlalchemy/orm/session.py",
>
>
> > line 1577, in flush
> > raise sa_exc.InvalidRequestError("Session is already flushing")
> > InvalidRequestError: Session is already flushing
> >
> > I think if session already flushing, may be I need to start new
> > db_session in event handler, how to do it right?
> >
> > --
> > You received this message because you are subscribed to the Google
> > Groups "pylons-discuss" group.
> > To unsubscribe from this group and stop receiving emails from it, send
> > an email to [email protected] <javascript:>.
> > To post to this group, send email to
> > [email protected]<javascript:>.
>
> > Visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
>
> > For more options, visit https://groups.google.com/groups/opt_out.
> >
> >
>
--
You received this message because you are subscribed to the Google Groups
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.