def post(self): json_data = request.get_json() current_worker = get_jwt_identity() tool = Tool(tool_name=json_data['tool_name'], inventory=json_data['inventory'], location=json_data['location'], price=json_data['price'], worker_id=current_worker) tool.save() return tool.data(), HTTPStatus.CREATED
def get(self): tools = Tool.get_all_published() data = [] for tool in tools: data.append(tool.data()) return {'data': data}, HTTPStatus.OK
def get_tools_used_by_group(self, group_name): group = self._get_group_by_name(group_name) tools = self._client.get_software_used_by_group(group) tools_list = [] for t in tools: try: tool = Tool(t) tools_list.append(tool) except AttributeError: continue return tools_list
def get(self, tool_id): tool = Tool.get_by_id(tool_id=tool_id) if tool is None: return {'message': 'Tool not found'}, HTTPStatus.NOT_FOUND current_worker = get_jwt_identity() if tool.is_publish == False and tool.worker_id != current_worker: return {'message': 'Access is not allowed'}, HTTPStatus.FORBIDDEN return tool.data(), HTTPStatus.OK
def delete(self, sale_id): tool = Tool.get_by_id(sale_id=sale_id) if tool is None: return {'message': 'Tool not found'}, HTTPStatus.NOT_FOUND current_user = get_jwt_identity() if current_user != tool.user_id: return {'message': 'Access is not allowed'}, HTTPStatus.FORBIDDEN tool.delete() return {}, HTTPStatus.NO_CONTENT
def put(self, tool_id): json_data = request.get_json() tool = Tool.get_by_id(tool_id=tool_id) if tool is None: return {'message': 'Tool not found'}, HTTPStatus.NOT_FOUND current_user = get_jwt_identity() if current_user != tool.user_id: return {'message': 'Access is not allowed'}, HTTPStatus.FORBIDDEN tool.tool_name = json_data['tool_name'] tool.inventory = json_data['inventory'] tool.location = json_data['location'] tool.price = json_data['price'] tool.save() return tool.data(), HTTPStatus.OK
def get_tool_by_name(self, tool_name): return Tool(self._get_tool_by_name(tool_name))