Introduce utility method to look up drive props by drive name. (#888)

* Introduce utility method to look up drive props by drive name.

* Make /drive/create endpoint fetch file ending from properties data structure; update tests
This commit is contained in:
Daniel Markstedt 2022-10-06 14:04:41 -07:00 committed by GitHub
parent 52ebb3a2ae
commit 90ace5fd53
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 64 additions and 60 deletions

View File

@ -28,13 +28,8 @@
</td>
<td style="text-align:left">
<form action="/drive/create" method="post">
<input type="hidden" name="vendor" value="{{ hd.vendor }}">
<input type="hidden" name="product" value="{{ hd.product }}">
<input type="hidden" name="revision" value="{{ hd.revision }}">
<input type="hidden" name="blocks" value="{{ hd.blocks }}">
<input type="hidden" name="block_size" value="{{ hd.block_size }}">
<input type="hidden" name="drive_name" value="{{ hd.name }}">
{{ _("Size:") }} <input type="number" name="size" min="512" max="274877906944" step="512" value="{{ hd.size }}">{{ _("B") }}
<input type="hidden" name="file_type" value="{{ hd.file_type }}">
<label for="file_name">{{ _("Save as:") }}</label>
<input type="text" name="file_name" value="{{ hd.secure_name }}" required />.{{ hd.file_type }}
<input type="submit" value="{{ _("Create") }}" />
@ -72,10 +67,7 @@
</td>
<td style="text-align:left">
<form action="/drive/cdrom" method="post">
<input type="hidden" name="vendor" value="{{ cd.vendor }}">
<input type="hidden" name="product" value="{{ cd.product }}">
<input type="hidden" name="revision" value="{{ cd.revision }}">
<input type="hidden" name="block_size" value="{{ cd.block_size }}">
<input type="hidden" name="drive_name" value="{{ cd.name }}">
<label for="file_name">{{ _("Create for:") }}</label>
<select type="select" name="file_name">
{% for file in files|sort(attribute='name') %}
@ -118,13 +110,8 @@
</td>
<td style="text-align:left">
<form action="/drive/create" method="post">
<input type="hidden" name="vendor" value="{{ rm.vendor }}">
<input type="hidden" name="product" value="{{ rm.product }}">
<input type="hidden" name="revision" value="{{ rm.revision }}">
<input type="hidden" name="blocks" value="{{ rm.blocks }}">
<input type="hidden" name="block_size" value="{{ rm.block_size }}">
<input type="hidden" name="drive_name" value="{{ rm.name }}">
{{ _("Size:") }} <input type="number" name="size" min="512" max="274877906944" step="512" value="{{ rm.size }}">{{ _("B") }}
<input type="hidden" name="file_type" value="{{ rm.file_type }}">
<label for="file_name">{{ _("Save as:") }}</label>
<input type="text" name="file_name" value="{{ rm.secure_name }}" required />.{{ rm.file_type }}
<input type="submit" value="{{ _("Create") }}" />

View File

@ -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)

View File

@ -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.

View File

@ -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,
},
)