Refactored create & update forms into 1 form
This commit is contained in:
parent
71866cd883
commit
7bb5727cbf
@ -10,7 +10,7 @@ addasset_bp = Blueprint('addasset', __name__)
|
|||||||
def create():
|
def create():
|
||||||
if request.method == 'GET':
|
if request.method == 'GET':
|
||||||
# Render the "add item" form
|
# Render the "add item" form
|
||||||
return render_template('create.html', item_attributes=item_attributes)
|
return render_template('item_form.html', item_attributes=item_attributes, item=None)
|
||||||
|
|
||||||
# Process submitted form
|
# Process submitted form
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
@ -20,7 +20,7 @@ def create():
|
|||||||
# Form validation
|
# Form validation
|
||||||
error = validate_values(form_data)
|
error = validate_values(form_data)
|
||||||
if error:
|
if error:
|
||||||
return render_template('create.html', item_attributes=item_attributes, error=error)
|
return render_template('item_form.html', item_attributes=item_attributes, item=None, error=error)
|
||||||
|
|
||||||
# Create the Asset object
|
# Create the Asset object
|
||||||
item = Asset(**form_data)
|
item = Asset(**form_data)
|
||||||
@ -35,15 +35,15 @@ def create():
|
|||||||
error = f"An entry with {primary_attrib.display_name} '{form_data[primary_attrib.attrib_name]}' already exists."
|
error = f"An entry with {primary_attrib.display_name} '{form_data[primary_attrib.attrib_name]}' already exists."
|
||||||
else:
|
else:
|
||||||
error = "An entry with the same primary key already exists."
|
error = "An entry with the same primary key already exists."
|
||||||
return render_template('create.html', item_attributes=item_attributes, error=error)
|
return render_template('item_form.html', item_attributes=item_attributes, item=None, error=error)
|
||||||
except exc.StatementError as e:
|
except exc.StatementError as e:
|
||||||
# Handle other database errors
|
# Handle other database errors
|
||||||
error = f"Database error: {str(e)}"
|
error = f"Database error: {str(e)}"
|
||||||
return render_template('create.html', item_attributes=item_attributes, error=error)
|
return render_template('item_form.html', item_attributes=item_attributes, item=None, error=error)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
# Handle unexpected errors
|
# Handle unexpected errors
|
||||||
error = f"An unexpected error occurred: {str(e)}"
|
error = f"An unexpected error occurred: {str(e)}"
|
||||||
return render_template('create.html', item_attributes=item_attributes, error=error)
|
return render_template('item_form.html', item_attributes=item_attributes, item=None, error=error)
|
||||||
|
|
||||||
# Redirect to /viewall on success
|
# Redirect to /viewall on success
|
||||||
return redirect('/viewall')
|
return redirect('/viewall')
|
@ -20,7 +20,7 @@ def update(primary_value):
|
|||||||
|
|
||||||
if request.method == 'GET':
|
if request.method == 'GET':
|
||||||
# Render the update form with the current item data
|
# Render the update form with the current item data
|
||||||
return render_template('update.html', item=item, item_attributes=item_attributes)
|
return render_template('item_form.html', item=item, item_attributes=item_attributes)
|
||||||
|
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
# Get data from form
|
# Get data from form
|
||||||
@ -29,7 +29,7 @@ def update(primary_value):
|
|||||||
# Form validation
|
# Form validation
|
||||||
error = validate_values(form_data)
|
error = validate_values(form_data)
|
||||||
if error:
|
if error:
|
||||||
return render_template('update.html', item=item, item_attributes=item_attributes, error=error)
|
return render_template('item_form.html', item=item, item_attributes=item_attributes, error=error)
|
||||||
|
|
||||||
# Update the item with the new data
|
# Update the item with the new data
|
||||||
for attrib in item_attributes:
|
for attrib in item_attributes:
|
||||||
@ -40,15 +40,15 @@ def update(primary_value):
|
|||||||
except exc.IntegrityError:
|
except exc.IntegrityError:
|
||||||
# Handle duplicate primary key or unique constraint errors
|
# 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."
|
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)
|
return render_template('item_form.html', item=item, item_attributes=item_attributes, error=error)
|
||||||
except exc.StatementError as e:
|
except exc.StatementError as e:
|
||||||
# Handle other database errors
|
# Handle other database errors
|
||||||
error = f"Database error: {str(e)}"
|
error = f"Database error: {str(e)}"
|
||||||
return render_template('update.html', item=item, item_attributes=item_attributes, error=error)
|
return render_template('item_form.html', item=item, item_attributes=item_attributes, error=error)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
# Handle unexpected errors
|
# Handle unexpected errors
|
||||||
error = f"An unexpected error occurred: {str(e)}"
|
error = f"An unexpected error occurred: {str(e)}"
|
||||||
return render_template('update.html', item=item, item_attributes=item_attributes, error=error)
|
return render_template('item_form.html', item=item, item_attributes=item_attributes, error=error)
|
||||||
|
|
||||||
# Redirect to /viewall on success
|
# Redirect to /viewall on success
|
||||||
return redirect('/viewall')
|
return redirect('/viewall')
|
@ -2,17 +2,17 @@
|
|||||||
<html lang="en">
|
<html lang="en">
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<title>Add an Item</title>
|
<title>{% if item %}Update{% else %}Add{% endif %} Item</title>
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h2 align="center">Add new Item</h2>
|
<h2 align="center">{% if item %}Update{% else %}Add{% endif %} Item</h2>
|
||||||
|
|
||||||
<!-- Display error message if any -->
|
<!-- Display error message if any -->
|
||||||
{% if error %}
|
{% if error %}
|
||||||
<p>{{ error }}</p>
|
<p>{{ error }}</p>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
<!-- Form for adding a new item -->
|
<!-- Form for adding/updating an item -->
|
||||||
<form method="POST">
|
<form method="POST">
|
||||||
{% for attrib in item_attributes -%}
|
{% for attrib in item_attributes -%}
|
||||||
<p>
|
<p>
|
||||||
@ -25,7 +25,7 @@
|
|||||||
{% if attrib.required %} required {% endif %}
|
{% if attrib.required %} required {% endif %}
|
||||||
>
|
>
|
||||||
{% for option in attrib.options -%}
|
{% for option in attrib.options -%}
|
||||||
<option value="{{ option }}">{{ option }}</option>
|
<option value="{{ option }}" {% if item and item[attrib.attrib_name] == option %}selected{% endif %}>{{ option }}</option>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</select>
|
</select>
|
||||||
{% else %}
|
{% else %}
|
||||||
@ -41,12 +41,16 @@
|
|||||||
{% if attrib.max_length is not none %} maxlength="{{ attrib.max_length }}" {% endif %}
|
{% if attrib.max_length is not none %} maxlength="{{ attrib.max_length }}" {% endif %}
|
||||||
{% if attrib.min_length is not none %} minlength="{{ attrib.min_length }}" {% endif %}
|
{% if attrib.min_length is not none %} minlength="{{ attrib.min_length }}" {% endif %}
|
||||||
{% if attrib.regex is not none %} pattern="{{ attrib.regex }}" {% endif %}
|
{% if attrib.regex is not none %} pattern="{{ attrib.regex }}" {% endif %}
|
||||||
{% if attrib.default_val is not none %} value="{{ attrib.default_val }}" {% endif %}
|
{% if item %}
|
||||||
|
value="{{ item[attrib.attrib_name] }}"
|
||||||
|
{% else %}
|
||||||
|
{% if attrib.default_val is not none %} value="{{ attrib.default_val }}" {% endif %}
|
||||||
|
{% endif %}
|
||||||
/>
|
/>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</p>
|
</p>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
<p><input type="submit" value="Submit" /></p>
|
<p><input type="submit" value="{% if item %}Update{% else %}Submit{% endif %}" /></p>
|
||||||
<button type="button" onclick="window.location.href='/'">Cancel</button>
|
<button type="button" onclick="window.location.href='/'">Cancel</button>
|
||||||
</form>
|
</form>
|
||||||
</body>
|
</body>
|
@ -1,51 +0,0 @@
|
|||||||
<!DOCTYPE html>
|
|
||||||
<html lang="en">
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8">
|
|
||||||
<title>Update Item</title>
|
|
||||||
</head>
|
|
||||||
<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 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 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 %}
|
|
||||||
</p>
|
|
||||||
{% endfor %}
|
|
||||||
<p><input type="submit" value="Update" /></p>
|
|
||||||
<button type="button" onclick="window.location.href='/'">Cancel</button>
|
|
||||||
</form>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
Loading…
Reference in New Issue
Block a user