Check csv for existing items
This commit is contained in:
parent
bfd4ecfd7a
commit
e96605d831
@ -22,11 +22,21 @@ def upload_file():
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
# Extract data from the CSV file
|
# Extract data from the CSV file
|
||||||
assets = get_csv_data(file)
|
csvdata = get_csv_data(file)
|
||||||
session['assets'] = assets # Store assets in session
|
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
|
# 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:
|
except Exception as e:
|
||||||
# Handle errors during file processing
|
# Handle errors during file processing
|
||||||
|
@ -10,6 +10,8 @@
|
|||||||
<h1>CSV Preview</h1>
|
<h1>CSV Preview</h1>
|
||||||
|
|
||||||
<!-- Display CSV data in a table -->
|
<!-- Display CSV data in a table -->
|
||||||
|
{% if new_assets %}
|
||||||
|
<p>New assets:</p>
|
||||||
<table border="1">
|
<table border="1">
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
@ -21,7 +23,7 @@
|
|||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
{% for asset in assets %}
|
{% for asset in new_assets %}
|
||||||
<tr>
|
<tr>
|
||||||
<td contenteditable="true">{{ asset.assettag }}</td>
|
<td contenteditable="true">{{ asset.assettag }}</td>
|
||||||
<td contenteditable="true">{{ asset.hostname }}</td>
|
<td contenteditable="true">{{ asset.hostname }}</td>
|
||||||
@ -32,10 +34,41 @@
|
|||||||
{% endfor %}
|
{% endfor %}
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</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 -->
|
<!-- 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="collectEditedData()">
|
||||||
<button type="submit">Confirm and Save to Database</button>
|
<button type="submit">Confirm and Save to Database</button>
|
||||||
</form>
|
</form>
|
||||||
|
{% endif %}
|
||||||
|
<!-- Cancel button that redirects to the home page -->
|
||||||
|
<button type="button" onclick="window.location.href='/'">Cancel</button>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
Loading…
Reference in New Issue
Block a user