def url(name, args={}, get={}, include_host=False): """Reverse the route in the routes file""" routes = app.userapp.routes.route.routes url = None for route_params in routes: if __match_route(name, route_params): url = __generate_url(route_params, args) if url is None: abort(500, 'Could not find route: ' + str(name)) get_string = '&'.join( [str(key) + '=' + str(val) for key, val in get.items()]) if get_string: url += '?' + get_string if include_host: host = app.userapp.settings.HOST port = app.userapp.settings.PORT if port != 80: host = f'{host}:{str(port)}' if app.userapp.settings.USING_SSL: host = f'https://{host}' else: host = f'http://{host}' url = host + url return url
def findOrFail(cls, pk): """Find model by primary key or throw 404.""" if pk is None: abort(404, 'Unable to find record.') model = cls.find(pk) if model is None: abort(404, 'Unable to find record.') return model
def get_resource_response(self, response, route): """Serve a resource.""" try: path = app.userapp.settings.RESOURCE_DIR + '/' + route.path if not os.path.exists(path) or not os.path.isfile(path): raise IOError return FileApp(path) except IOError: logging.warning('IOError while trying to access resource.') abort(404)
def check_csrf_on_post(request, response): """ Check the existence or validity of a csrf token in on post. Checks request.json if content type is application/json """ if request.method.upper() == 'POST': token = request.post('_token') if token is None and request.content_type == 'application/json': token = request.get_json('_token') if not request.session.check_csrf(token): errors.abort(401, 'Invalid form request.')
def view(self): """ This should be in charge of handling generating the view. If not implemented then throws http 500 error. """ logging.debug('Unimplemented view method!') errors.abort( 500, 'Unimplemented view method in: ' +\ self.__class__.__name__ )
def _get_model_or_404(self): """ Returns the model if model and model_id are set. Throws 404 not found if model cannot be found. Assigns the fetched model to model_name or the lowercase name of model. """ model = self.__class__.model model_id = self.__class__.model_id if model is None or model_id is None: return None passed_id = self.request.url_param(model_id) record = model.find(passed_id) if record is None: logging.error('Unable to get model \'%s\' with id \'%s\'', model.__tablename__, passed_id) errors.abort(404, 'Unable to find record.') return record
def get_route(self, request): """ Return the callable for this route. Returns Route instance. """ if app.userapp.settings.SERVE_PUBLIC: if request.path.startswith(app.userapp.settings.RESOURCE_URL): return ResourceRoute(request.path) found = None for route_params in self.routes: if not self.__match(route_params, request): continue found = Route(route_params, request.path) break if not found: abort(404) return found