def getPreferredLanguages(self): # If the default locale negotiater can get a value, # that means we had a parameter or one of the cookies # (because of the subscriber that gets us here). negotiated = default_locale_negotiator(self.request) if negotiated: return [negotiated] # Here is where we would check for something during traversal, # but we don't actually support that at this time because it # relies on implementation details # Is there a non-default user preference? Right now we know # what a default is due to implementation details above. We also # know for sure that we *have* a remote use, otherwise we wouldn't # be here remote_user = IPrincipal(self.request, None) remote_user_langs = IUserPreferredLanguages(remote_user) if remote_user_langs is not EnglishUserPreferredLanguagesImpl: return remote_user_langs.getPreferredLanguages() # pylint:disable=too-many-function-args # Ok, see what the HTTP request can come up with. Note that we're # going to the Zope interface so that we don't get into an infinite # loop browser_request = IBrowserRequest(self.request) browser_langs = IModifiableUserPreferredLanguages(browser_request) return browser_langs.getPreferredLanguages() # pylint:disable=too-many-function-args
def test_has_key(self): environ = { 'PATH_INFO': '/', 'QUERY_STRING': 'lastName=Doe;country:list=Japan;country:list=Hungary', } request = Request(environ) zrequest = IBrowserRequest(request) assert_that(zrequest.has_key('lastName'), is_(True))
def test_form_parsing(self): environ = { 'PATH_INFO': '/', 'QUERY_STRING': 'lastName=Doe;country:list=Japan;country:list=Hungary', } request = Request(environ) zrequest = IBrowserRequest(request) assert_that(zrequest.form, { 'country': ['Japan', 'Hungary'], 'lastName': 'Doe' })
def __call__(self, value, system): """ :param value: The object returned from the view. Either a dictionary, or a context object. If a context object, will be available at the path ``options/here`` in the template. If a dictionary, its values are merged with those in `system`. """ __traceback_info__ = value, system try: system.update(value) except (TypeError, ValueError): # raise ValueError('renderer was passed non-dictionary as value') system['here'] = value # See plasTeX/Renderers/__init__.py for comments about how 'self' # is a problem request = None if 'request' in system and system['request'] is not None: request = IBrowserRequest(system['request']) system['request'] = request view = system['view'] # TODO: We can do better with this if view is None and request is not None: view = request system['view'] = request # We used to register macros, but now you should use # z3c.macro and the macro: expression type # if 'master' not in system: # XXX: FIXME: There must be a better way to handle this. # How did zope do it? (Acquisition?) # (Answer: Yes, basically. Every template was auto-loaded # and at a traversable location, usually also in the # acquisition path; pages traversed to the macros of the # template they wanted. We can do something similar though # traversal, we just need to update our templates.) # FIXME: Note use of nti.appserver package # master = get_renderer('nti.appserver:templates/master_email.pt').implementation() # system['master'] = master result = self.template.bind(view)(**system) return result
def test_url_traversal(self): request = Request.blank('http://foobar.com/folder/item') zrequest = IBrowserRequest(request) assert_that(str(zrequest.URL), is_('http://foobar.com/folder/item')) assert_that(zrequest.URL['-1'], is_('http://foobar.com/folder')) assert_that(zrequest.URL['-2'], is_('http://foobar.com')) assert_that( calling(zrequest.URL.__getitem__).with_args('-3'), raises(KeyError)) assert_that(zrequest.URL['0'], is_('http://foobar.com')) assert_that(zrequest.URL['1'], is_('http://foobar.com/folder')) assert_that(zrequest.URL['2'], is_('http://foobar.com/folder/item')) assert_that( calling(zrequest.URL.__getitem__).with_args('3'), raises(KeyError)) assert_that(zrequest.URL.get('0'), is_('http://foobar.com')) assert_that(zrequest.URL.get('1'), is_('http://foobar.com/folder')) assert_that(zrequest.URL.get('2'), is_('http://foobar.com/folder/item')) assert_that(zrequest.URL.get('3', 'none'), is_('none'))
def test_positional_args(self): request = Request.blank('/dataserver2/foo/bar.html') zrequest = IBrowserRequest(request) assert_that(zrequest.getPositionalArguments(), is_(()))
def test_adapts(self): request = Request.blank('/') zrequest = IBrowserRequest(request) assert_that(zrequest, verifiably_provides(IBrowserRequest)) # and it's still a valid pyramid request assert_that(zrequest, verifiably_provides(IRequest))
def __init__(self, context, request): request = IBrowserRequest(request) if not IDefaultBrowserLayer.providedBy(request): interface.alsoProvides(request, IDefaultBrowserLayer) # We lie super(resource, self).__init__(context, request)