Updated process_csv.py to use config
This commit is contained in:
parent
7b6418f185
commit
20d0f163e6
@ -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
|
||||
# Extract data dynamically based on config
|
||||
return [{attrib: row[attrib] for attrib in item_attributes} for row in reader]
|
||||
|
Loading…
Reference in New Issue
Block a user