Updated csv upload page to use config

This commit is contained in:
Candifloss 2025-02-08 03:26:07 +05:30
parent ca5a572363
commit c0c2276f05
2 changed files with 32 additions and 27 deletions

View File

@ -3,6 +3,7 @@ import csv
from io import TextIOWrapper
from models import Asset, db
from process_csv import get_csv_data # Import the CSV processing function
from config import item_attributes # Import the configuration
# Create a Blueprint for upload
upload_bp = Blueprint('uploadcsv', __name__)
@ -23,20 +24,31 @@ def upload_file():
try:
# Extract data from the CSV file
csvdata = get_csv_data(file)
# Identify the primary attribute
primary_attrib = next(
(attrib for attrib, config in item_attributes.items() if config.primary),
None
)
if not primary_attrib:
return "Primary attribute not defined in configuration."
# Separate new and existing assets
new_assets = []
existing_assets = []
for row in csvdata:
tag = row['assettag']
asset_exists = Asset.query.filter_by(assettag=tag).first()
primary_value = row[primary_attrib]
asset_exists = Asset.query.filter_by(**{primary_attrib: primary_value}).first()
if asset_exists:
existing_assets.append(row)
else:
new_assets.append(row)
session['assets'] = new_assets # Store assets in session
session['assets'] = new_assets # Store new assets in session
# Redirect to preview page with the CSV data
return render_template('csv_preview.html', new_assets=new_assets, existing=existing_assets)
return render_template('csv_preview.html', new_assets=new_assets, existing=existing_assets, item_attributes=item_attributes)
except Exception as e:
# Handle errors during file processing

View File

@ -4,7 +4,7 @@
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSV Preview</title>
<script src="{{ url_for('static', filename='edited _csv.js') }}"></script>
<script src="{{ url_for('static', filename='edited_csv.js') }}"></script>
</head>
<body>
<h1>CSV Preview</h1>
@ -15,21 +15,17 @@
<table border="1">
<thead>
<tr>
<th>Asset Tag</th>
<th>Hostname</th>
<th>Warranty From</th>
<th>Status</th>
<th>Staff Number</th>
{% for attrib, config in item_attributes.items() %}
<th>{{ config.display_name }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for asset in new_assets %}
<tr>
<td contenteditable="true">{{ asset.assettag }}</td>
<td contenteditable="true">{{ asset.hostname }}</td>
<td contenteditable="true">{{ asset.warrantyfrom }}</td>
<td contenteditable="true">{{ asset.status }}</td>
<td contenteditable="true">{{ asset.staffnum }}</td>
{% for attrib, config in item_attributes.items() %}
<td contenteditable="true">{{ asset[attrib] }}</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
@ -41,21 +37,17 @@
<table border="1">
<thead>
<tr>
<th>Asset Tag</th>
<th>Hostname</th>
<th>Warranty From</th>
<th>Status</th>
<th>Staff Number</th>
{% for attrib, config in item_attributes.items() %}
<th>{{ config.display_name }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for asset in existing %}
<tr>
<td>{{ asset.assettag }}</td>
<td>{{ asset.hostname }}</td>
<td>{{ asset.warrantyfrom }}</td>
<td>{{ asset.status }}</td>
<td>{{ asset.staffnum }}</td>
{% for attrib, config in item_attributes.items() %}
<td>{{ asset[attrib] }}</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
@ -64,10 +56,11 @@
<!-- Form button to confirm and save data -->
{% if new_assets %}
<form action="/confirm_save" method="POST" onsubmit="collectEditedData()">
<form action="/confirm_save" method="POST" onsubmit="return collectEditedData(event)">
<button type="submit">Confirm and Save to Database</button>
</form>
{% endif %}
<!-- Cancel button that redirects to the home page -->
<button type="button" onclick="window.location.href='/'">Cancel</button>
</body>