def getTool(): try: result = None result = [tool.serialize() for tool in ToolDAO.dbRead()] return jsonify(result), 200 except Exception as e: print(e) return "Server error", 500
def getToolById(id): try: tool = ToolDAO.dbGet(id) if tool: return jsonify(tool.serialize()), 200 else: return jsonify(tool), 200 except Exception as e: print(e) return "Server error", 500
def getToolByTribulationID(id): try: result = None result = [ tool.serialize() for tool in ToolDAO.dbGetByTribulationID(id) ] return jsonify(result), 200 except Exception as e: print(e) return "Server error", 500
def searchTool(): try: start = request.args.get('start') end = request.args.get('end') status = request.args.get('status') result = None result = [ tool.serialize() for tool in ToolDAO.dbSearch(start, end, status) ] return jsonify(result), 200 except Exception as e: print(e) return "Server error", 500
def delete(id): try: result = ToolDAO.dbDelete(id) if result > 0: user_id = request.headers['UserID'] log = LogDTO(user_id=user_id, action="delete tool id: " + str(id), date_create=datetime.now()) LogDAO.dbCreate(log) return jsonify(result), 200 return "Can't delete", 403 except Exception as e: if "404 Not Found" in str(e): return "404 Not Found", 404 else: return "Server error", 500
def index(): if not request.is_json: return "Bad Request", 403 try: data = request.get_json() new_tool = ToolDTO(**data) result = ToolDAO.dbCreate(new_tool) if result > 0: user_id = request.headers['UserID'] log = LogDTO(user_id=user_id, action="create tool", date_create=datetime.now()) LogDAO.dbCreate(log) return jsonify(result), 201 return "Can't create", 403 except Exception as e: print(e) return "Server error", 500
def enoughQuantity(tool_id, tribulation_id, quantity): tribulation = TribulationDAO.dbGet(tribulation_id) tool_quantity = ToolDAO.dbGet(tool_id).quantity result = False try: list_tool = tft.query.filter( tft.tool_id == tool_id, tft.time_start >= tribulation.time_start, tft.time_end <= tribulation.time_end).all() #sum quantity sum = 0 for tool in list_tool: sum += tool.quantity print('tool quantity = ', tool_quantity) print('sum = ', sum) print('quantity = ', quantity) result = (tool_quantity >= (sum + quantity)) except Exception as e: raise e return result
def update(id): if not request.is_json: return "Bad Request", 403 try: data = request.get_json() result = ToolDAO.dbUpdate(id, ToolDTO(**data)) if result > 0: user_id = request.headers['UserID'] log = LogDTO(user_id=user_id, action="update tool tool id:" + str(id), date_create=datetime.now()) LogDAO.dbCreate(log) return jsonify(result), 200 return "Can't delete", 403 except Exception as e: if "404 Not Found" in str(e): return "404 Not Found", 404 else: return "Server error", 500