def update_context(self, context): context.setdefault('settings', settings._current_obj()) if registry_has_object(user): context.setdefault('user', user._current_obj()) else: context.setdefault('user', None) if registry_has_object(rg): context.setdefault('rg', rg) else: context.setdefault('rg', None)
def prefix_relative_url(url): """ If the url given is an absolute url of any of the following forms: http(s)://example.com/the-page /the-page / then the url will be returned as-is. But if it is a relative url: the-page or an empty string "", then it will be prefixed with forward slash (/) as well as the script-name of the current environment if applicable. Note: this function checks for the presence of the current environment in a safe way and can therefore be used both inside and outside of a request context. """ if url.startswith('http') or url.startswith('/'): return url if registry_has_object(rg): script_name = rg.request.script_root if script_name: return '/%s/%s' % (script_name.lstrip('/'), url) return '/%s' % url
def builtin_import(self, name, globals={}, locals={}, fromlist=[], level=default_import_level): mod = self._builtin_import(name, globals, locals, fromlist, level) # for module reloading purposes in visitmods(), we need to keep track # of what application imported a module. But we only need to do that # for modules that are in BlazeWeb applications or are BW components if registry_has_object(ag) and registry_has_object(settings): # is this module part of the main or supporting app? toplevel = name.split('.')[0] if toplevel in listapps() or toplevel in list_component_packages(): mod._blazeweb_hierarchy_last_imported_by = id(ag.app) return mod
def _user(self): """Lazy initial creation of user object""" if '_user_inst' in self.__dict__: # if we get called a second time, then # there is already a User instance behind the "user" SOP, that # means something called user._curr_obj() and got ahold of this # UserProxy instance. The problem is, the code is likely going # to continue calling this instance, instead of the "user" SOP, # therefore, we need to continue proxying the real User instance # hoping the calling code lets go of us eventually # If we continue, didn't do this the code below would try to pop the # User instance off the SOP and throw a TypeError return self.__dict__['_user_inst'] # load user instance from the beaker session if possible if registry_has_object(rg) and rg.session is not None: if '__blazeweb_user' in rg.session: user_inst = rg.session['__blazeweb_user'] else: user_inst = self._new_user_instance() rg.session['__blazeweb_user'] = user_inst else: user_inst = self._new_user_instance() # save the user instance in case we get called again self.__dict__['_user_inst'] = user_inst # replace underlying object on the user global variable to be the real # user instance. The user has been accessed, so no need to proxy # anymore. After we replace ourselves, future calls to the global # "user" SOP variable will go to the real user instance, instead of # this UserProxy instance. if registry_has_object(guser): guser_cur_obj = guser._current_obj() if not isinstance(guser_cur_obj, UserProxy): raise TypeError( 'UserProxy tried to unregister a class of type: %s' % guser_cur_obj) rg.environ['paste.registry'].register(guser, user_inst) return user_inst
def handle_exception(self, e): """Default exception handling that kicks in when an exception occours that is not caught. In debug mode the exception will be re-raised immediately, otherwise it is logged an the handler for an 500 internal server error is used. If no such handler exists, a default 500 internal server error message is displayed. .. versionadded: 0.3 """ log.error('exception encountered: %s' % exception_with_context()) if not self.settings.exception_handling: raise if 'email' in self.settings.exception_handling: try: mail_programmers('exception encountered', exception_with_context()) except Exception: log.exception('exception when trying to email exception') if 'handle' in self.settings.exception_handling: if registry_has_object(rg) and rg.exception_handler: return rg.exception_handler(e) else: endpoint = self.settings.error_docs.get(500) if endpoint is not None: log.debug('handling exception with error doc endpoint %s' % endpoint) try: return self.response_cycle(endpoint, {}, error_doc_code=500) except HTTPException as httpe: log.debug( 'error doc endpoint %s raised HTTPException: %s', endpoint, httpe) except Exception: log.exception( 'error doc endpoint %s raised exception:', endpoint) # turn the exception into a 500 server response log.debug('handling exception with generic 500 response') return InternalServerError() raise
def bind_to_context(self): if registry_has_object(rg): rg.request = self
def get_scoped_session_class(self): if settings.components.sqlalchemy.use_split_sessions and not registry_has_object(rg): return self.AppLevelSession return self.Session