From b78526ad9fb9ce75e06fa3a4d28948592235738e Mon Sep 17 00:00:00 2001 From: Swapnil9693 Date: Tue, 24 Mar 2026 13:41:00 +0530 Subject: [PATCH] Village Add, Edit and Delete Messages done --- controllers/village_controller.py | 20 +- static/js/village.js | 346 ++++++++++++++---------------- templates/add_village.html | 186 +++++++++------- templates/edit_village.html | 24 +-- 4 files changed, 298 insertions(+), 278 deletions(-) diff --git a/controllers/village_controller.py b/controllers/village_controller.py index 8612354..27a1ff2 100644 --- a/controllers/village_controller.py +++ b/controllers/village_controller.py @@ -83,17 +83,29 @@ def check_village(): return village.CheckVillage(request=request) -# ------------------------- Delete Village ------------------------- @village_bp.route('/delete_village/') @login_required def delete_village(village_id): - village = Village() village.DeleteVillage(request=request, village_id=village_id) - flash(village.resultMessage, "success" if village.isSuccess else "error") - return redirect(url_for('village.add_village')) + # ✅ Convert resultMessage to string if it's a Response or tuple + raw_msg = village.resultMessage + if isinstance(raw_msg, tuple): + # e.g., (, 200) + msg = "Village deleted successfully!" + elif hasattr(raw_msg, 'get_data'): + # Flask Response object + msg = raw_msg.get_data(as_text=True) # get raw text + else: + # fallback + msg = str(raw_msg) if raw_msg else "Village deleted successfully!" + + return jsonify({ + "status": "success" if village.isSuccess else "error", + "message": msg + }) # ------------------------- Edit Village ------------------------- @village_bp.route('/edit_village/', methods=['GET', 'POST']) diff --git a/static/js/village.js b/static/js/village.js index 94917f7..2a6f4fd 100644 --- a/static/js/village.js +++ b/static/js/village.js @@ -1,264 +1,250 @@ - - window.onload = function () { - document.getElementById('Village_Name').focus(); + if (document.getElementById('Village_Name')) { + document.getElementById('Village_Name').focus(); + } }; $(document).ready(function () { - // 🔥 RESTORE VIEW MODE AFTER RELOAD - var viewMode = localStorage.getItem("viewMode"); + // ✅ RUN ONLY IF THIS PAGE HAS FORM/TABLE + if ($('#addForm').length && $('#addTable').length) { - if (viewMode === "table") { - $('#addForm').hide(); - $('#addTable').show(); - } else { + // ✅ DEFAULT VIEW → SHOW FORM $('#addForm').show(); $('#addTable').hide(); + + + // 🔥 BUTTON TOGGLE + $('#addButton').click(function () { + $('#addForm').show(); + $('#addTable').hide(); + }); + + $('#displayButton').click(function () { + $('#addForm').hide(); + $('#addTable').show(); + }); } - // 🔥 BUTTON TOGGLE LOGIC - - $('#addButton').click(function () { - $('#addForm').show(); - $('#addTable').hide(); - - localStorage.setItem("viewMode", "form"); - }); - - $('#displayButton').click(function () { - $('#addForm').hide(); - $('#addTable').show(); - - localStorage.setItem("viewMode", "table"); - }); - - + // =============================== // STATE → DISTRICT - $('#state_Id').change(function () { + // =============================== + if ($('#state_Id').length) { - var stateId = $(this).val(); + $('#state_Id').change(function () { - if (stateId) { + var stateId = $(this).val(); - $.ajax({ - url: '/get_districts/' + stateId, - type: 'GET', + if (stateId) { - success: function (data) { + $.ajax({ + url: '/get_districts/' + stateId, + type: 'GET', - var districtDropdown = $('#district_Id'); + success: function (data) { - districtDropdown.empty(); - districtDropdown.append(''); + var districtDropdown = $('#district_Id'); - data.forEach(function (district) { + districtDropdown.empty(); + districtDropdown.append(''); - districtDropdown.append( - '' - ); + data.forEach(function (district) { + districtDropdown.append( + '' + ); + }); - }); - - districtDropdown.prop('disabled', false); - - } - - }); - - } - - }); + districtDropdown.prop('disabled', false); + } + }); + } + }); + } + // =============================== // DISTRICT → BLOCK - $('#district_Id').change(function () { + // =============================== + if ($('#district_Id').length) { - var districtId = $(this).val(); + $('#district_Id').change(function () { - if (districtId) { + var districtId = $(this).val(); - $.ajax({ - url: '/get_blocks/' + districtId, - type: 'GET', + if (districtId) { - success: function (data) { + $.ajax({ + url: '/get_blocks/' + districtId, + type: 'GET', - var blockDropdown = $('#block_Id'); + success: function (data) { - blockDropdown.empty(); - blockDropdown.append(''); + var blockDropdown = $('#block_Id'); - data.forEach(function (block) { + blockDropdown.empty(); + blockDropdown.append(''); - blockDropdown.append( - '' - ); + data.forEach(function (block) { + blockDropdown.append( + '' + ); + }); - }); - - blockDropdown.prop('disabled', false); - - } - - }); - - } - - }); + blockDropdown.prop('disabled', false); + } + }); + } + }); + } + // =============================== // VILLAGE NAME VALIDATION - $('#Village_Name').on('input', function () { + // =============================== + if ($('#Village_Name').length) { - var villageName = $(this).val(); - var validPattern = /^[A-Za-z ]*$/; + $('#Village_Name').on('input', function () { - if (!validPattern.test(villageName)) { + var villageName = $(this).val(); + var validPattern = /^[A-Za-z ]*$/; - $('#villageMessage') - .text('Only letters and spaces are allowed!') - .css('color', 'red'); + if (!validPattern.test(villageName)) { - $('#submitVillage').prop('disabled', true); + $('#villageMessage') + .text('Only letters and spaces are allowed!') + .css('color', 'red'); - } else { + $('#submitVillage').prop('disabled', true); - $('#villageMessage').text(''); - $('#submitVillage').prop('disabled', false); + } else { - } - - }); + $('#villageMessage').text(''); + $('#submitVillage').prop('disabled', false); + } + }); + } + // =============================== // CHECK DUPLICATE VILLAGE - $('#Village_Name, #block_Id').on('change keyup', function () { + // =============================== + if ($('#Village_Name').length && $('#block_Id').length) { - var blockId = $('#block_Id').val(); - var villageName = $('#Village_Name').val().trim(); + $('#Village_Name, #block_Id').on('change keyup', function () { - if (blockId && villageName) { + var blockId = $('#block_Id').val(); + var villageName = $('#Village_Name').val().trim(); - $.ajax({ + if (blockId && villageName) { - url: '/check_village', - type: 'POST', + $.ajax({ + url: '/check_village', + type: 'POST', - data: { - block_Id: blockId, - Village_Name: villageName - }, + data: { + block_Id: blockId, + Village_Name: villageName + }, - success: function (response) { + success: function (response) { - if (response.status === 'exists') { + if (response.status === 'exists') { + + $('#villageMessage') + .text(response.message) + .css('color', 'red'); + + $('#submitVillage').prop('disabled', true); + + } else { + + $('#villageMessage') + .text(response.message) + .css('color', 'green'); + + $('#submitVillage').prop('disabled', false); + } + }, + + error: function () { $('#villageMessage') - .text(response.message) + .text('Error checking village name') .css('color', 'red'); $('#submitVillage').prop('disabled', true); + } + }); + } + }); + } + + + // =============================== + // ADD VILLAGE (SAFE SCOPE FIX) + // =============================== + if ($('#villageForm').length) { + + $('#villageForm').submit(function (event) { + + event.preventDefault(); + + $.ajax({ + url: '/add_village', + type: 'POST', + data: $(this).serialize(), + + success: function (response) { + + if (response && response.status === 'success') { + + alert(response.message || 'Village added successfully!'); + + // ✅ clear form + $('#villageForm')[0].reset(); + + // ✅ switch to table + $('#addForm').hide(); + $('#addTable').show(); + + // optional refresh + location.reload(); } else { - - $('#villageMessage') - .text(response.message) - .css('color', 'green'); - - $('#submitVillage').prop('disabled', false); - + alert(response.message || 'Error adding village. Please try again.'); } - }, error: function () { - - $('#villageMessage') - .text('Error checking village name') - .css('color', 'red'); - - $('#submitVillage').prop('disabled', true); - + alert('An error occurred. Please try again.'); } - }); - } - - }); - - - // ADD VILLAGE - $('#villageForm').submit(function (event) { - - event.preventDefault(); - - $.ajax({ - - url: '/add_village', - type: 'POST', - data: $(this).serialize(), - - success: function (response) { - - if (response.status === 'success') { - - alert('Village added successfully!'); - location.reload(); - - } else { - - alert(response.message || 'Error adding village. Please try again.'); - - } - - }, - - error: function () { - - alert('An error occurred. Please try again.'); - - } - }); - - }); + } }); -// 🔥 DELETE FUNCTION (UPDATED) -function deleteVillage(villageId) { - if (!confirm("Are you sure you want to delete this village?")) { - return; - } - // ✅ save that user is on table - localStorage.setItem("viewMode", "table"); +// =============================== +// DELETE FUNCTION (SAFE) +// =============================== +function deleteVillage(villageId, element) { + if (!confirm("Are you sure you want to delete this village?")) return; $.ajax({ url: '/delete_village/' + villageId, type: 'GET', - - success: function () { - - setTimeout(function () { - - alert("Village deleted successfully!"); - - // reload but stay on table - location.reload(); - - }, 1000); - + dataType: 'json', + success: function (response) { + alert(response.message); // ✅ now shows "Village deleted successfully!" + if (element) $(element).closest("tr").remove(); }, - error: function () { alert("Error deleting village. Please try again."); } - }); } \ No newline at end of file diff --git a/templates/add_village.html b/templates/add_village.html index 8adf450..0c4c4d5 100644 --- a/templates/add_village.html +++ b/templates/add_village.html @@ -1,99 +1,129 @@ -{% extends 'base.html' %} -{% block content %} +{% extends 'base.html' %} {% block content %} - - - Village Management - - - + + + Village Management + + + - - -
+ +
-
+
- - + +{% with messages = get_flashed_messages(with_categories=true) %} + {% if messages %} + + {% endif %} +{% endwith %} {% endblock %} diff --git a/templates/edit_village.html b/templates/edit_village.html index c4e7afe..f2b4dc2 100644 --- a/templates/edit_village.html +++ b/templates/edit_village.html @@ -28,25 +28,17 @@ - - - + + - + {% endblock %}