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 config import item_attributes
|
||||
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.route('/update/<string:primary_value>/', methods=['GET', 'POST'])
|
||||
def update(primary_value):
|
||||
# Identify the primary attribute
|
||||
primary_attrib = next(
|
||||
(attrib for attrib in item_attributes if attrib.primary),
|
||||
None
|
||||
)
|
||||
|
||||
primary_attrib = next((attrib for attrib in item_attributes if attrib.primary), None)
|
||||
if not primary_attrib:
|
||||
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."
|
||||
|
||||
if request.method == 'GET':
|
||||
return render_template(
|
||||
'update.html',
|
||||
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
|
||||
)
|
||||
# Render the update form with the current item data
|
||||
return render_template('update.html', item=item, item_attributes=item_attributes)
|
||||
|
||||
if request.method == 'POST':
|
||||
# Dynamically get form data using item_attributes
|
||||
form_data = {attrib.attrib_name: request.form[attrib.attrib_name] for attrib in item_attributes}
|
||||
# Get data from form
|
||||
form_data = {attrib.attrib_name: request.form.get(attrib.attrib_name) for attrib in item_attributes}
|
||||
|
||||
# Validate status (if it's an option field)
|
||||
status_attrib = next((attrib for attrib in item_attributes if attrib.attrib_name == 'status'), None)
|
||||
if status_attrib and isinstance(status_attrib, selectAttribute):
|
||||
if form_data['status'] not in status_attrib.options:
|
||||
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)
|
||||
# Form validation
|
||||
error = validate_values(form_data)
|
||||
if error:
|
||||
return render_template('update.html', item=item, item_attributes=item_attributes, error=error)
|
||||
|
||||
# Update the item with the new data
|
||||
for attrib in item_attributes:
|
||||
@ -58,8 +38,17 @@ def update(primary_value):
|
||||
try:
|
||||
db.session.commit()
|
||||
except exc.IntegrityError:
|
||||
return render_template('update.html', item=item, exc='integrity', item_attributes=item_attributes)
|
||||
except (exc.StatementError, exc.InvalidRequestError):
|
||||
return render_template('update.html', item=item, exc='status', item_attributes=item_attributes)
|
||||
# Handle duplicate primary key or unique constraint errors
|
||||
error = f"An entry with {primary_attrib.display_name} '{form_data[primary_attrib.attrib_name]}' already exists."
|
||||
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')
|
@ -7,46 +7,44 @@
|
||||
<body>
|
||||
<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">
|
||||
{% for attrib in item_attributes -%}
|
||||
<p>
|
||||
<label for="{{ attrib.attrib_name }}">{{ attrib.display_name }}:</label>
|
||||
{%- if isinstance(attrib, selectAttribute) %}
|
||||
<select
|
||||
id="{{ attrib.attrib_name }}"
|
||||
name="{{ attrib.attrib_name }}"
|
||||
{%- if attrib.required %} required {% endif -%}
|
||||
>
|
||||
{% for option in attrib.options -%}
|
||||
<option value="{{ option }}" {% if item[attrib.attrib_name] == option %}selected{% endif %}>{{ option }}</option>
|
||||
{% endfor -%}
|
||||
</select>
|
||||
{% if attrib.html_input_type == "select" %}
|
||||
<!-- Render a dropdown for select attributes -->
|
||||
<select
|
||||
id="{{ attrib.attrib_name }}"
|
||||
name="{{ attrib.attrib_name }}"
|
||||
{% if attrib.required %} required {% endif %}
|
||||
>
|
||||
{% for option in attrib.options -%}
|
||||
<option value="{{ option }}" {% if item[attrib.attrib_name] == option %}selected{% endif %}>{{ option }}</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
{% else %}
|
||||
<!-- Render an input field for other attributes -->
|
||||
<input
|
||||
id="{{ attrib.attrib_name }}"
|
||||
type="{{ attrib.html_input_type }}"
|
||||
name="{{ attrib.attrib_name }}"
|
||||
{%- if attrib.required %} required {% endif -%}
|
||||
{%- if hasattr(attrib, 'min_val') and attrib.min_val is not none %} min="{{ attrib.min_val }}" {% endif -%}
|
||||
{%- if hasattr(attrib, 'max_val') and attrib.max_val is not none %} max="{{ attrib.max_val }}" {% endif -%}
|
||||
{%- if item[attrib.attrib_name] %} value="{{ item[attrib.attrib_name] }}" {% endif -%}
|
||||
{% if attrib.required %} required {% endif %}
|
||||
{% if attrib.html_input_type == "number" %}
|
||||
{% if attrib.min_val is not none %} min="{{ attrib.min_val }}" {% 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>
|
||||
{% endfor %}
|
||||
<p><input type="submit" value="Update" /></p>
|
||||
</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>
|
||||
</html>
|
Loading…
Reference in New Issue
Block a user