diff --git a/python/web/src/templates/drives.html b/python/web/src/templates/drives.html index e1bf9f40..0f5c8561 100644 --- a/python/web/src/templates/drives.html +++ b/python/web/src/templates/drives.html @@ -28,13 +28,8 @@
- - - - - + {{ _("Size:") }} {{ _("B") }} - .{{ hd.file_type }} @@ -72,10 +67,7 @@ - - - - + - - - - + {{ _("Size:") }} {{ _("B") }} - .{{ rm.file_type }} diff --git a/python/web/src/web.py b/python/web/src/web.py index 803fac86..0d95348f 100644 --- a/python/web/src/web.py +++ b/python/web/src/web.py @@ -52,6 +52,7 @@ from web_utils import ( get_device_name, map_image_file_descriptions, format_drive_properties, + get_properties_by_drive_name, auth_active, is_bridge_configured, upload_with_dropzonejs, @@ -358,28 +359,23 @@ def drive_create(): """ Creates the image and properties file pair """ - vendor = request.form.get("vendor") - product = request.form.get("product") - revision = request.form.get("revision") - block_size = request.form.get("block_size") + drive_name = request.form.get("drive_name") size = request.form.get("size") - file_type = request.form.get("file_type") file_name = request.form.get("file_name") - full_file_name = file_name + "." + file_type + + properties = get_properties_by_drive_name( + APP.config["RASCSI_DRIVE_PROPERTIES"], + drive_name + ) # Creating the image file - process = file_cmd.create_new_image(file_name, file_type, size) + process = file_cmd.create_new_image(file_name, properties["file_type"], size) if not process["status"]: return response(error=True, message=process["msg"]) # Creating the drive properties file - prop_file_name = f"{file_name}.{file_type}.{PROPERTIES_SUFFIX}" - properties = { - "vendor": vendor, - "product": product, - "revision": revision, - "block_size": block_size, - } + full_file_name = f"{file_name}.{properties['file_type']}" + prop_file_name = f"{full_file_name}.{PROPERTIES_SUFFIX}" process = file_cmd.write_drive_properties(prop_file_name, properties) process = ReturnCodeMapper.add_msg(process) if not process["status"]: @@ -394,20 +390,15 @@ def drive_cdrom(): """ Creates a properties file for a CD-ROM image """ - vendor = request.form.get("vendor") - product = request.form.get("product") - revision = request.form.get("revision") - block_size = request.form.get("block_size") + drive_name = request.form.get("drive_name") file_name = request.form.get("file_name") # Creating the drive properties file file_name = f"{file_name}.{PROPERTIES_SUFFIX}" - properties = { - "vendor": vendor, - "product": product, - "revision": revision, - "block_size": block_size, - } + properties = get_properties_by_drive_name( + APP.config["RASCSI_DRIVE_PROPERTIES"], + drive_name + ) process = file_cmd.write_drive_properties(file_name, properties) process = ReturnCodeMapper.add_msg(process) if process["status"]: @@ -946,17 +937,10 @@ def create_file(): # Creating the drive properties file, if one is chosen if drive_name: - drive_props = None - for drive in APP.config["RASCSI_DRIVE_PROPERTIES"]: - if drive["name"] == drive_name: - drive_props = drive - break - properties = { - "vendor": drive_props["vendor"], - "product": drive_props["product"], - "revision": drive_props["revision"], - "block_size": drive_props["block_size"], - } + properties = get_properties_by_drive_name( + APP.config["RASCSI_DRIVE_PROPERTIES"], + drive_name + ) prop_file_name = f"{full_file_name}.{PROPERTIES_SUFFIX}" process = file_cmd.write_drive_properties(prop_file_name, properties) process = ReturnCodeMapper.add_msg(process) diff --git a/python/web/src/web_utils.py b/python/web/src/web_utils.py index c58ec1f2..5412a9eb 100644 --- a/python/web/src/web_utils.py +++ b/python/web/src/web_utils.py @@ -156,6 +156,12 @@ def format_drive_properties(drive_properties): FORMAT_FILTER = "{:,.2f}" for device in drive_properties: + # Add fallback device names, since other code relies on this data for display + if not device["name"]: + if device["product"]: + device["name"] = device["product"] + else: + device["name"] = "Unknown Device" if device["device_type"] == "SCHD": device["secure_name"] = secure_filename(device["name"]) device["size_mb"] = FORMAT_FILTER.format(device["size"] / 1024 / 1024) @@ -179,6 +185,40 @@ def format_drive_properties(drive_properties): "mo_conf": mo_conf, } +def get_properties_by_drive_name(drives, drive_name): + """ + Takes (list) of (dict) drives, and (str) drive_name + Returns (dict) with the collection of drive properties that matches drive_name + """ + drives.sort(key=lambda item: item.get("name")) + + drive_props = None + prev_drive = {"name": ""} + for drive in drives: + # TODO: Make this check into an integration test + if "name" not in drive: + logging.warning( + "Device without a name exists in the drive properties database. This is a bug." + ) + break + # TODO: Make this check into an integration test + if drive["name"] == prev_drive["name"]: + logging.warning( + "Device with duplicate name \"%s\" in drive properties database. This is a bug.", + drive["name"], + ) + prev_drive = drive + if drive["name"] == drive_name: + drive_props = drive + + return { + "file_type": drive_props["file_type"], + "vendor": drive_props["vendor"], + "product": drive_props["product"], + "revision": drive_props["revision"], + "block_size": drive_props["block_size"], + } + def auth_active(group): """ Inspects if the group defined in (str) group exists on the system. diff --git a/python/web/tests/api/test_misc.py b/python/web/tests/api/test_misc.py index a88a7243..9ffe698a 100644 --- a/python/web/tests/api/test_misc.py +++ b/python/web/tests/api/test_misc.py @@ -52,10 +52,7 @@ def test_create_cdrom_properties_file(http_client): response = http_client.post( "/drive/cdrom", data={ - "vendor": "TEST_AAA", - "product": "TEST_BBB", - "revision": "1.0A", - "block_size": 2048, + "drive_name": "Sony CDU-8012", "file_name": file_name, }, ) @@ -77,12 +74,8 @@ def test_create_image_with_properties_file(http_client, delete_file): response = http_client.post( "/drive/create", data={ - "vendor": "TEST_AAA", - "product": "TEST_BBB", - "revision": "1.0A", - "block_size": 512, + "drive_name": "Miniscribe M8425", "size": FILE_SIZE_1_MIB, - "file_type": "hds", "file_name": file_prefix, }, )