Re: [Zope3-Users] How to access "request" variable from vocabulary

2007-08-07 Thread Hermann Himmelbauer
Am Montag, 6. August 2007 19:08 schrieb Benji York:
> Adam Groszer wrote:
> > I'm using something like this:
> >
> > def getCurrentRequest():
> > return getInteraction().participations[0]
>
> It is, of course, better if you can structure your app so you don't have
> to pull the request "out of thin air", but if you do, I'd be a little
> more careful about it.  Like so:
>
> def getRequest():
>  i = zope.security.management.getInteraction() # raises
> NoInteraction
>  for p in i.participations:
>  if IRequest.providedBy(p):
>  return p
>  raise RuntimeError('Could not find current request.')

Ah, thanks a lot for this, it works.

In my RDB/SQLAlchemy-based application, when a user logs in, his 
database-userID is stored into the session. In some forms, I have choices 
which are filled with user-related data (e.g. his email addresses). To 
retrieve this data from the relational database, I need the ID from the 
session.

I'd prefer if the vocabulary had direct access to the request, but it seems 
that vocabularies are bound to the content object and not to the view, 
whereas the request is only available in the view.

One solution could be to build a view-bound vocabulary, but that's probably 
hard to do and may break other things.  So this seems like a structural 
problem that is not easy to solve.

Best Regards,
Hermann

-- 
[EMAIL PROTECTED]
GPG key ID: 299893C7 (on keyservers)
FP: 0124 2584 8809 EF2A DBF9  4902 64B4 D16B 2998 93C7
___
Zope3-users mailing list
Zope3-users@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users


Re: [Zope3-Users] How to access "request" variable from vocabulary

2007-08-06 Thread Benji York

Yow, the whitespace in my last message was seriously messed up.  Let me
try again.

Adam Groszer wrote:
> I'm using something like this:
>
> def getCurrentRequest():
> return getInteraction().participations[0]

It is, of course, better if you can structure your app so you don't have
to pull the request "out of thin air", but if you do, I'd be a little
more careful about it.  Like so:

def getRequest():
 i = zope.security.management.getInteraction()

 for p in i.participations:
 if IRequest.providedBy(p):
 return p

 raise RuntimeError('Could not find current request.')
--
Benji York
Senior Software Engineer
Zope Corporation
___
Zope3-users mailing list
Zope3-users@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users


Re: [Zope3-Users] How to access "request" variable from vocabulary

2007-08-06 Thread Benji York

Adam Groszer wrote:

I'm using something like this:

def getCurrentRequest():
return getInteraction().participations[0]


It is, of course, better if you can structure your app so you don't have 
to pull the request "out of thin air", but if you do, I'd be a little 
more careful about it.  Like so:


def getRequest(): 




i = zope.security.management.getInteraction() # raises 
NoInteraction 




for p in i.participations: 




if IRequest.providedBy(p): 




return p 








raise RuntimeError('Could not find current request.')
--
Benji York
Senior Software Engineer
Zope Corporation
___
Zope3-users mailing list
Zope3-users@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users


Re: [Zope3-Users] How to access "request" variable from vocabulary

2007-08-06 Thread Adam Groszer
Hello Hermann,

I'm using something like this:

def getCurrentRequest():
return getInteraction().participations[0]


Monday, August 6, 2007, 6:32:16 PM, you wrote:

> Hi,
> I have a simple function that is registered a vocabulary for a Choice-widget.

> My problem is that the vocabulary should access the session, which seems to be
> only possible if I somehow get the "request" variable, which is available in
> a view. (A common way to retrieve the session seems to 
> be "session=ISession(self.request)[KEY])".

> However, the vocabulary seems to get only the context as a parameter but not
> the view.

> How can I solve this problem?

> Best Regards,
> Hermann


-- 
Best regards,
 Adammailto:[EMAIL PROTECTED]

___
Zope3-users mailing list
Zope3-users@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users


[Zope3-Users] How to access "request" variable from vocabulary

2007-08-06 Thread Hermann Himmelbauer
Hi,
I have a simple function that is registered a vocabulary for a Choice-widget.

My problem is that the vocabulary should access the session, which seems to be 
only possible if I somehow get the "request" variable, which is available in 
a view. (A common way to retrieve the session seems to 
be "session=ISession(self.request)[KEY])".

However, the vocabulary seems to get only the context as a parameter but not 
the view.

How can I solve this problem?

Best Regards,
Hermann

-- 
[EMAIL PROTECTED]
GPG key ID: 299893C7 (on keyservers)
FP: 0124 2584 8809 EF2A DBF9  4902 64B4 D16B 2998 93C7
___
Zope3-users mailing list
Zope3-users@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users