本文整理匯總了Python中pecan.abort方法的典型用法代碼示例。如果您正苦於以下問題:Python pecan.abort方法的具體用法?Python pecan.abort怎麽用?Python pecan.abort使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pecan
的用法示例。
在下文中一共展示了pecan.abort方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: enforce
# 需要導入模塊: import pecan [as 別名]
# 或者: from pecan import abort [as 別名]
def enforce(rule, target):
"""Return the user and project the request should be limited to.
:param rule: The rule name
:param target: The target to enforce on.
"""
creds = pecan.request.auth_helper.get_auth_info(pecan.request)
if not isinstance(target, dict):
if hasattr(target, "jsonify"):
target = target.jsonify()
else:
target = target.__dict__
# Flatten dict
target = dict(flatten_dict_to_keypairs(d=target, separator='.'))
if not pecan.request.policy_enforcer.enforce(rule, target, creds):
abort(403)
示例2: deserialize
# 需要導入模塊: import pecan [as 別名]
# 或者: from pecan import abort [as 別名]
def deserialize(expected_content_types=None):
if expected_content_types is None:
expected_content_types = ("application/json", )
mime_type, options = werkzeug.http.parse_options_header(
pecan.request.headers.get('Content-Type'))
if mime_type not in expected_content_types:
abort(415)
try:
params = json.load(pecan.request.body_file)
except Exception as e:
details = rest_exceptions.UnableToDecodeBody(e,
pecan.request.body_file)
LOG.warning(details.jsonify())
abort(400, details)
return params
示例3: patch
# 需要導入模塊: import pecan [as 別名]
# 或者: from pecan import abort [as 別名]
def patch(self):
ap = pecan.request.indexer.get_archive_policy(self.archive_policy)
if not ap:
abort(404, six.text_type(
indexer.NoSuchArchivePolicy(self.archive_policy)))
enforce("update archive policy", ap)
body = deserialize_and_validate(voluptuous.Schema({
voluptuous.Required("definition"): ArchivePolicyDefinitionSchema,
}))
# Validate the data
try:
ap_items = [archive_policy.ArchivePolicyItem(**item) for item in
body['definition']]
except ValueError as e:
abort(400, six.text_type(e))
try:
return pecan.request.indexer.update_archive_policy(
self.archive_policy, ap_items)
except indexer.UnsupportedArchivePolicyChange as e:
abort(400, six.text_type(e))
示例4: _lookup
# 需要導入模塊: import pecan [as 別名]
# 或者: from pecan import abort [as 別名]
def _lookup(self, name, *remainder):
m = pecan.request.indexer.list_metrics(
details=True,
attribute_filter={"and": [
{"=": {"name": name}},
{"=": {"resource_id": self.resource_id}},
]})
if m:
return MetricController(m[0]), remainder
resource = pecan.request.indexer.get_resource(self.resource_type,
self.resource_id)
if resource:
abort(404, six.text_type(indexer.NoSuchMetric(name)))
else:
abort(404, six.text_type(indexer.NoSuchResource(self.resource_id)))
示例5: post
# 需要導入模塊: import pecan [as 別名]
# 或者: from pecan import abort [as 別名]
def post(self):
resource = pecan.request.indexer.get_resource(
self.resource_type, self.resource_id)
if not resource:
abort(404, six.text_type(indexer.NoSuchResource(self.resource_id)))
enforce("update resource", resource)
metrics = deserialize_and_validate(MetricsSchema)
try:
r = pecan.request.indexer.update_resource(
self.resource_type,
self.resource_id,
metrics=metrics,
append_metrics=True,
create_revision=False)
except (indexer.NoSuchMetric,
indexer.NoSuchArchivePolicy,
ValueError) as e:
abort(400, six.text_type(e))
except indexer.NamedMetricAlreadyExists as e:
abort(409, six.text_type(e))
except indexer.NoSuchResource as e:
abort(404, six.text_type(e))
return r.metrics
示例6: _handle_post
# 需要導入模塊: import pecan [as 別名]
# 或者: from pecan import abort [as 別名]
def _handle_post(self, method, remainder):
'''
Routes ``POST`` actions to the appropriate controller.
'''
# route to a post_all or get if no additional parts are available
if not remainder or remainder == ['']:
controller = self._find_controller('post_all', 'post')
if controller:
return controller, []
pecan.abort(405)
controller = getattr(self, remainder[0], None)
if controller and not inspect.ismethod(controller):
return pecan.routing.lookup_controller(controller, remainder[1:])
# finally, check for the regular post_one/post requests
controller = self._find_controller('post_one', 'post')
if controller:
return controller, remainder
pecan.abort(405)
示例7: _handle_patch
# 需要導入模塊: import pecan [as 別名]
# 或者: from pecan import abort [as 別名]
def _handle_patch(self, method, remainder):
'''
Routes ``PATCH`` actions to the appropriate controller.
'''
# route to a patch_all or get if no additional parts are available
if not remainder or remainder == ['']:
controller = self._find_controller('patch_all', 'patch')
if controller:
return controller, []
pecan.abort(405)
controller = getattr(self, remainder[0], None)
if controller and not inspect.ismethod(controller):
return pecan.routing.lookup_controller(controller, remainder[1:])
# finally, check for the regular patch_one/patch requests
controller = self._find_controller('patch_one', 'patch')
if controller:
return controller, remainder
pecan.abort(405)
示例8: _handle_put
# 需要導入模塊: import pecan [as 別名]
# 或者: from pecan import abort [as 別名]
def _handle_put(self, method, remainder):
'''
Routes ``PUT`` actions to the appropriate controller.
'''
# route to a put_all or get if no additional parts are available
if not remainder or remainder == ['']:
controller = self._find_controller('put_all', 'put')
if controller:
return controller, []
pecan.abort(405)
controller = getattr(self, remainder[0], None)
if controller and not inspect.ismethod(controller):
return pecan.routing.lookup_controller(controller, remainder[1:])
# finally, check for the regular put_one/put requests
controller = self._find_controller('put_one', 'put')
if controller:
return controller, remainder
pecan.abort(405)
示例9: _handle_delete
# 需要導入模塊: import pecan [as 別名]
# 或者: from pecan import abort [as 別名]
def _handle_delete(self, method, remainder):
'''
Routes ``DELETE`` actions to the appropriate controller.
'''
# route to a delete_all or get if no additional parts are available
if not remainder or remainder == ['']:
controller = self._find_controller('delete_all', 'delete')
if controller:
return controller, []
pecan.abort(405)
controller = getattr(self, remainder[0], None)
if controller and not inspect.ismethod(controller):
return pecan.routing.lookup_controller(controller, remainder[1:])
# finally, check for the regular delete_one/delete requests
controller = self._find_controller('delete_one', 'delete')
if controller:
return controller, remainder
pecan.abort(405)
示例10: on_post
# 需要導入模塊: import pecan [as 別名]
# 或者: from pecan import abort [as 別名]
def on_post(self, external_project_id, **kwargs):
"""Handles creation of secret metadatum."""
data = api.load_body(pecan.request, validator=self.metadatum_validator)
key = data.get('key')
value = data.get('value')
metadata = self.user_meta_repo.get_metadata_for_secret(self.secret.id)
if key in metadata:
pecan.abort(409, u._('Conflict. Key in request is already in the '
'secret metadata'))
LOG.debug('Start secret metadatum on_post...%s', metadata)
self.user_meta_repo.create_replace_user_metadatum(self.secret.id,
key, value)
url = hrefs.convert_user_meta_to_href(self.secret.id)
LOG.debug('URI to secret metadata is %s', url)
pecan.response.status = 201
return {'metadata_ref': url + "/%s {key: %s, value:%s}" % (key,
key,
value)}
示例11: handle_exceptions
# 需要導入模塊: import pecan [as 別名]
# 或者: from pecan import abort [as 別名]
def handle_exceptions(operation_name=u._('System')):
"""Decorator handling generic exceptions from REST methods."""
def exceptions_decorator(fn):
def handler(inst, *args, **kwargs):
try:
return fn(inst, *args, **kwargs)
except exc.HTTPError:
LOG.exception(u._LE('Webob error seen'))
raise # Already converted to Webob exception, just reraise
except Exception as e:
# In case intervening modules have disabled logging.
LOG.logger.disabled = False
status, message = api.generate_safe_exception_message(
operation_name, e)
LOG.exception(message)
pecan.abort(status, message)
return handler
return exceptions_decorator
示例12: set_preferred
# 需要導入模塊: import pecan [as 別名]
# 或者: from pecan import abort [as 別名]
def set_preferred(self, external_project_id):
if pecan.request.method != 'POST':
pecan.abort(405)
LOG.debug("== Setting preferred CA %s for project %s",
self.ca.id, external_project_id)
project_model = res.get_or_create_project(external_project_id)
(project_ca, __offset, __limit, __total) = (
self.project_ca_repo.get_by_create_date(
project_id=project_model.id,
ca_id=self.ca.id,
suppress_exception=True))
if not project_ca:
_requested_preferred_ca_not_a_project_ca()
self.preferred_ca_repo.create_or_update_by_project_id(
project_model.id, self.ca.id)
示例13: _lookup
# 需要導入模塊: import pecan [as 別名]
# 或者: from pecan import abort [as 別名]
def _lookup(self, sub_resource, *remainder):
if sub_resource == 'acl':
return acls.SecretACLsController(self.secret), remainder
elif sub_resource == 'metadata':
if len(remainder) == 0 or remainder == ('',):
return secretmeta.SecretMetadataController(self.secret), \
remainder
else:
request_method = pecan.request.method
allowed_methods = ['GET', 'PUT', 'DELETE']
if request_method in allowed_methods:
return secretmeta.SecretMetadatumController(self.secret), \
remainder
else:
# methods cannot be handled at controller level
pecan.abort(405)
else:
# only 'acl' and 'metadata' as sub-resource is supported
pecan.abort(405)
示例14: abort
# 需要導入模塊: import pecan [as 別名]
# 或者: from pecan import abort [as 別名]
def abort(status_code, detail=''):
"""Like pecan.abort, but make sure detail is a string."""
if status_code == 404 and not detail:
raise RuntimeError("http code 404 must have 'detail' set")
if isinstance(detail, voluptuous.Invalid):
detail = {
'cause': 'Invalid input',
'reason': six.text_type(detail),
'detail': [six.text_type(path) for path in detail.path],
}
elif isinstance(detail, Exception):
detail = detail.jsonify()
LOG.debug("Aborting request. Code [%s]. Details [%s]", status_code, detail)
return pecan.abort(status_code, detail)
示例15: validate
# 需要導入模塊: import pecan [as 別名]
# 或者: from pecan import abort [as 別名]
def validate(schema, data, required=True):
try:
return voluptuous.Schema(schema, required=required)(data)
except voluptuous.Invalid as e:
abort(400, e)