2025-01-30 07:37:21 +00:00
|
|
|
import csv
|
|
|
|
from io import TextIOWrapper
|
|
|
|
|
2025-01-30 09:58:34 +00:00
|
|
|
def get_csv_data(file):
|
2025-01-30 07:37:21 +00:00
|
|
|
"""
|
2025-01-30 09:58:34 +00:00
|
|
|
Processes the uploaded CSV file and returns a list of asset dictionaries.
|
2025-01-30 07:37:21 +00:00
|
|
|
Raises an exception if the file is invalid or data is incorrect.
|
|
|
|
"""
|
2025-01-30 09:58:34 +00:00
|
|
|
# Open and process the CSV file
|
2025-01-30 07:37:21 +00:00
|
|
|
csv_file = TextIOWrapper(file, encoding='utf-8')
|
2025-01-30 09:58:34 +00:00
|
|
|
reader = csv.DictReader(csv_file, delimiter='|')
|
2025-01-30 07:37:21 +00:00
|
|
|
|
|
|
|
# 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.")
|
|
|
|
|
2025-01-30 09:58:34 +00:00
|
|
|
# Extract data from the CSV file
|
2025-01-30 07:37:21 +00:00
|
|
|
assets = []
|
|
|
|
for row in reader:
|
2025-01-30 09:58:34 +00:00
|
|
|
assets.append({
|
|
|
|
'assettag': row['assettag'],
|
|
|
|
'hostname': row['hostname'],
|
|
|
|
'warrantyfrom': row['warrantyfrom'],
|
|
|
|
'status': row['status'],
|
|
|
|
'staffnum': row['staffnum']
|
|
|
|
})
|
2025-01-30 07:37:21 +00:00
|
|
|
|
2025-01-30 09:58:34 +00:00
|
|
|
return assets
|