Evaluation and error messages for route "update"
This commit is contained in:
parent
719dc3ce2e
commit
4a9ac53dd8
@ -2,18 +2,14 @@ from flask import Blueprint, request, render_template, redirect
|
|||||||
from definitions.models import Asset, db
|
from definitions.models import Asset, db
|
||||||
from config import item_attributes
|
from config import item_attributes
|
||||||
from sqlalchemy import exc # Import exc for database exceptions
|
from sqlalchemy import exc # Import exc for database exceptions
|
||||||
from definitions.attribute import selectAttribute, intAttribute # Import attribute classes
|
from functions.validate_values import validate_values # Form validation
|
||||||
|
|
||||||
update_bp = Blueprint('editasset', __name__)
|
update_bp = Blueprint('editasset', __name__)
|
||||||
|
|
||||||
@update_bp.route('/update/<string:primary_value>/', methods=['GET', 'POST'])
|
@update_bp.route('/update/<string:primary_value>/', methods=['GET', 'POST'])
|
||||||
def update(primary_value):
|
def update(primary_value):
|
||||||
# Identify the primary attribute
|
# Identify the primary attribute
|
||||||
primary_attrib = next(
|
primary_attrib = next((attrib for attrib in item_attributes if attrib.primary), None)
|
||||||
(attrib for attrib in item_attributes if attrib.primary),
|
|
||||||
None
|
|
||||||
)
|
|
||||||
|
|
||||||
if not primary_attrib:
|
if not primary_attrib:
|
||||||
return "Primary attribute not defined in configuration."
|
return "Primary attribute not defined in configuration."
|
||||||
|
|
||||||
@ -23,33 +19,17 @@ def update(primary_value):
|
|||||||
return f"{primary_attrib.display_name} '{primary_value}' not found."
|
return f"{primary_attrib.display_name} '{primary_value}' not found."
|
||||||
|
|
||||||
if request.method == 'GET':
|
if request.method == 'GET':
|
||||||
return render_template(
|
# Render the update form with the current item data
|
||||||
'update.html',
|
return render_template('update.html', item=item, item_attributes=item_attributes)
|
||||||
item=item,
|
|
||||||
item_attributes=item_attributes,
|
|
||||||
isinstance=isinstance, # Pass isinstance to the template
|
|
||||||
hasattr=hasattr, # Pass hasattr to the template
|
|
||||||
selectAttribute=selectAttribute, # Pass selectAttribute to the template
|
|
||||||
intAttribute=intAttribute # Pass intAttribute to the template
|
|
||||||
)
|
|
||||||
|
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
# Dynamically get form data using item_attributes
|
# Get data from form
|
||||||
form_data = {attrib.attrib_name: request.form[attrib.attrib_name] for attrib in item_attributes}
|
form_data = {attrib.attrib_name: request.form.get(attrib.attrib_name) for attrib in item_attributes}
|
||||||
|
|
||||||
# Validate status (if it's an option field)
|
# Form validation
|
||||||
status_attrib = next((attrib for attrib in item_attributes if attrib.attrib_name == 'status'), None)
|
error = validate_values(form_data)
|
||||||
if status_attrib and isinstance(status_attrib, selectAttribute):
|
if error:
|
||||||
if form_data['status'] not in status_attrib.options:
|
return render_template('update.html', item=item, item_attributes=item_attributes, error=error)
|
||||||
return render_template('update.html', item=item, exc='status', item_attributes=item_attributes)
|
|
||||||
|
|
||||||
# Convert staffnum to int (if it's a number field)
|
|
||||||
staffnum_attrib = next((attrib for attrib in item_attributes if attrib.attrib_name == 'staffnum'), None)
|
|
||||||
if staffnum_attrib and isinstance(staffnum_attrib, intAttribute):
|
|
||||||
try:
|
|
||||||
form_data['staffnum'] = int(form_data['staffnum'])
|
|
||||||
except ValueError:
|
|
||||||
return render_template('update.html', item=item, exc='staffnum', item_attributes=item_attributes)
|
|
||||||
|
|
||||||
# Update the item with the new data
|
# Update the item with the new data
|
||||||
for attrib in item_attributes:
|
for attrib in item_attributes:
|
||||||
@ -58,8 +38,17 @@ def update(primary_value):
|
|||||||
try:
|
try:
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
except exc.IntegrityError:
|
except exc.IntegrityError:
|
||||||
return render_template('update.html', item=item, exc='integrity', item_attributes=item_attributes)
|
# Handle duplicate primary key or unique constraint errors
|
||||||
except (exc.StatementError, exc.InvalidRequestError):
|
error = f"An entry with {primary_attrib.display_name} '{form_data[primary_attrib.attrib_name]}' already exists."
|
||||||
return render_template('update.html', item=item, exc='status', item_attributes=item_attributes)
|
return render_template('update.html', item=item, item_attributes=item_attributes, error=error)
|
||||||
|
except exc.StatementError as e:
|
||||||
|
# Handle other database errors
|
||||||
|
error = f"Database error: {str(e)}"
|
||||||
|
return render_template('update.html', item=item, item_attributes=item_attributes, error=error)
|
||||||
|
except Exception as e:
|
||||||
|
# Handle unexpected errors
|
||||||
|
error = f"An unexpected error occurred: {str(e)}"
|
||||||
|
return render_template('update.html', item=item, item_attributes=item_attributes, error=error)
|
||||||
|
|
||||||
|
# Redirect to /viewall on success
|
||||||
return redirect('/viewall')
|
return redirect('/viewall')
|
@ -7,46 +7,44 @@
|
|||||||
<body>
|
<body>
|
||||||
<h2 align="center">Update Item</h2>
|
<h2 align="center">Update Item</h2>
|
||||||
|
|
||||||
|
<!-- Display error message if any -->
|
||||||
|
{% if error %}
|
||||||
|
<p>{{ error }}</p>
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
|
<!-- Form for updating an item -->
|
||||||
<form method="POST">
|
<form method="POST">
|
||||||
{% for attrib in item_attributes -%}
|
{% for attrib in item_attributes -%}
|
||||||
<p>
|
<p>
|
||||||
<label for="{{ attrib.attrib_name }}">{{ attrib.display_name }}:</label>
|
<label for="{{ attrib.attrib_name }}">{{ attrib.display_name }}:</label>
|
||||||
{%- if isinstance(attrib, selectAttribute) %}
|
{% if attrib.html_input_type == "select" %}
|
||||||
|
<!-- Render a dropdown for select attributes -->
|
||||||
<select
|
<select
|
||||||
id="{{ attrib.attrib_name }}"
|
id="{{ attrib.attrib_name }}"
|
||||||
name="{{ attrib.attrib_name }}"
|
name="{{ attrib.attrib_name }}"
|
||||||
{%- if attrib.required %} required {% endif -%}
|
{% if attrib.required %} required {% endif %}
|
||||||
>
|
>
|
||||||
{% for option in attrib.options -%}
|
{% for option in attrib.options -%}
|
||||||
<option value="{{ option }}" {% if item[attrib.attrib_name] == option %}selected{% endif %}>{{ option }}</option>
|
<option value="{{ option }}" {% if item[attrib.attrib_name] == option %}selected{% endif %}>{{ option }}</option>
|
||||||
{% endfor -%}
|
{% endfor %}
|
||||||
</select>
|
</select>
|
||||||
{% else %}
|
{% else %}
|
||||||
|
<!-- Render an input field for other attributes -->
|
||||||
<input
|
<input
|
||||||
id="{{ attrib.attrib_name }}"
|
id="{{ attrib.attrib_name }}"
|
||||||
type="{{ attrib.html_input_type }}"
|
type="{{ attrib.html_input_type }}"
|
||||||
name="{{ attrib.attrib_name }}"
|
name="{{ attrib.attrib_name }}"
|
||||||
{%- if attrib.required %} required {% endif -%}
|
{% if attrib.required %} required {% endif %}
|
||||||
{%- if hasattr(attrib, 'min_val') and attrib.min_val is not none %} min="{{ attrib.min_val }}" {% endif -%}
|
{% if attrib.html_input_type == "number" %}
|
||||||
{%- if hasattr(attrib, 'max_val') and attrib.max_val is not none %} max="{{ attrib.max_val }}" {% endif -%}
|
{% if attrib.min_val is not none %} min="{{ attrib.min_val }}" {% endif %}
|
||||||
{%- if item[attrib.attrib_name] %} value="{{ item[attrib.attrib_name] }}" {% endif -%}
|
{% if attrib.max_val is not none %} max="{{ attrib.max_val }}" {% endif %}
|
||||||
|
{% endif %}
|
||||||
|
{% if item[attrib.attrib_name] %} value="{{ item[attrib.attrib_name] }}" {% endif %}
|
||||||
/>
|
/>
|
||||||
{% endif -%}
|
{% endif %}
|
||||||
</p>
|
</p>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
<p><input type="submit" value="Update" /></p>
|
<p><input type="submit" value="Update" /></p>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
<p align="center">
|
|
||||||
{%- if exc == 'integrity' -%}
|
|
||||||
Item with the same assettag already exists
|
|
||||||
{%- endif -%}
|
|
||||||
{%- if exc == 'status' -%}
|
|
||||||
Data input error. Invalid status value
|
|
||||||
{%- endif -%}
|
|
||||||
{%- if exc == 'staffnum' -%}
|
|
||||||
Data input error. Staff number must be an integer
|
|
||||||
{%- endif -%}
|
|
||||||
</p>
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
Loading…
Reference in New Issue
Block a user