[Twisted-Python] returning a deferred in getStateToCopyFor.

Flint grindizer at gmail.com
Sat Mar 31 03:59:50 MDT 2012


Hi, all.
using PB i wanted to implement some access control check on my copyable
objects.
so i placed that in getStateToCopyFor, all is ok except when i need to
perfom some asynchronous call in that method.
ex: (i use twisted 10.1.0)
...
def getStateToCopyFor(self, user):
  state = self.getStateToCopy()
  d = user.has_perm()  # this do an asynchronous call
  def got_perm(allowed, state):
    if not allowed:
      state['secret'] = ''
      return state
  d.addCallback(got_perm, state)
  return d
...

here user.has_perm performe a database access so it returns a deferred, but
when i return that deferred, i got an error saying that 'state' is not a
dict (which is quit normal cause it's a deferred).
it seems pb do not handle this case, i know i can put my control logic
directly in the view method (where returning a deferred is handled) but
this will lead to a lot of code duplication, and i really thought that
getStateToCopyFor is made for that purpouse.
so i wanted to ask what's the best way to achieve that ?

Thankx !
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20120331/c1e4737b/attachment-0001.html>


More information about the Twisted-Python mailing list