From 7bb5727cbf370ae9c857676c3a0cf67b1f1977ca Mon Sep 17 00:00:00 2001 From: candifloss Date: Wed, 5 Mar 2025 12:25:37 +0530 Subject: [PATCH] Refactored create & update forms into 1 form --- routes/create.py | 10 ++--- routes/update.py | 10 ++--- templates/{create.html => item_form.html} | 16 ++++--- templates/update.html | 51 ----------------------- 4 files changed, 20 insertions(+), 67 deletions(-) rename templates/{create.html => item_form.html} (72%) delete mode 100644 templates/update.html diff --git a/routes/create.py b/routes/create.py index 0975cf3..e4abc7c 100644 --- a/routes/create.py +++ b/routes/create.py @@ -10,7 +10,7 @@ addasset_bp = Blueprint('addasset', __name__) def create(): if request.method == 'GET': # 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 if request.method == 'POST': @@ -20,7 +20,7 @@ def create(): # Form validation error = validate_values(form_data) 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 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." else: 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: # Handle other database errors 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: # Handle unexpected errors 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 return redirect('/viewall') \ No newline at end of file diff --git a/routes/update.py b/routes/update.py index fdf7948..bc06ee1 100644 --- a/routes/update.py +++ b/routes/update.py @@ -20,7 +20,7 @@ def update(primary_value): if request.method == 'GET': # 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': # Get data from form @@ -29,7 +29,7 @@ def update(primary_value): # Form validation error = validate_values(form_data) 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 for attrib in item_attributes: @@ -40,15 +40,15 @@ def update(primary_value): except exc.IntegrityError: # 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) + return render_template('item_form.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) + return render_template('item_form.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) + return render_template('item_form.html', item=item, item_attributes=item_attributes, error=error) # Redirect to /viewall on success return redirect('/viewall') \ No newline at end of file diff --git a/templates/create.html b/templates/item_form.html similarity index 72% rename from templates/create.html rename to templates/item_form.html index 06ce7b3..68793c9 100644 --- a/templates/create.html +++ b/templates/item_form.html @@ -2,17 +2,17 @@ - Add an Item + {% if item %}Update{% else %}Add{% endif %} Item -

Add new Item

+

{% if item %}Update{% else %}Add{% endif %} Item

{% if error %}

{{ error }}

{% endif %} - +
{% for attrib in item_attributes -%}

@@ -25,7 +25,7 @@ {% if attrib.required %} required {% endif %} > {% for option in attrib.options -%} - + {% endfor %} {% else %} @@ -41,12 +41,16 @@ {% 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.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 %}

{% endfor %} -

+

diff --git a/templates/update.html b/templates/update.html deleted file mode 100644 index 628f202..0000000 --- a/templates/update.html +++ /dev/null @@ -1,51 +0,0 @@ - - - - - Update Item - - -

Update Item

- - - {% if error %} -

{{ error }}

- {% endif %} - - -
- {% for attrib in item_attributes -%} -

- - {% if attrib.html_input_type == "select" %} - - - {% else %} - - - {% endif %} -

- {% endfor %} -

- -
- - \ No newline at end of file