本文整理汇总了Python中werkzeug.exceptions.NotFound方法的典型用法代码示例。如果您正苦于以下问题:Python exceptions.NotFound方法的具体用法?Python exceptions.NotFound怎么用?Python exceptions.NotFound使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类werkzeug.exceptions
的用法示例。
在下文中一共展示了exceptions.NotFound方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: match_url
# 需要导入模块: from werkzeug import exceptions [as 别名]
# 或者: from werkzeug.exceptions import NotFound [as 别名]
def match_url(url, method=None):
appctx = _app_ctx_stack.top
reqctx = _request_ctx_stack.top
if appctx is None:
raise RuntimeError('Attempted to match a URL without the '
'application context being pushed. This has to be '
'executed when application context is available.')
if reqctx is not None:
url_adapter = reqctx.url_adapter
else:
url_adapter = appctx.url_adapter
if url_adapter is None:
raise RuntimeError('Application was not able to create a URL '
'adapter for request independent URL matching. '
'You might be able to fix this by setting '
'the SERVER_NAME config variable.')
parsed_url = url_parse(url)
if parsed_url.netloc is not '' and \
parsed_url.netloc != url_adapter.server_name:
raise NotFound()
return url_adapter.match(parsed_url.path, method)
示例2: safe_join
# 需要导入模块: from werkzeug import exceptions [as 别名]
# 或者: from werkzeug.exceptions import NotFound [as 别名]
def safe_join(directory, filename):
"""Safely join `directory` and `filename`.
Example usage::
@app.route('/wiki/<path:filename>')
def wiki_page(filename):
filename = safe_join(app.config['WIKI_FOLDER'], filename)
with open(filename, 'rb') as fd:
content = fd.read() # Read and process the file content...
:param directory: the base directory.
:param filename: the untrusted filename relative to that directory.
:raises: :class:`~werkzeug.exceptions.NotFound` if the resulting path
would fall out of `directory`.
"""
filename = posixpath.normpath(filename)
for sep in _os_alt_seps:
if sep in filename:
raise NotFound()
if os.path.isabs(filename) or \
filename == '..' or \
filename.startswith('../'):
raise NotFound()
return os.path.join(directory, filename)
示例3: metadata_required
# 需要导入模块: from werkzeug import exceptions [as 别名]
# 或者: from werkzeug.exceptions import NotFound [as 别名]
def metadata_required(f):
"""
Decorate a view which has ``gordo_name`` as a url parameter and will
set ``g.metadata`` to that model's metadata
"""
@wraps(f)
def wrapper(*args: tuple, gordo_project: str, gordo_name: str, **kwargs: dict):
try:
g.metadata = load_metadata(directory=g.collection_dir, name=gordo_name)
except FileNotFoundError:
raise NotFound(f"No model found for '{gordo_name}'")
else:
return f(*args, **kwargs)
return wrapper
示例4: model_required
# 需要导入模块: from werkzeug import exceptions [as 别名]
# 或者: from werkzeug.exceptions import NotFound [as 别名]
def model_required(f):
"""
Decorate a view which has ``gordo_name`` as a url parameter and will
set ``g.model`` to be the loaded model and ``g.metadata``
to that model's metadata
"""
@wraps(f)
def wrapper(*args: tuple, gordo_project: str, gordo_name: str, **kwargs: dict):
try:
g.model = load_model(directory=g.collection_dir, name=gordo_name)
except FileNotFoundError:
raise NotFound(f"No such model found: '{gordo_name}'")
else:
# If the model was required, the metadata is also required.
return metadata_required(f)(
*args, gordo_project=gordo_project, gordo_name=gordo_name, **kwargs
)
return wrapper
示例5: post
# 需要导入模块: from werkzeug import exceptions [as 别名]
# 或者: from werkzeug.exceptions import NotFound [as 别名]
def post(self, name):
if not may(DELETE):
raise Forbidden()
try:
with current_app.storage.open(name) as item:
if not item.meta[COMPLETE] and not may(ADMIN):
error = 'Upload incomplete. Try again later.'
return render_template('error.html', heading=item.meta[FILENAME], body=error), 409
if item.meta[LOCKED] and not may(ADMIN):
raise Forbidden()
current_app.storage.remove(name)
except (OSError, IOError) as e:
if e.errno == errno.ENOENT:
raise NotFound()
raise
return redirect_next_referrer('bepasty.index')
示例6: post
# 需要导入模块: from werkzeug import exceptions [as 别名]
# 或者: from werkzeug.exceptions import NotFound [as 别名]
def post(self, name):
if self.REQUIRED_PERMISSION is not None and not may(self.REQUIRED_PERMISSION):
raise Forbidden()
try:
with current_app.storage.openwrite(name) as item:
if item.meta[self.KEY] == self.NEXT_VALUE:
error = '%s already is %r.' % (self.KEY, self.NEXT_VALUE)
elif not item.meta[COMPLETE]:
error = 'Upload incomplete. Try again later.'
else:
error = None
if error:
return render_template('error.html', heading=item.meta[FILENAME], body=error), 409
item.meta[self.KEY] = self.NEXT_VALUE
return redirect_next_referrer('bepasty.display', name=name)
except (OSError, IOError) as e:
if e.errno == errno.ENOENT:
raise NotFound()
raise
示例7: get
# 需要导入模块: from werkzeug import exceptions [as 别名]
# 或者: from werkzeug.exceptions import NotFound [as 别名]
def get(self, name):
if not may(CREATE):
raise Forbidden()
try:
item = current_app.storage.open(name)
except (OSError, IOError) as e:
if e.errno == errno.ENOENT:
return 'No file found.', 404
raise
if item.meta[COMPLETE]:
error = 'Upload complete. Cannot delete fileupload garbage.'
else:
error = None
if error:
return error, 409
try:
item = current_app.storage.remove(name)
except (OSError, IOError) as e:
if e.errno == errno.ENOENT:
raise NotFound()
raise
return 'Upload aborted'
示例8: get
# 需要导入模块: from werkzeug import exceptions [as 别名]
# 或者: from werkzeug.exceptions import NotFound [as 别名]
def get(self, report_uid):
"""Get a Testplan report (JSON) given it's uid."""
# report_uid will be used when looking up the report from a database.
report_path = os.path.abspath(
os.path.join(
app.config["DATA_PATH"], app.config["TESTPLAN_REPORT_NAME"]
)
)
if os.path.exists(report_path):
return send_from_directory(
directory=os.path.dirname(report_path),
filename=os.path.basename(report_path),
)
else:
raise exceptions.NotFound()
示例9: send_static_file
# 需要导入模块: from werkzeug import exceptions [as 别名]
# 或者: from werkzeug.exceptions import NotFound [as 别名]
def send_static_file(self, filename):
"""
Send static files from the static folder
in the current selected theme prior to the global static folder.
:param filename: static filename
:return: response object
"""
if self.config['MODE'] == 'api-only':
# if 'api-only' mode is set, we should not send static files
abort(404)
theme_static_folder = getattr(self, 'theme_static_folder', None)
if theme_static_folder:
try:
return send_from_directory(theme_static_folder, filename)
except NotFound:
pass
return super(CustomFlask, self).send_static_file(filename)
示例10: test_app
# 需要导入模块: from werkzeug import exceptions [as 别名]
# 或者: from werkzeug.exceptions import NotFound [as 别名]
def test_app():
# app's config should be loaded from instance/config.py
assert app.config['STORAGE_TYPE'] == 'file'
assert app.config['THEME'] == 'test'
with app.test_request_context('/'):
app.send_static_file('no-use.css')
app.send_static_file('no-use-2.css')
with raises(NotFound):
app.send_static_file('non-exists.css')
origin_mode = app.config['MODE']
app.config['MODE'] = 'api-only'
with app.test_request_context('/'):
with raises(NotFound):
app.send_static_file('no-use.css')
app.config['MODE'] = origin_mode
示例11: safe_join
# 需要导入模块: from werkzeug import exceptions [as 别名]
# 或者: from werkzeug.exceptions import NotFound [as 别名]
def safe_join(directory, filename):
"""Safely join `directory` and `filename`.
Example usage::
@app.route('/wiki/<path:filename>')
def wiki_page(filename):
filename = safe_join(app.config['WIKI_FOLDER'], filename)
with open(filename, 'rb') as fd:
content = fd.read() # Read and process the file content...
:param directory: the base directory.
:param filename: the untrusted filename relative to that directory.
:raises: :class:`~werkzeug.exceptions.NotFound` if the resulting path
would fall out of `directory`.
"""
filename = posixpath.normpath(filename)
for sep in _os_alt_seps:
if sep in filename:
raise NotFound()
if os.path.isabs(filename) or \
filename == '..' or \
filename.startswith('../'):
raise NotFound()
return os.path.join(directory, filename)
示例12: url
# 需要导入模块: from werkzeug import exceptions [as 别名]
# 或者: from werkzeug.exceptions import NotFound [as 别名]
def url(url: str, method: str):
"""Show details for a specific URL."""
try:
url_rule, params = (current_app.url_map.bind('localhost')
.match(url, method=method, return_rule=True))
except (NotFound, MethodNotAllowed) as e:
click.secho(str(e), fg='white', bg='red')
else:
headings = ('Method(s)', 'Rule', 'Params', 'Endpoint', 'View', 'Options')
print_table(headings,
[(_get_http_methods(url_rule),
url_rule.rule if url_rule.strict_slashes
else url_rule.rule + '[/]',
_format_dict(params),
url_rule.endpoint,
_get_rule_view(url_rule),
_format_rule_options(url_rule))],
['<' if i > 0 else '>' for i, col in enumerate(headings)],
primary_column_idx=1)
示例13: test_404_on_lookup_error
# 需要导入模块: from werkzeug import exceptions [as 别名]
# 或者: from werkzeug.exceptions import NotFound [as 别名]
def test_404_on_lookup_error(self, user, role):
from ._bundles.vendor_one.models import OneUser, OneRole
@param_converter(id=OneUser, one_role_id=OneRole)
def method(one_user, one_role):
assert False
with pytest.raises(NotFound):
method(id=0, one_role_id=role.id)
@param_converter(id=OneUser, one_role_id=OneRole)
def method(one_user, one_role):
assert False
with pytest.raises(NotFound):
method(id=user.id, one_role_id=0)
示例14: get_item_or_404
# 需要导入模块: from werkzeug import exceptions [as 别名]
# 或者: from werkzeug.exceptions import NotFound [as 别名]
def get_item_or_404(self, id, **kwargs):
"""Get an item by ID; raise a 404 if it not found.
This will get an item by ID per `get_item` below. If no item is found,
it will rethrow the `NoResultFound` exception as an HTTP 404.
:param id: The item ID.
:return: The item corresponding to the ID.
:rtype: object
"""
try:
item = self.get_item(id, **kwargs)
except NoResultFound as e:
raise NotFound() from e
return item
示例15: delete_user
# 需要导入模块: from werkzeug import exceptions [as 别名]
# 或者: from werkzeug.exceptions import NotFound [as 别名]
def delete_user(musicbrainz_id):
""" Delete a user from ListenBrainz completely.
First, drops the user's influx measurement and then deletes the user from the
database.
Args:
musicbrainz_id (str): the MusicBrainz ID of the user
Raises:
NotFound if user isn't present in the database
"""
user = _get_user(musicbrainz_id)
_influx.delete(user.musicbrainz_id)
publish_data_to_queue(
data={
'type': 'delete.user',
'musicbrainz_id': musicbrainz_id,
},
exchange=current_app.config['BIGQUERY_EXCHANGE'],
queue=current_app.config['BIGQUERY_QUEUE'],
error_msg='Could not put user %s into queue for deletion, please try again later' % musicbrainz_id,
)
db_user.delete(user.id)