Updated csv upload page to use config
This commit is contained in:
parent
ca5a572363
commit
c0c2276f05
@ -3,6 +3,7 @@ import csv
|
|||||||
from io import TextIOWrapper
|
from io import TextIOWrapper
|
||||||
from models import Asset, db
|
from models import Asset, db
|
||||||
from process_csv import get_csv_data # Import the CSV processing function
|
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
|
# Create a Blueprint for upload
|
||||||
upload_bp = Blueprint('uploadcsv', __name__)
|
upload_bp = Blueprint('uploadcsv', __name__)
|
||||||
@ -23,20 +24,31 @@ def upload_file():
|
|||||||
try:
|
try:
|
||||||
# Extract data from the CSV file
|
# Extract data from the CSV file
|
||||||
csvdata = get_csv_data(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 = []
|
new_assets = []
|
||||||
existing_assets = []
|
existing_assets = []
|
||||||
for row in csvdata:
|
for row in csvdata:
|
||||||
tag = row['assettag']
|
primary_value = row[primary_attrib]
|
||||||
asset_exists = Asset.query.filter_by(assettag=tag).first()
|
asset_exists = Asset.query.filter_by(**{primary_attrib: primary_value}).first()
|
||||||
if asset_exists:
|
if asset_exists:
|
||||||
existing_assets.append(row)
|
existing_assets.append(row)
|
||||||
else:
|
else:
|
||||||
new_assets.append(row)
|
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
|
# 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:
|
except Exception as e:
|
||||||
# Handle errors during file processing
|
# Handle errors during file processing
|
||||||
@ -44,4 +56,4 @@ def upload_file():
|
|||||||
return redirect('uploadcsv')
|
return redirect('uploadcsv')
|
||||||
|
|
||||||
# Render the upload page for GET requests
|
# Render the upload page for GET requests
|
||||||
return render_template('upload.html')
|
return render_template('upload.html')
|
@ -4,7 +4,7 @@
|
|||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||||
<title>CSV Preview</title>
|
<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>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>CSV Preview</h1>
|
<h1>CSV Preview</h1>
|
||||||
@ -15,21 +15,17 @@
|
|||||||
<table border="1">
|
<table border="1">
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th>Asset Tag</th>
|
{% for attrib, config in item_attributes.items() %}
|
||||||
<th>Hostname</th>
|
<th>{{ config.display_name }}</th>
|
||||||
<th>Warranty From</th>
|
{% endfor %}
|
||||||
<th>Status</th>
|
|
||||||
<th>Staff Number</th>
|
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
{% for asset in new_assets %}
|
{% for asset in new_assets %}
|
||||||
<tr>
|
<tr>
|
||||||
<td contenteditable="true">{{ asset.assettag }}</td>
|
{% for attrib, config in item_attributes.items() %}
|
||||||
<td contenteditable="true">{{ asset.hostname }}</td>
|
<td contenteditable="true">{{ asset[attrib] }}</td>
|
||||||
<td contenteditable="true">{{ asset.warrantyfrom }}</td>
|
{% endfor %}
|
||||||
<td contenteditable="true">{{ asset.status }}</td>
|
|
||||||
<td contenteditable="true">{{ asset.staffnum }}</td>
|
|
||||||
</tr>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</tbody>
|
</tbody>
|
||||||
@ -41,21 +37,17 @@
|
|||||||
<table border="1">
|
<table border="1">
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th>Asset Tag</th>
|
{% for attrib, config in item_attributes.items() %}
|
||||||
<th>Hostname</th>
|
<th>{{ config.display_name }}</th>
|
||||||
<th>Warranty From</th>
|
{% endfor %}
|
||||||
<th>Status</th>
|
|
||||||
<th>Staff Number</th>
|
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody>
|
<tbody>
|
||||||
{% for asset in existing %}
|
{% for asset in existing %}
|
||||||
<tr>
|
<tr>
|
||||||
<td>{{ asset.assettag }}</td>
|
{% for attrib, config in item_attributes.items() %}
|
||||||
<td>{{ asset.hostname }}</td>
|
<td>{{ asset[attrib] }}</td>
|
||||||
<td>{{ asset.warrantyfrom }}</td>
|
{% endfor %}
|
||||||
<td>{{ asset.status }}</td>
|
|
||||||
<td>{{ asset.staffnum }}</td>
|
|
||||||
</tr>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</tbody>
|
</tbody>
|
||||||
@ -64,10 +56,11 @@
|
|||||||
|
|
||||||
<!-- Form button to confirm and save data -->
|
<!-- Form button to confirm and save data -->
|
||||||
{% if new_assets %}
|
{% 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>
|
<button type="submit">Confirm and Save to Database</button>
|
||||||
</form>
|
</form>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
<!-- Cancel button that redirects to the home page -->
|
<!-- Cancel button that redirects to the home page -->
|
||||||
<button type="button" onclick="window.location.href='/'">Cancel</button>
|
<button type="button" onclick="window.location.href='/'">Cancel</button>
|
||||||
</body>
|
</body>
|
||||||
|
Loading…
Reference in New Issue
Block a user