def get_pool(): if "id" not in request.args: return "Pool ID not provided in request", 400 pool_id = request.args.get('id') try: pool = Pool.get_pool(pool_id) return jsonify({"pool": Pool.json(pool)}) except AttributeError: return "Pool of ID {} doesn't exist".format(pool_id), 404
def create_reservation(): if not request.json: return "Create data not provided", 400 try: pool_id = request.json['PoolID'] email = request.json['Email'] start_date = dt.strptime(request.json["StartDate"], date_conversion_format) end_date = dt.strptime(request.json["EndDate"], date_conversion_format) machine_count = int(request.json['Count']) except KeyError as e: return "Value of {} missing in given JSON".format(e), 400 except ValueError: return 'Inappropriate value in json', 400 try: pool = Pool.get_pool(pool_id) user = User.get_user_by_email(email) if pool and user: reservation = pool.add_reservation(user, machine_count, start_date, end_date) return jsonify({'ReservationID': reservation.ID}), 200 except Exception as e: return str(e), 404
def edit_pool(): if "id" not in request.args: return "Pool ID not provided in request", 400 if not request.json: return "Pool data not provided", 400 pool_id = request.args.get('id') try: pool = Pool.get_pool(pool_id) pool.edit_pool(request.json['ID'], request.json['Name'], request.json.get('MaximumCount', ''), request.json.get('Description', ''), request.json.get('Enabled', False)) pool.edit_software(request.json.get('InstalledSoftware', [])) operating_system = request.json.get('OSName', '') if operating_system: operating_system = OperatingSystem.add_operating_system( operating_system) pool.set_operating_system(operating_system) return "Pool successfully edited", 200 except ValueError: return "Pool of given ID already exists", 422 except AttributeError as e: print(e) return "Pool of ID {} doesn't exist".format(pool_id), 404
def gen_mock_reservations(filename): with open(os.path.join(MOCK_DATA_PATH, filename)) as json_file: data = json.load(json_file) for res_data in data['reservations']: user = User.get_user(res_data["UserID"]) pool = Pool.get_pool(res_data["PoolID"]) today = datetime.datetime.now() sd = datetime.datetime.fromtimestamp(int(res_data["StartDate"])) start_date = today + datetime.timedelta( days=-today.weekday() + sd.weekday(), hours=-today.hour + sd.hour - 2, minutes=-today.minute + sd.minute, seconds=-today.second) ed = datetime.datetime.fromtimestamp(int(res_data["EndDate"])) end_date = today + datetime.timedelta( days=-today.weekday() + ed.weekday(), hours=-today.hour + ed.hour - 2, minutes=-today.minute + ed.minute, seconds=-today.second) reservation = Reservation(PoolID=pool.ID, UserID=user.ID, StartDate=start_date, EndDate=end_date, MachineCount=res_data["Count"], Cancelled=res_data["Cancelled"]) db.session.add(reservation) db.session.commit()
def remove_pool(): if "id" not in request.args: return "Pool ID not provided in request", 400 pool_id = request.args.get('id') try: pool = Pool.get_pool(pool_id) pool.remove() except Exception as e: print(e) return "Pool of ID {} doesn't exist!".format(id), 404 return "Pool of ID {} successfully deleted".format(id), 200
def add_pool(): if not request.json: return "Pool data not provided", 400 try: pool_id = request.json['ID'] pool = Pool.add_pool(pool_id, request.json['Name'], request.json.get('MaximumCount', 0), request.json.get('Description', ''), request.json.get('Enabled', False)) operating_system = request.json.get('OSName', '') if operating_system: operating_system = OperatingSystem.add_operating_system( operating_system) pool.set_operating_system(operating_system) installed_software = request.json.get('InstalledSoftware', []) for name, version in installed_software: software = Software.add_software(name) pool.add_software(software, version) return Pool.get_pool(pool_id).ID, 200 except KeyError as e: return "Value of {} missing in given JSON".format(e), 400 except ValueError: return "Pool of given ID already exists", 422
def gen_mock_pools(filename): with open(os.path.join(MOCK_DATA_PATH, filename)) as json_file: data = json.load(json_file) for pool_data in data['pools']: pool = Pool.add_pool(pool_data['ID'], pool_data['Name'], pool_data['MaximumCount'], pool_data['Description'], pool_data['Enabled']) operating_system = OperatingSystem.add_operating_system( pool_data['OSName']) pool.set_operating_system(operating_system) for name, version in pool_data['InstalledSoftware']: software = Software.add_software(name) pool.add_software(software, version)
def get_pools(): return jsonify({"pools": Pool.get_table()})
def parse_file(self, force=False): csv_string = str(self.file.read(), "utf-8") csv_reader = csv.reader(csv_string.split("\n"), delimiter=",") next(csv_reader) for row_number, row in enumerate(csv_reader, 1): if len(row) > 0: # ID try: pool_id = Parser.extract_name(row[0]) try: if Pool.get_pool(pool_id): self.add_error( row_number, row, "Pool with this ID already exists!") force = False except ValueError: pass except (ValueError, NameError) as e: print(str(e)) self.add_error(row_number, row, "Incorrect 'Pool ID' value!") force = False # Name try: pool_name = Parser.extract_name(row[1]) except (ValueError, NameError): self.add_warning(row_number, row, "Incorrect 'Pool Name' value") force = False # Maximum Count try: pool_maximum_count = int(row[2]) except ValueError: self.add_warning("Incorrect 'Maximum Count' value") pool_maximum_count = 0 if pool_maximum_count < 0: self.add_warning(row_number, row, "Incorrect 'Maximum Count' value") # Enabled if row[3].strip() == "true": enabled = True elif row[3].strip() == "false": enabled = False else: self.add_warning(row_number, row, "Incorrect 'Enabled' value'") enabled = False pool_description = '' # adding Pool to database if force: pool = Pool.add_pool( pool_id, pool_name, pool_maximum_count, pool_description, enabled, ) else: pool = None # Operating System try: pool_os = Parser.extract_version(row[1]) # adding Operating System to Pool in database if force and pool: operating_system = OperatingSystem.add_operating_system( pool_os) pool.set_operating_system(operating_system) except (ValueError, NameError): self.add_warning(row_number, row, "Incorrect 'Operating System' value") # Software if len(row[4]) < 1: continue software_list = row[4].split(",") for line in software_list: # Software Name try: software_name = Parser.extract_name(line) except (ValueError, NameError): self.add_error(row_number, row, "Incorrect 'Software Name' value") force = False # Software Version try: software_version = Parser.extract_version(line) if line.find(')') + 1 < len(line): self.add_warning( row_number, row, "Unexpected content after software version (split software with ',')" ) except ValueError: self.add_warning(row_number, row, "Incorrect 'Software Version' value") software_version = '' except NameError: software_version = '' # adding Software to pool and database if force and pool: software = Software.add_software(software_name) pool.add_software(software, software_version)