def cached_call_controller(controller, remainder, params): req = request._current_obj() if self.key: key_dict = req.args_params if self.key != NoDefault: if isinstance(self.key, (list, tuple)): key_dict = dict((k, key_dict[k]) for k in key_dict) else: key_dict = {self.key: key_dict[self.key]} else: key_dict = {} namespace, cache_key = create_cache_key( func, key_dict, req.controller_state.controller) req._fast_setattr('caching', Bunch(namespace=namespace, key=cache_key)) return _cached_call(next_caller, (controller, remainder, params), {}, namespace, cache_key, expire=self.expire, type=self.type, starttime=starttime, cache_headers=self.cache_headers, cache_response=self.cache_response, cache_extra_args=self.beaker_options)
def _after_cursor_execute(conn, cursor, stmt, params, context, execmany): stop_timer = time.time() try: req = request._current_obj() except: req = None if req is not None: with lock: engines = getattr(app_globals, 'tgdb_sqla_engines', {}) engines[id(conn.engine)] = weakref.ref(conn.engine) setattr(app_globals, 'tgdb_sqla_engines', engines) queries = getattr(req, 'tgdb_sqla_queries', []) queries.append({ 'engine_id': id(conn.engine), 'duration': (stop_timer - conn.tgdb_start_timer) * 1000, 'statement': stmt, 'parameters': params, 'context': context }) req.tgdb_sqla_queries = queries delattr(conn, 'tgdb_start_timer')
def after_cursor_next(self, cursor): spent = (time.time() - cursor.tgdb_ming_timer) * 1000 try: req = request._current_obj() except Exception: req = None if req is not None: try: active_cursors = req.tgdb_ming_cursors except Exception: active_cursors = req.tgdb_ming_cursors = {} info = active_cursors.setdefault(id(cursor), { 'duration': 0, 'command': '', 'collection': '', 'params': ''}) info['duration'] += spent if not hasattr(cursor, 'tgdb_action'): return info['command'] = cursor.tgdb_action info['collection'] = cursor.tgdb_class info['params'] = cursor.tgdb_args
def replace_template_hook(remainder, params, output): req = request._current_obj() try: dispatch_state = req._controller_state except: dispatch_state = req.controller_state try: if req.validation['exception']: controller = req.validation['error_handler'] else: controller = dispatch_state.method except (AttributeError, KeyError): controller = dispatch_state.method decoration = Decoration.get_decoration(controller) if 'tg.locals' in req.environ: content_type, engine, template, exclude_names = decoration.lookup_template_engine(req.environ['tg.locals'])[:4] else: content_type, engine, template, exclude_names = decoration.lookup_template_engine(req)[:4] replaced_template = config._pluggable_templates_replacements.get(template) if replaced_template: override_template(decoration.controller, replaced_template)
def replace_template_hook(remainder, params, output): req = request._current_obj() try: dispatch_state = req._controller_state except: dispatch_state = req.controller_state try: if req.validation['exception']: controller = req.validation['error_handler'] else: controller = dispatch_state.method except (AttributeError, KeyError): controller = dispatch_state.method decoration = Decoration.get_decoration(controller) if 'tg.locals' in req.environ: content_type, engine, template, exclude_names = decoration.lookup_template_engine( req.environ['tg.locals'])[:4] else: content_type, engine, template, exclude_names = decoration.lookup_template_engine( req)[:4] replaced_template = config._pluggable_templates_replacements.get(template) if replaced_template: override_template(decoration.controller, replaced_template)
def after_cursor_next(self, cursor): spent = (time.time() - cursor.tgdb_ming_timer) * 1000 try: req = request._current_obj() except Exception: req = None if req is not None: try: active_cursors = req.tgdb_ming_cursors except Exception: active_cursors = req.tgdb_ming_cursors = {} info = active_cursors.setdefault(id(cursor), { 'duration': 0, 'command': '', 'collection': '', 'params': '' }) info['duration'] += spent if not hasattr(cursor, 'tgdb_action'): return info['command'] = cursor.tgdb_action info['collection'] = cursor.tgdb_class info['params'] = cursor.tgdb_args
def pop_payload(self): # First try fetching it from the request req = request._current_obj() payload = req.environ.get('webflash.payload', {}) if not payload: payload = req.cookies.get(self.cookie_name, {}) if payload: payload = json.loads(url_unquote(payload)) if 'webflash.deleted_cookie' not in req.environ: response.delete_cookie(self.cookie_name) req.environ['webflash.delete_cookie'] = True return payload or {}
def _check_authorization(self, *args, **kwargs): req = request._current_obj() try: self.predicate.check_authorization(req.environ) except NotAuthorizedError as e: reason = unicode_text(e) if req.environ.get('repoze.who.identity'): # The user is authenticated. code = 403 else: # The user is not authenticated. code = 401 response.status = code return self.denial_handler(reason)
def on_after_render_call(template_engine, template_name, template_vars, kwargs): now = time.time() req = request._current_obj() try: render_calls = req.tgdb_render_calls except: render_calls = req.tgdb_render_calls = {} template_key = '%s:%s' % (template_engine, template_name) template_info = render_calls.setdefault(template_key, {'time':0, 'count':0, 'template_name':template_name, 'template_engine':template_engine}) template_info['time'] += (now - req.tgdb_render_call_start_time) * 1000 template_info['count'] += 1
def replace_template_hook(remainder, params, output): req = request._current_obj() try: dispatch_state = req._controller_state except: dispatch_state = req.controller_state decoration = Decoration.get_decoration(dispatch_state.method) if 'tg.locals' in req.environ: content_type, engine, template, exclude_names = decoration.lookup_template_engine(req.environ['tg.locals'])[:4] else: content_type, engine, template, exclude_names = decoration.lookup_template_engine(req)[:4] replaced_template = config._pluggable_templates_replacements.get(template) if replaced_template: override_template(dispatch_state.method, replaced_template)
def replace_template_hook(remainder, params, output): req = request._current_obj() try: dispatch_state = req._controller_state except: dispatch_state = req.controller_state decoration = Decoration.get_decoration(dispatch_state.method) if 'tg.locals' in req.environ: content_type, engine, template, exclude_names = decoration.lookup_template_engine( req.environ['tg.locals'])[:4] else: content_type, engine, template, exclude_names = decoration.lookup_template_engine( req)[:4] replaced_template = config._pluggable_templates_replacements.get(template) if replaced_template: override_template(dispatch_state.method, replaced_template)
def on_after_render(response, *args, **kw): now = time.time() req = request._current_obj() req.tgdb_render_info = response req.tgdb_render_time = (now - req.tgdb_render_start_time) * 1000 try: req.tgdb_total_time = (now - req.tgdb_call_start_time) * 1000 except: req.tgdb_total_time = -1 req.tgdb_call_start_time = -1 req.tgdb_call_time = -1 req.tgdb_profiling_function_calls = [] req.tgdb_profiling_stats = [] try: req.tgdb_render_call_start_time req.tgdb_render_calls except: req.tgdb_render_calls = {} req.tgdb_render_call_start_time = -1
def cached_call_controller(controller, remainder, params): req = request._current_obj() if self.key: key_dict = req.args_params if self.key != NoDefault: if isinstance(self.key, (list, tuple)): key_dict = dict((k, key_dict[k]) for k in key_dict) else: key_dict = {self.key: key_dict[self.key]} else: key_dict = {} namespace, cache_key = create_cache_key(func, key_dict, req.controller_state.controller) req._fast_setattr('caching', Bunch(namespace=namespace, key=cache_key)) return _cached_call(next_caller, (controller, remainder, params), {}, namespace, cache_key, expire=self.expire, type=self.type, starttime=starttime, cache_headers=self.cache_headers, cache_response=self.cache_response, cache_extra_args=self.beaker_options)
def with_trailing_slash(remainder, params): """This decorator allows you to ensure that the URL ends in "/". The decorator accomplish this by redirecting to the correct URL. :Usage: You use this decorator as follows:: class MyController(object): @with_trailing_slash @expose() def sample(self, *args): return "found sample" In the above example http://localhost:8080/sample redirects to http://localhost:8080/sample/ In addition, the URL http://localhost:8080/sample/1 redirects to http://localhost:8080/sample/1/ """ req = request._current_obj() if (req.method == 'GET' and not(req.path.endswith('/')) and not(req._response_type) and len(req.params)==0): redirect(request.url+'/', redirect_with=HTTPMovedPermanently)