def details(self, company_id, *args, **kwargs): try: company = CompanyAlchemy.get_validated_company(company_id) except NoResultFound: raise HTTPNotFound() except Exception as exc: logging.getLogger(__name__).log(logging.ERROR, exc) raise HTTPNotFound() else: return dict(company=company)
def details(self, offer_id, *args, **kwargs): try: job = JobAlchemy.get_job_offer(offer_id) except NoResultFound: raise HTTPNotFound() except Exception as exc: logging.getLogger(__name__).log(logging.ERROR, exc) raise HTTPNotFound() else: return dict(job=job, sources=SOURCES)
def _lookup(self, model_name, *args): model_name = model_name[:-1] try: model = self.config.models[model_name] except KeyError: raise HTTPNotFound().exception config = self.config.lookup_controller_config(model_name) controller = self._make_controller(config, self.session) return controller, args
def _find_object(obj, remainder, notfound_handlers): parent = None while True: if obj is None: raise HTTPNotFound().exception _check_controller_auth(obj) if _iscontroller(obj): return obj, parent, remainder if not remainder or (len(remainder) == 1 and remainder[0] == ''): if isinstance(remainder, tuple): remainder = list(remainder) index = getattr(obj, 'index', None) if _iscontroller(index): return index, obj, remainder default, lookup = _get_notfound_handlers(obj) if default: notfound_handlers.append(('default', default, obj, remainder)) if lookup and remainder and not (len(remainder) == 1 and (remainder[0] == '')): notfound_handlers.append(('lookup', lookup, obj, remainder)) #what causes this condition? if not remainder: raise HTTPNotFound().exception parent = obj try: obj = getattr(obj, remainder[0].encode("utf-8"), None) remainder = remainder[1:] # this can happen when parts of the path # contain non-ascii-characters. # If that's the case, we throw HTTPNotFound # and let a potential default-handler deal # with the issue except UnicodeEncodeError: raise HTTPNotFound().exception
class RootController(TGController): CACHE_EXPIRE = 7 * 86400 # 7 Days depotmiddleware = DepotMiddleware(HTTPNotFound(), '/pages') @expose() def _default(self, page=None, *args, **kw): page = model.FlatPage.by_slug(page) if page is None: abort(404, 'Page not found') permission = page.required_permission if permission and permission != 'public': if permission == 'not_anonymous': predicate = predicates.not_anonymous() else: predicate = predicates.has_permission(permission) if not predicate.is_met(request.environ): abort(403, 'Forbidden') try: userid = request.identity['user'].user_id except: userid = None override_template(RootController._default, page.template) hooks.notify('flatpages.before_render_page', (page, )) return dict(page=page, tg_cache={ 'expire': self.CACHE_EXPIRE, 'key': '%s-%s-%s' % (page.slug, page.updated_at, userid) }) @expose(content_type='text/html') def flatfiles(self, *args, **kwargs): r = Request.blank('/pages/flatfiles/' + '/'.join(args)) return r.get_response(self.depotmiddleware) @cached_property def manage(self): return FlatPagesAdminController()
def _lookup(self, model_name, *args): model_name = model_name[:-1] try: model = self.config.models[model_name] except KeyError: raise HTTPNotFound() try: controller = self.controllers_cache[model_name] except KeyError: config = self.config.lookup_controller_config(model_name) menu_items = None if self.config.include_left_menu: menu_items = self.config.models controller = self.controllers_cache[ model_name] = self.make_controller(config, self.session, left_menu_items=menu_items) return controller, args
def _find_restful_dispatch(obj, parent, remainder): _check_controller_auth(obj) if not inspect.isclass(obj) and not isinstance(obj, RestController): return obj, remainder request_method = method = request.method.lower() #conventional hack for handling methods which are not supported by most browsers params = request.params if '_method' in params: if params['_method']: method = params['_method'].lower() if remainder and remainder[-1] == '': remainder = remainder[:-1] if remainder: remainder_len = len(remainder) #dispatch is finished, and we are where we need to be if remainder_len <= 2 and remainder[-1] in ['new', 'edit']: if method == 'get': method = remainder[-1] if method == 'edit' and len(remainder) <= 2: remainder = remainder[:-1] if method == 'new' and len(remainder) == 1: remainder = remainder[:-1] elif remainder_len <= 2 and remainder[-1] == 'delete': method = 'delete' if remainder_len <= 2: remainder = remainder[:-1] #handles put and post for parental relations elif remainder_len >= 2 and (method == 'post' or method == 'put') and hasattr(obj, 'get_one'): func = getattr(obj, 'get_one') arg_len = len(inspect.getargspec(func)[0]) - 1 new_remainder = remainder[arg_len:] if len(new_remainder) > 0 and hasattr(obj, new_remainder[0]): return _find_restful_dispatch(*_find_object( getattr(obj, new_remainder[0]), new_remainder[1:], [])) elif hasattr(obj, remainder[0]): return _find_restful_dispatch(*_find_object( getattr(obj, remainder[0]), remainder[1:], [])) else: raise HTTPNotFound().exception #handles new and edit for parental relations if remainder and hasattr( obj, remainder[0]) and remainder[0] not in ['new', 'edit']: #revert the dispatch back to object_dispatch if inspect.isclass(obj): obj = obj() return _find_restful_dispatch( *_find_object(getattr(obj, remainder[0]), remainder[1:], [])) #support for get_all and get_one methods if not remainder and method == 'get' and hasattr( obj, 'get_all') and hasattr( obj.get_all, 'decoration') and obj.get_all.decoration.exposed: method = 'get_all' if len(remainder) > 0 and method == 'get' and hasattr( obj, 'get_one') and hasattr( obj.get_all, 'decoration') and obj.get_one.decoration.exposed: if len(remainder) == 1: method = 'get_one' else: func = getattr(obj, 'get_one') arg_len = len(inspect.getargspec(func)[0]) - 1 remainder = remainder[arg_len:] if len(remainder) > 0 and hasattr(obj, remainder[0]): return _find_restful_dispatch(*_find_object( getattr(obj, remainder[0]), remainder[1:], [])) elif hasattr(obj, 'get_one') and inspect.getargspec( obj.get_one)[1]: method = 'get_one' else: raise HTTPNotFound().exception #support for get_delete and post_delete methods if request_method == 'get' and method == 'delete' and hasattr( obj, 'get_delete') and hasattr( obj.get_delete, 'decoration') and obj.get_delete.decoration.exposed: method = 'get_delete' if request_method == 'post' and method == 'delete' and hasattr( obj, 'post_delete') and hasattr( obj.post_delete, 'decoration') and obj.post_delete.decoration.exposed: method = 'post_delete' if hasattr(obj, method): possible_rest_method = getattr(obj, method) if hasattr(possible_rest_method, 'decoration') and possible_rest_method.decoration.exposed: if inspect.isclass(obj): obj = obj() #attach the parent class so the inner class has access to it's members obj.parent = parent obj = getattr(obj, method) else: raise HTTPNotFound().exception elif isinstance(obj, RestController): raise HTTPNotFound().exception return obj, remainder
This module also contains the standard ObjectDispatch class which provides the ordinary TurboGears mechanism. """ from urllib import url2pathname from inspect import ismethod, isclass, getargspec from warnings import warn import pylons import mimetypes from pylons.controllers import WSGIController from tg.exceptions import HTTPNotFound from tg.i18n import setup_i18n from tg.util import odict HTTPNotFound = HTTPNotFound().exception class DispatchState(object): """ This class keeps around all the pertainent info for the state of the dispatch as it traverses through the tree. This allows us to attach things like routing args and to keep track of the path the controller takes along the system. """ def __init__(self, url_path, params): self.url_path = url_path self.controller_path = odict() self.routing_args = {} self.method = None self.remainder = None
def new(self, *args, **kwargs): raise HTTPNotFound()
def post(self, *args, **kw): raise HTTPNotFound()
def post_delete(self, *args, **kw): raise HTTPNotFound()