Initial commit
This commit is contained in:
208
AppCode/Block.py
Normal file
208
AppCode/Block.py
Normal file
@@ -0,0 +1,208 @@
|
||||
from flask import Flask, render_template, request, redirect, url_for, send_from_directory, flash, jsonify, json
|
||||
from flask import current_app
|
||||
|
||||
from datetime import datetime
|
||||
from flask_login import LoginManager, UserMixin, login_user, logout_user, login_required, current_user
|
||||
|
||||
from AppCode.Utilities import RegEx, ResponseHandler, HtmlHelper
|
||||
from AppCode.Log import LogData, LogHelper
|
||||
|
||||
import os
|
||||
import config
|
||||
import re
|
||||
|
||||
import mysql.connector
|
||||
from mysql.connector import Error
|
||||
|
||||
|
||||
class Block:
|
||||
|
||||
isSuccess = False
|
||||
resultMessage = ""
|
||||
|
||||
def __init__(self):
|
||||
self.isSuccess = False
|
||||
self.resultMessage = ""
|
||||
|
||||
# ----------------------------------------------------------
|
||||
# Add Block
|
||||
# ----------------------------------------------------------
|
||||
def AddBlock(self, request):
|
||||
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()
|
||||
|
||||
block_name = request.form['block_Name'].strip()
|
||||
district_id = request.form['district_Id']
|
||||
|
||||
LogHelper.log_action("Add Block", f"User {current_user.id} Added Block '{block_name}'")
|
||||
|
||||
if not re.match(RegEx.patternAlphabetOnly, block_name):
|
||||
self.isSuccess = False
|
||||
self.resultMessage = HtmlHelper.json_response(ResponseHandler.invalid_name("block"), 400)
|
||||
return
|
||||
|
||||
try:
|
||||
cursor.callproc("GetBlockByNameAndDistrict", (block_name, district_id))
|
||||
for rs in cursor.stored_results():
|
||||
existing_block = rs.fetchone()
|
||||
|
||||
if existing_block:
|
||||
self.isSuccess = False
|
||||
self.resultMessage = HtmlHelper.json_response(ResponseHandler.already_exists("block"), 409)
|
||||
return
|
||||
|
||||
cursor.callproc("SaveBlock", (block_name, district_id))
|
||||
connection.commit()
|
||||
|
||||
self.isSuccess = True
|
||||
self.resultMessage = HtmlHelper.json_response(ResponseHandler.add_success("block"), 200)
|
||||
return
|
||||
|
||||
except mysql.connector.Error as e:
|
||||
print(f"Error inserting block: {e}")
|
||||
self.isSuccess = False
|
||||
self.resultMessage = HtmlHelper.json_response(ResponseHandler.add_failure("block"), 500)
|
||||
finally:
|
||||
cursor.close()
|
||||
connection.close()
|
||||
|
||||
# ----------------------------------------------------------
|
||||
# Get All Blocks
|
||||
# ----------------------------------------------------------
|
||||
def GetAllBlocks(self):
|
||||
blockdata = []
|
||||
connection = config.get_db_connection()
|
||||
|
||||
if not connection:
|
||||
return []
|
||||
|
||||
cursor = connection.cursor()
|
||||
|
||||
try:
|
||||
cursor.callproc("GetAllBlocks")
|
||||
for rs in cursor.stored_results():
|
||||
blockdata = rs.fetchall()
|
||||
|
||||
self.isSuccess = True
|
||||
|
||||
except mysql.connector.Error as e:
|
||||
print(f"Error fetching blocks: {e}")
|
||||
self.isSuccess = False
|
||||
self.resultMessage = HtmlHelper.json_response(ResponseHandler.fetch_failure("blocks"), 500)
|
||||
return []
|
||||
|
||||
finally:
|
||||
cursor.close()
|
||||
connection.close()
|
||||
|
||||
return blockdata
|
||||
|
||||
# ----------------------------------------------------------
|
||||
# Check Block Exists
|
||||
# ----------------------------------------------------------
|
||||
def CheckBlock(self, request):
|
||||
connection = config.get_db_connection()
|
||||
cursor = connection.cursor()
|
||||
|
||||
block_name = request.json.get('block_Name', '').strip()
|
||||
district_id = request.json.get('district_Id')
|
||||
|
||||
LogHelper.log_action("Check Block", f"User {current_user.id} Checked block '{block_name}'")
|
||||
|
||||
if not re.match(RegEx.patternAlphabetOnly, block_name):
|
||||
return HtmlHelper.json_response(ResponseHandler.invalid_name("block"), 400)
|
||||
|
||||
try:
|
||||
cursor.callproc("GetBlockByNameAndDistrict", (block_name, district_id))
|
||||
for rs in cursor.stored_results():
|
||||
existing_block = rs.fetchone()
|
||||
|
||||
if existing_block:
|
||||
return HtmlHelper.json_response(ResponseHandler.already_exists("block"), 409)
|
||||
|
||||
return HtmlHelper.json_response(ResponseHandler.is_available("block"), 200)
|
||||
|
||||
except mysql.connector.Error as e:
|
||||
print(f"Error checking block: {e}")
|
||||
return HtmlHelper.json_response(ResponseHandler.fetch_failure("block"), 500)
|
||||
|
||||
finally:
|
||||
cursor.close()
|
||||
connection.close()
|
||||
|
||||
# ----------------------------------------------------------
|
||||
# Get Block By ID
|
||||
# ----------------------------------------------------------
|
||||
def GetBlockByID(self, id):
|
||||
blockdata = None
|
||||
connection = config.get_db_connection()
|
||||
cursor = connection.cursor()
|
||||
|
||||
try:
|
||||
cursor.callproc("GetBlockDataByID", (id,))
|
||||
for rs in cursor.stored_results():
|
||||
blockdata = rs.fetchone()
|
||||
|
||||
except mysql.connector.Error as e:
|
||||
print(f"Error fetching block data: {e}")
|
||||
return None
|
||||
finally:
|
||||
cursor.close()
|
||||
connection.close()
|
||||
|
||||
return blockdata
|
||||
|
||||
# ----------------------------------------------------------
|
||||
# Update Block
|
||||
# ----------------------------------------------------------
|
||||
def EditBlock(self, request, id):
|
||||
connection = config.get_db_connection()
|
||||
cursor = connection.cursor()
|
||||
|
||||
block_name = request.form['block_Name'].strip()
|
||||
district_id = request.form['district_Id']
|
||||
|
||||
LogHelper.log_action("Edit Block", f"User {current_user.id} Edited block '{id}'")
|
||||
|
||||
if not re.match(RegEx.patternAlphabetOnly, block_name):
|
||||
return HtmlHelper.json_response(ResponseHandler.invalid_name("block"), 400)
|
||||
|
||||
try:
|
||||
cursor.callproc("UpdateBlockById", (block_name, district_id, id))
|
||||
connection.commit()
|
||||
return "Successfully Updated"
|
||||
|
||||
except mysql.connector.Error as e:
|
||||
print(f"Error updating block: {e}")
|
||||
return HtmlHelper.json_response(ResponseHandler.update_failure("block"), 500)
|
||||
|
||||
finally:
|
||||
cursor.close()
|
||||
connection.close()
|
||||
|
||||
# ----------------------------------------------------------
|
||||
# Delete Block
|
||||
# ----------------------------------------------------------
|
||||
def DeleteBlock(self, id):
|
||||
connection = config.get_db_connection()
|
||||
cursor = connection.cursor()
|
||||
|
||||
LogHelper.log_action("Delete Block", f"User {current_user.id} Deleted block '{id}'")
|
||||
|
||||
try:
|
||||
cursor.callproc("DeleteBlock", (id,))
|
||||
connection.commit()
|
||||
return "Successfully Deleted"
|
||||
|
||||
except mysql.connector.Error as e:
|
||||
print(f"Error deleting block: {e}")
|
||||
return HtmlHelper.json_response(ResponseHandler.delete_failure("block"), 500)
|
||||
|
||||
finally:
|
||||
cursor.close()
|
||||
connection.close()
|
||||
Reference in New Issue
Block a user