Updated process_csv.py to use config
This commit is contained in:
parent
7b6418f185
commit
20d0f163e6
@ -1,29 +1,15 @@
|
|||||||
import csv
|
import csv
|
||||||
from io import TextIOWrapper
|
from io import TextIOWrapper
|
||||||
|
from config import item_attributes
|
||||||
|
|
||||||
def get_csv_data(file):
|
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')
|
csv_file = TextIOWrapper(file, encoding='utf-8')
|
||||||
reader = csv.DictReader(csv_file, delimiter='|')
|
reader = csv.DictReader(csv_file, delimiter='|')
|
||||||
|
|
||||||
# Validate CSV headers
|
# Validate CSV headers based on config
|
||||||
required_headers = ['assettag', 'hostname', 'warrantyfrom', 'status', 'staffnum']
|
required_headers = set(item_attributes.keys())
|
||||||
if not all(header in reader.fieldnames for header in required_headers):
|
if not required_headers.issubset(reader.fieldnames):
|
||||||
raise ValueError("CSV file must include headers: assettag, hostname, warrantyfrom, status, staffnum.")
|
raise ValueError(f"CSV file must include headers: {', '.join(required_headers)}.")
|
||||||
|
|
||||||
# Extract data from the CSV file
|
# Extract data dynamically based on config
|
||||||
assets = []
|
return [{attrib: row[attrib] for attrib in item_attributes} for row in reader]
|
||||||
for row in reader:
|
|
||||||
assets.append({
|
|
||||||
'assettag': row['assettag'],
|
|
||||||
'hostname': row['hostname'],
|
|
||||||
'warrantyfrom': row['warrantyfrom'],
|
|
||||||
'status': row['status'],
|
|
||||||
'staffnum': row['staffnum']
|
|
||||||
})
|
|
||||||
|
|
||||||
return assets
|
|
||||||
|
Loading…
Reference in New Issue
Block a user