[Zope3-Users] User preferences for languages- possible?
Giovannetti, Mark
giovanne at nrcan.gc.ca
Thu Feb 22 16:10:18 EST 2007
Hi,
This stuff is crazy. I just want to implement a user
preference for a language (because I have to). I've
read all the user pref. documentation (skinpref, apidoc
prefs and zope.app.preference) until I'm blue in the
face. Nothing seems to work and there is no help via
google and so forth.
I know that UserPreferences() wants an ILocation object.
I can't seem to get one inside the BrowserFormLanguages
class. I am only given the request object.
What do I need to do? Getting a root site is insane
the way I found to do it via a mailing list message. Not
to mention it had issues racking up db connections, then
failing on close.
Help! Please! Thank you!
Preferences seem to work since I can edit them using
the http://.../++preferences++/ form.
Mark
P.S. This is not covered in Richter's book, nor von
Weitershausen's (1st ed.) both of which I bought.
Experimental code and traceback follow:
=============== 8<=========== language.py ===========
from zope.interface import implements
from zope.component import adapts
from zope.publisher.interfaces.browser import IBrowserRequest
from zope.i18n.interfaces import IUserPreferredLanguages
from zope.i18n.interfaces import IModifiableUserPreferredLanguages
from zope.app.session.interfaces import IClientId
from zope.publisher.browser import BrowserLanguages
from zope.app.component.hooks import setSite, getSite
from zope.app.preference.interfaces import IUserPreferences
from zope.app.preference import UserPreferences
#import ZODB
#from zope.component import getUtility
class BrowserFormLanguages(BrowserLanguages):
implements(IModifiableUserPreferredLanguages)
language_store = [] # ignore this for now
def __init__(self, request):
super(BrowserFormLanguages, self).__init__(request)
print "created BFL", self.language_store
def setPreferredLanguages(self, langs):
#prefs = UserPreferences()
#print prefs.eods.general.language
self.language_store = langs
print "set language", self.language_store
def getPreferredLanguages(self):
langs = super(BrowserFormLanguages, self).getPreferredLanguages()
if self.language_store:
langs = self.language_store + langs
form_lang = self.request.get("Language", None)
if form_lang is not None:
langs.insert(0, form_lang)
# this just leads to madness and a horrible error
# [...] exceptions.ValueError: I/O operation on closed file
# db = getUtility(ZODB.interfaces.IDatabase)
# conn = db.open()
# root = conn.root().data['Application']
# setSite(root)
# prefs = UserPreferences(getSite())
prefs = UserPreferences()
print "Prefs", repr(prefs)
# print repr(getSite())
print prefs.__dict__
# crashes on the next line - but prints the word lang
print "lang", prefs.eods.general.language
print "My prefered override", langs
# db.close()
return langs
======================
Traceback:
==========================================
created BFL []
Prefs <zope.app.preference.preference.PreferenceGroup object at 0x963466c>
{'__schema__': None, '__Security_checker__': <zope.security.checker.Checker object at 0xa153dec>, '__description__': u'', '_PreferenceGroup__parent': <BaseGlobalComponents base>, '__name__': '++preferences++', '__title__': u'User Preferences', '__provides__': <zope.interface.Provides object at 0x95bfe8c>, '__id__': u''}
lang------
2007-02-22T15:54:50 DEBUG twisted Exception rendering:
------
2007-02-22T15:54:50 DEBUG twisted Traceback (most recent call last):
File "/usr/local/lib/python2.4/threading.py", line 422, in run
self.__target(*self.__args, **self.__kwargs)
File "/usr/local/www/Zope3/lib/python/twisted/python/threadpool.py", line 149, in _worker
context.call(ctx, function, *args, **kwargs)
File "/usr/local/www/Zope3/lib/python/twisted/python/context.py", line 59, in callWithContext
return self.currentContext().callWithContext(ctx, func, *args, **kw)
File "/usr/local/www/Zope3/lib/python/twisted/python/context.py", line 37, in callWithContext
return func(*args,**kw)
--- <exception caught here> ---
File "/usr/local/www/Zope3/lib/python/twisted/web2/wsgi.py", line 139, in run
result = self.application(self.environment, self.startWSGIResponse)
File "/usr/local/www/Zope3/lib/python/zope/app/wsgi/__init__.py", line 50, in __call__
request = self.requestFactory(environ['wsgi.input'], environ)
File "/usr/local/www/Zope3/lib/python/zope/app/publication/httpfactory.py", line 69, in __call__
request = request_class(input_stream, env)
File "/usr/local/www/Zope3/lib/python/zope/publisher/browser.py", line 229, in __init__
super(BrowserRequest, self).__init__(body_instream, environ, response)
File "/usr/local/www/Zope3/lib/python/zope/publisher/http.py", line 323, in __init__
self.setupLocale()
File "/usr/local/www/Zope3/lib/python/zope/publisher/http.py", line 331, in setupLocale
langs = envadapter.getPreferredLanguages()
File "/jmnt/usr/local/www/Zope3/eodsfm/lib/python/books/finance/language.py", line 64, in getPreferredLanguages
print "lang", prefs.eods.general.language
File "/usr/local/www/Zope3/lib/python/zope/app/preference/preference.py", line 141, in __getattr__
value = self.data.get(key, marker)
File "/usr/local/www/Zope3/lib/python/zope/app/preference/preference.py", line 177, in data
utility = zapi.getUtility(IPrincipalAnnotationUtility, context=self)
File "/usr/local/www/Zope3/lib/python/zope/component/_api.py", line 207, in getUtility
raise ComponentLookupError(interface, name)
zope.component.interfaces.ComponentLookupError: (<InterfaceClass zope.app.principalannotation.interfaces.IPrincipalAnnotationUtility>, '')
More information about the Zope3-users
mailing list