Check csv for existing items

This commit is contained in:
Candifloss 2025-02-05 13:56:37 +05:30
parent bfd4ecfd7a
commit e96605d831
2 changed files with 47 additions and 4 deletions

View File

@ -22,11 +22,21 @@ def upload_file():
try:
# Extract data from the CSV file
assets = get_csv_data(file)
session['assets'] = assets # Store assets in session
csvdata = get_csv_data(file)
new_assets = []
existing_assets = []
for row in csvdata:
tag = row['assettag']
asset_exists = Asset.query.filter_by(assettag=tag).first()
if asset_exists:
existing_assets.append(row)
else:
new_assets.append(row)
session['assets'] = new_assets # Store assets in session
# Redirect to preview page with the CSV data
return render_template('csv_preview.html', assets=assets)
return render_template('csv_preview.html', new_assets=new_assets, existing=existing_assets)
except Exception as e:
# Handle errors during file processing

View File

@ -10,6 +10,8 @@
<h1>CSV Preview</h1>
<!-- Display CSV data in a table -->
{% if new_assets %}
<p>New assets:</p>
<table border="1">
<thead>
<tr>
@ -21,7 +23,7 @@
</tr>
</thead>
<tbody>
{% for asset in assets %}
{% for asset in new_assets %}
<tr>
<td contenteditable="true">{{ asset.assettag }}</td>
<td contenteditable="true">{{ asset.hostname }}</td>
@ -32,10 +34,41 @@
{% endfor %}
</tbody>
</table>
{% endif %}
{% if existing %}
<p>These assets are already in the database:</p>
<table border="1">
<thead>
<tr>
<th>Asset Tag</th>
<th>Hostname</th>
<th>Warranty From</th>
<th>Status</th>
<th>Staff Number</th>
</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>
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
<!-- Form button to confirm and save data -->
{% if new_assets %}
<form action="/confirm_save" method="POST" onsubmit="collectEditedData()">
<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>
</html>