TurboGears / tg2

Python web framework with full-stack layer implemented on top of a microframework core with support for SQL DBMS, MongoDB and Pluggable Applications
http://www.turbogears.org/
Other
806 stars 78 forks source link

Sprox setters not working #61

Closed moschlar closed 9 years ago

moschlar commented 9 years ago

With ZopeTransactionExtension(keep_session=True), Sprox setters (http://turbogears.readthedocs.org/en/latest/cookbook/Crud/index.html?highlight=setters#customizing-easycrudrestcontroller) don't work anymore. They are simply not setting the value they are supposed to set.

amol- commented 9 years ago

Saw your report, need to experiment a bit because they are actually plain setattr() calls and so there shouldn't be anything particular that might change the behaviour.

amol- commented 9 years ago

Tested right now in a newly quickstarted project with following code:

import datetime
from tgext.crud import EasyCrudRestController

class TestController(EasyCrudRestController):
    model = model.User

    __setters__ = {
        'release':('created', lambda x: datetime.datetime.utcnow())
    }

class RootController(BaseController):
    test = TestController(model.DBSession)

By accessing http://localhost:8080/test/ I see the users with their created field, then opening http://localhost:8080/test/1/release I see the manager created date correctly updating both with and without keep_session=True in model/__init__.py.

Does it work for you the same test?

amol- commented 9 years ago

any update? It looks that they are working in the previous example

moschlar commented 9 years ago

Ah, I'm sorry, gonna look into it tomorrow - leaving tab open!

moschlar commented 9 years ago

Hm, no, when I do that within my SAUCE project, it doesn't work either... Enabling sqlalchemy query logging also shows that only select statements got omitted and then there's a rollback.

When I run it in a fresh virtualenv with a fresh quickstart, I get:

Traceback (most recent call last):
  File "/home/moschlar/.virtualenvs/tetz/lib/python2.7/site-packages/tg/wsgiapp.py", line 129, in __call__
    return response(environ, start_response)
  File "/home/moschlar/.virtualenvs/tetz/lib/python2.7/site-packages/webob/exc.py", line 500, in __call__
    environ, start_response)
  File "/home/moschlar/.virtualenvs/tetz/lib/python2.7/site-packages/webob/exc.py", line 330, in __call__
    app_iter = self.generate_response(environ, start_response)
  File "/home/moschlar/.virtualenvs/tetz/lib/python2.7/site-packages/webob/exc.py", line 309, in generate_response
    body = self.html_body(environ)
  File "/home/moschlar/.virtualenvs/tetz/lib/python2.7/site-packages/webob/exc.py", line 298, in html_body
    body = self._make_body(environ, html_escape)
  File "/home/moschlar/.virtualenvs/tetz/lib/python2.7/site-packages/webob/exc.py", line 284, in _make_body
    args[k] = escape(v)
  File "/home/moschlar/.virtualenvs/tetz/lib/python2.7/site-packages/webob/util.py", line 32, in html_escape
    s = str(s)
  File "/tmp/TETZ/tetz/model/auth.py", line 95, in __repr__
    repr(self.user_name),
  File "/home/moschlar/.virtualenvs/tetz/lib/python2.7/site-packages/sqlalchemy/orm/attributes.py", line 237, in __get__
    return self.impl.get(instance_state(instance), dict_)
  File "/home/moschlar/.virtualenvs/tetz/lib/python2.7/site-packages/sqlalchemy/orm/attributes.py", line 573, in get
    value = state._load_expired(state, passive)
  File "/home/moschlar/.virtualenvs/tetz/lib/python2.7/site-packages/sqlalchemy/orm/state.py", line 480, in _load_expired
    self.manager.deferred_scalar_loader(self, toload)
  File "/home/moschlar/.virtualenvs/tetz/lib/python2.7/site-packages/sqlalchemy/orm/loading.py", line 608, in load_scalar_attributes
    (state_str(state)))
DetachedInstanceError: Instance <User at 0x2ac7f3638650> is not bound to a Session; attribute refresh operation cannot proceed
moschlar commented 9 years ago

Ah, but in the sqlalchemy query log, I can see that it basically worked.

11:13:11,222 INFO  [sqlalchemy.engine.base.Engine] UPDATE tg_user SET created=? WHERE tg_user.user_id = ?
11:13:11,222 INFO  [sqlalchemy.engine.base.Engine] ('2015-06-24 09:13:11.221136', 1)
11:13:11,223 INFO  [sqlalchemy.engine.base.Engine] COMMIT

So it must be something that's different on my side, sigh... :D

amol- commented 9 years ago

Closing as setters seem to properly work as stated by query log, please reopen if you find they are actually broken.