def put(self): """Modify this plan.""" policy.check('update_plan', pecan.request.security_context) # make sure the plan exists before parsing the request handler = plan_handler.PlanHandler(pecan.request.security_context) handler.get(self._id) host_url = pecan.request.application_url.rstrip('/') if not pecan.request.body or len(pecan.request.body) < 1: raise exception.BadRequest(reason="No data.") if (pecan.request.content_type is not None and 'yaml' in pecan.request.content_type): data = init_yml_plan_by_version() updated_plan_yml = yamlutils.dump(yaml_content(handler.update( self._id, data.as_dict(objects.registry.Plan)))) else: data = init_json_plan_by_version() plan_obj = handler.update(self._id, data.as_dict(objects.registry.Plan)) updated_plan_yml = wsme_json.encode_result(plan.Plan.from_db_model( plan_obj, host_url), plan.Plan) pecan.response.status = 200 return updated_plan_yml
def get(self): """Return this plan.""" handler = plan_handler.PlanHandler(pecan.request.security_context) if pecan.request.accept is not None and 'yaml' in pecan.request.accept: plan_serialized = yamlutils.dump( yaml_content(handler.get(self._id))) else: plan_model = plan.Plan.from_db_model( handler.get(self._id), pecan.request.host_url) plan_serialized = wsme_json.encode_result(plan_model, plan.Plan) pecan.response.status = 200 return plan_serialized
def get_all(self): """Return all plans, based on the query provided.""" handler = plan_handler.PlanHandler(pecan.request.security_context) if pecan.request.accept is not None and 'yaml' in pecan.request.accept: plan_serialized = yamlutils.dump([yaml_content(obj) for obj in handler.get_all() if obj and obj.raw_content]) else: plan_serialized = wsme_json.encode_result( [plan.Plan.from_db_model(obj, pecan.request.host_url) for obj in handler.get_all()], wsme_types.ArrayType(plan.Plan)) pecan.response.status = 200 return plan_serialized
def get(self): """Return this plan.""" policy.check('show_plan', pecan.request.security_context) handler = plan_handler.PlanHandler(pecan.request.security_context) host_url = pecan.request.application_url.rstrip('/') if pecan.request.accept is not None and 'yaml' in pecan.request.accept: plan_serialized = yamlutils.dump( yaml_content(handler.get(self._id))) else: plan_model = plan.Plan.from_db_model( handler.get(self._id), host_url) plan_serialized = wsme_json.encode_result(plan_model, plan.Plan) pecan.response.status = 200 return plan_serialized
def __call__(self, data, context): response = context['request'].response response.content_type = 'application/json' if 'faultcode' in data: if 'orig_code' in data: response.status_code = data['orig_code'] elif data['faultcode'] == 'Client': response.status_code = 400 else: response.status_code = 500 return restjson.encode_error(None, data) obj = data['result'] if isinstance(obj, wsme.api.Response): response.status_code = obj.status_code if obj.error: return restjson.encode_error(None, obj.error) obj = obj.obj return restjson.encode_result(obj, data['datatype'])
def post(self): """Create a new plan.""" if not pecan.request.body or len(pecan.request.body) < 1: raise exception.BadRequest(reason="No data.") handler = plan_handler.PlanHandler(pecan.request.security_context) if (pecan.request.content_type is not None and 'yaml' in pecan.request.content_type): data = init_yml_plan_by_version() created_plan = yamlutils.dump(yaml_content(handler.create( data.as_dict(objects.registry.Plan)))) else: data = init_json_plan_by_version() plan_wsme = plan.Plan.from_db_model(handler.create( data.as_dict(objects.registry.Plan)), pecan.request.host_url) created_plan = wsme_json.encode_result(plan_wsme, plan.Plan) pecan.response.status = 201 return created_plan
def post(self): """Create a new plan.""" if not pecan.request.body or len(pecan.request.body) < 1: raise exception.BadRequest handler = plan_handler.PlanHandler(pecan.request.security_context) if (pecan.request.content_type is not None and 'yaml' in pecan.request.content_type): data = init_yml_plan_by_version() created_plan = yamlutils.dump(yaml_content(handler.create( data.as_dict(objects.registry.Plan)))) else: data = init_json_plan_by_version() plan_wsme = plan.Plan.from_db_model(handler.create( data.as_dict(objects.registry.Plan)), pecan.request.host_url) created_plan = wsme_json.encode_result(plan_wsme, plan.Plan) pecan.response.status = 201 return created_plan