diff --git a/process_csv.py b/process_csv.py index f733412..c7a755d 100644 --- a/process_csv.py +++ b/process_csv.py @@ -1,29 +1,15 @@ import csv from io import TextIOWrapper +from config import item_attributes def get_csv_data(file): - """ - Processes the uploaded CSV file and returns a list of asset dictionaries. - Raises an exception if the file is invalid or data is incorrect. - """ - # Open and process the CSV file csv_file = TextIOWrapper(file, encoding='utf-8') reader = csv.DictReader(csv_file, delimiter='|') - # Validate CSV headers - required_headers = ['assettag', 'hostname', 'warrantyfrom', 'status', 'staffnum'] - if not all(header in reader.fieldnames for header in required_headers): - raise ValueError("CSV file must include headers: assettag, hostname, warrantyfrom, status, staffnum.") + # Validate CSV headers based on config + required_headers = set(item_attributes.keys()) + if not required_headers.issubset(reader.fieldnames): + raise ValueError(f"CSV file must include headers: {', '.join(required_headers)}.") - # Extract data from the CSV file - assets = [] - for row in reader: - assets.append({ - 'assettag': row['assettag'], - 'hostname': row['hostname'], - 'warrantyfrom': row['warrantyfrom'], - 'status': row['status'], - 'staffnum': row['staffnum'] - }) - - return assets \ No newline at end of file + # Extract data dynamically based on config + return [{attrib: row[attrib] for attrib in item_attributes} for row in reader]