249 lines
8.7 KiB
Python
249 lines
8.7 KiB
Python
|
|
|
||
|
|
from flask_login import LoginManager, UserMixin, login_user, logout_user, login_required, current_user
|
||
|
|
|
||
|
|
from AppCode.Utilities import RegEx, ResponseHandler, HtmlHelper, ItemCRUDType
|
||
|
|
from AppCode.Log import LogData, LogHelper
|
||
|
|
|
||
|
|
import os
|
||
|
|
import config
|
||
|
|
import re
|
||
|
|
|
||
|
|
import mysql.connector
|
||
|
|
from mysql.connector import Error
|
||
|
|
|
||
|
|
|
||
|
|
class itemCRUDMapping:
|
||
|
|
name = ""
|
||
|
|
|
||
|
|
def __init__(self, itemType):
|
||
|
|
if itemType is ItemCRUDType.Village:
|
||
|
|
self.name = "Village"
|
||
|
|
elif itemType is ItemCRUDType.Block:
|
||
|
|
self.name = "Block"
|
||
|
|
elif itemType is ItemCRUDType.State:
|
||
|
|
self.name = "State"
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
class ItemCRUD:
|
||
|
|
isSuccess = False
|
||
|
|
resultMessage = ""
|
||
|
|
itemCRUDType = ItemCRUDType.Village
|
||
|
|
itemCRUDMapping = ItemCRUDType(itemCRUDType)
|
||
|
|
#itemCRUDMapping itemCRUDMapping
|
||
|
|
|
||
|
|
|
||
|
|
def __init__(self, itemType):
|
||
|
|
self.isSuccess = False
|
||
|
|
self.resultMessage = ""
|
||
|
|
self.itemCRUDType = itemType
|
||
|
|
self.itemCRUDMapping = ItemCRUDType(self.itemCRUDType)
|
||
|
|
|
||
|
|
|
||
|
|
def DeleteItem(self, request, itemID, storedprocDelete):
|
||
|
|
self.isSuccess = False
|
||
|
|
self.resultMessage = ""
|
||
|
|
connection = config.get_db_connection()
|
||
|
|
cursor = connection.cursor()
|
||
|
|
LogHelper.log_action("Delete Village", f"User {current_user.id} deleted village '{itemID}'")
|
||
|
|
|
||
|
|
try:
|
||
|
|
cursor.callproc(storedprocDelete, (itemID,))
|
||
|
|
connection.commit()
|
||
|
|
self.resultMessage = ResponseHandler.delete_success(self.itemCRUDMapping.name) # Simple message, route will handle redirect
|
||
|
|
self.isSuccess = True
|
||
|
|
except mysql.connector.Error as e:
|
||
|
|
print(f"Error deleting village: {e}")
|
||
|
|
self.isSuccess = False
|
||
|
|
self.resultMessage = HtmlHelper.json_response(ResponseHandler.delete_failure(self.itemCRUDMapping.name), 500)
|
||
|
|
finally:
|
||
|
|
cursor.close()
|
||
|
|
connection.close()
|
||
|
|
|
||
|
|
#return self.resultMessage
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
def AddItem(self, request, parentid, childname, storedprocfetch, storedprocadd):
|
||
|
|
|
||
|
|
connection = config.get_db_connection()
|
||
|
|
if not connection:
|
||
|
|
self.isSuccess = False
|
||
|
|
self.resultMessage = HtmlHelper.json_response(ResponseHandler.db_connection_failure(), 500)
|
||
|
|
return
|
||
|
|
|
||
|
|
cursor = connection.cursor()
|
||
|
|
|
||
|
|
|
||
|
|
LogHelper.log_action(f"Add '{self.itemCRUDMapping.name}'",
|
||
|
|
f"User {current_user.id} adding '{self.itemCRUDMapping.name}' '{childname}' to block '{parentid}'")
|
||
|
|
|
||
|
|
if not parentid:
|
||
|
|
self.isSuccess = False
|
||
|
|
self.resultMessage = HtmlHelper.json_response(ResponseHandler.add_failure("block"),
|
||
|
|
400) # Assuming this is a valid response
|
||
|
|
return
|
||
|
|
|
||
|
|
if not re.match(RegEx.patternAlphabetOnly, childname):
|
||
|
|
self.isSuccess = False
|
||
|
|
self.resultMessage = HtmlHelper.json_response(ResponseHandler.invalid_name(self.itemCRUDMapping.name), 400)
|
||
|
|
return
|
||
|
|
|
||
|
|
try:
|
||
|
|
# Check if the village already exists in the block
|
||
|
|
cursor.callproc(storedprocfetch, (childname, parentid,))
|
||
|
|
for rs in cursor.stored_results():
|
||
|
|
existing_item = rs.fetchone()
|
||
|
|
|
||
|
|
if existing_item:
|
||
|
|
print("Existing ", self.itemCRUDMapping.name)
|
||
|
|
self.isSuccess = False
|
||
|
|
self.resultMessage = HtmlHelper.json_response(ResponseHandler.already_exists(self.itemCRUDMapping.name), 409)
|
||
|
|
return
|
||
|
|
|
||
|
|
# Insert new village
|
||
|
|
cursor.callproc(storedprocadd, (childname, parentid))
|
||
|
|
connection.commit()
|
||
|
|
self.isSuccess = True
|
||
|
|
self.resultMessage = HtmlHelper.json_response(ResponseHandler.add_success(self.itemCRUDMapping.name), 200)
|
||
|
|
return
|
||
|
|
|
||
|
|
except mysql.connector.Error as e:
|
||
|
|
print(f"Database Error: {e}")
|
||
|
|
print("DatabaseError")
|
||
|
|
self.isSuccess = False
|
||
|
|
self.resultMessage = HtmlHelper.json_response(ResponseHandler.add_failure("village"), 500)
|
||
|
|
return
|
||
|
|
finally:
|
||
|
|
cursor.close()
|
||
|
|
connection.close()
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
def EditItem(self, request, childid, parentid, childname, storedprocupdate):
|
||
|
|
"""Handles the POST logic for updating a district."""
|
||
|
|
self.isSuccess = False
|
||
|
|
self.resultMessage = ""
|
||
|
|
connection = config.get_db_connection()
|
||
|
|
cursor = connection.cursor()
|
||
|
|
|
||
|
|
#district_name = request.form['district_Name'].strip()
|
||
|
|
#state_id = request.form['state_Id']
|
||
|
|
LogHelper.log_action("Edit District", f"User {current_user.id} Edited District '{childid}'")
|
||
|
|
|
||
|
|
#Need to add validation to see if item exits
|
||
|
|
|
||
|
|
# Added validation consistent with your other Edit methods
|
||
|
|
if not re.match(RegEx.patternAlphabetOnly, childname):
|
||
|
|
self.isSuccess = False
|
||
|
|
self.resultMessage = ResponseHandler.invalid_name(self.itemCRUDMapping.name), 400
|
||
|
|
return self.resultMessage
|
||
|
|
|
||
|
|
try:
|
||
|
|
cursor.callproc(storedprocupdate, (childid, parentid, childname,))
|
||
|
|
connection.commit()
|
||
|
|
self.isSuccess = True
|
||
|
|
self.resultMessage = "Successfully Edited"
|
||
|
|
except mysql.connector.Error as e:
|
||
|
|
print(f"Error updating district: {e}")
|
||
|
|
self.isSuccess = False
|
||
|
|
self.resultMessage = ResponseHandler.update_failure(self.itemCRUDMapping.name), 500
|
||
|
|
finally:
|
||
|
|
cursor.close()
|
||
|
|
connection.close()
|
||
|
|
|
||
|
|
return self.resultMessage
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
def GetAllData(self, request, storedproc):
|
||
|
|
|
||
|
|
data = []
|
||
|
|
connection = config.get_db_connection()
|
||
|
|
|
||
|
|
self.isSuccess = False
|
||
|
|
self.resultMessage = ""
|
||
|
|
|
||
|
|
if not connection:
|
||
|
|
return []
|
||
|
|
|
||
|
|
cursor = connection.cursor()
|
||
|
|
|
||
|
|
try:
|
||
|
|
cursor.callproc(storedproc)
|
||
|
|
for result in cursor.stored_results():
|
||
|
|
data = result.fetchall()
|
||
|
|
self.isSuccess = True
|
||
|
|
|
||
|
|
except mysql.connector.Error as e:
|
||
|
|
print(f"Error fetching villages: {e}")
|
||
|
|
self.isSuccess = False
|
||
|
|
self.resultMessage = HtmlHelper.json_response(ResponseHandler.fetch_failure(self.itemCRUDMapping.name), 500)
|
||
|
|
return []
|
||
|
|
|
||
|
|
finally:
|
||
|
|
cursor.close()
|
||
|
|
connection.close()
|
||
|
|
|
||
|
|
return data
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
def GetDataByID(self, id, storedproc):
|
||
|
|
data = None
|
||
|
|
connection = config.get_db_connection()
|
||
|
|
cursor = connection.cursor()
|
||
|
|
|
||
|
|
try:
|
||
|
|
cursor.callproc(storedproc, (id,))
|
||
|
|
for rs in cursor.stored_results():
|
||
|
|
data = rs.fetchone()
|
||
|
|
|
||
|
|
except mysql.connector.Error as e:
|
||
|
|
print(f"Error fetching block data: {e}")
|
||
|
|
return None
|
||
|
|
finally:
|
||
|
|
cursor.close()
|
||
|
|
connection.close()
|
||
|
|
|
||
|
|
return data
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
def CheckItem(self, request, parentid, childname, storedprocfetch):
|
||
|
|
self.isSuccess = False
|
||
|
|
self.resultMessage = ""
|
||
|
|
connection = config.get_db_connection()
|
||
|
|
cursor = connection.cursor()
|
||
|
|
|
||
|
|
LogHelper.log_action("Check Block", f"User {current_user.id} Checked block '{childname}'")
|
||
|
|
|
||
|
|
if not re.match(RegEx.patternAlphabetOnly, childname):
|
||
|
|
self.isSuccess = False
|
||
|
|
self.resultMessage = HtmlHelper.json_response(ResponseHandler.invalid_name(self.itemCRUDMapping.name), 400)
|
||
|
|
return HtmlHelper.json_response(ResponseHandler.invalid_name(self.itemCRUDMapping.name), 400)
|
||
|
|
|
||
|
|
try:
|
||
|
|
cursor.callproc(storedprocfetch, (childname, parentid))
|
||
|
|
for rs in cursor.stored_results():
|
||
|
|
existing_item = rs.fetchone()
|
||
|
|
|
||
|
|
if existing_item:
|
||
|
|
self.isSuccess = False
|
||
|
|
self.resultMessage = HtmlHelper.json_response(ResponseHandler.already_exists(self.itemCRUDMapping.name), 409)
|
||
|
|
return HtmlHelper.json_response(ResponseHandler.already_exists(self.itemCRUDMapping.name), 409)
|
||
|
|
|
||
|
|
self.isSuccess = True
|
||
|
|
self.resultMessage = HtmlHelper.json_response(ResponseHandler.is_available(self.itemCRUDMapping.name), 200)
|
||
|
|
return HtmlHelper.json_response(ResponseHandler.is_available(self.itemCRUDMapping.name), 200)
|
||
|
|
|
||
|
|
except mysql.connector.Error as e:
|
||
|
|
print(f"Error checking block: {e}")
|
||
|
|
self.isSuccess = False
|
||
|
|
self.resultMessage = HtmlHelper.json_response(ResponseHandler.fetch_failure(self.itemCRUDMapping.name), 500)
|
||
|
|
return HtmlHelper.json_response(ResponseHandler.fetch_failure(self.itemCRUDMapping.name), 500)
|
||
|
|
|
||
|
|
finally:
|
||
|
|
cursor.close()
|
||
|
|
connection.close()
|