(20251114) Both APIs ready for production test.

This commit is contained in:
2025-11-14 19:06:00 +05:30
parent 6ddc0e6966
commit a2f6390755
7 changed files with 440 additions and 56 deletions
@@ -136,7 +136,7 @@ def init(blueprint_setup_state):
# @log_chain_to_mongo(attr_name = "logs_mongo")
@should_not_be_under_maintenance(attr_name = "is_under_maintenance")
@validate_input(
header_validator = lambda x: SimpleCosecCredentialsHeaders(**x).model_dump(),
header_validator = lambda x: SimpleCosecCredentialsHeaders(**x),
data_validator = lambda x: CosecInOutReportRequestData(**x)
)
@handle_cancelled_request()
@@ -156,8 +156,6 @@ async def in_out_report_generate(
:return: A standard response structure.
"""
current_app.printer("Hi")
# Start by assuming failure:
success = False
report_data = None
@@ -165,7 +163,7 @@ async def in_out_report_generate(
# Figure out the paths:
base_dir = files.get_parent_directory(
files.get_file_directory(include_filename = False),
depth = 1
depth = 4
)
# Put together the directory for the drivers, the downloads, etc.:
@@ -0,0 +1,246 @@
"""
AUTHOR:
Khushal P Soonderji
DATE:
Fri, 14th Nov, 2025
OBJECTIVE:
To build APIs to serve Muster Roll Reports from Cosec's Web portal.
REFERENCES:
N/A
DOWNLOADS:
N/A
NOTES:
N/A
"""
# *****************************************************************************************************************
# ***** ****
# *** IMPORT ***
# ***** ****
# *****************************************************************************************************************
# To make sibling directories accessible for imports:
import sys
sys.path.append(".")
sys.path.append("..")
# For system-level activities:
import os
# For using Quart:
from quart import Blueprint, current_app, request
# My utils:
from utils_v2.string import json
from utils_v2.date_time import date_time
from utils_v2.system import files
from utils_v2.api.codes import StatusCodes, HttpCodes
from utils_v2.api.response import ResponseModel
from utils_v2.api.async_quart import (
set_api_version,
read_input,
get_session_info,
log_request_to_mongo,
log_chain_to_mongo,
should_not_be_under_maintenance,
only_whitelisted_ips,
limit_rate,
validate_input,
handle_cancelled_request
)
# Models:
from backend.models.api.common import SimpleCosecCredentialsHeaders
from backend.models.api.reports.muster_roll import CosecMusterRollReportRequestData
# Cosec-related:
from cosec_web.cosec_web import CosecWeb
# Common:
from backend.shared import constants
# To work with dat and time:
import datetime
# For asynchronous activities:
import asyncio
# *****************************************************************************************************************
# ***** ****
# *** MACROS / ONE-TIME INIT ***
# ***** ****
# *****************************************************************************************************************
# Related to Quart:
muster_roll_report_bp = Blueprint("muster_roll_report", __name__)
# *****************************************************************************************************************
# ***** ****
# *** VARIABLES ***
# ***** ****
# *****************************************************************************************************************
# --- Nothing Yet
# *****************************************************************************************************************
# ***** ****
# *** FUNCTIONS ***
# ***** ****
# *****************************************************************************************************************
@muster_roll_report_bp.record_once
def init(blueprint_setup_state):
# This gets called when the blueprints is registered.
# Consider this to be a one-time setup for the whole blueprints:
pass
# ---------------------------------------------------------------------------------------------------------------------
@muster_roll_report_bp.route("/generate", methods = ["GET"])
@set_api_version(api_version = "1.0.0")
@read_input(sanitize_headers = False, sanitize_data = False)
# @get_session_info(key = "X-User-Id", session_coro = "get_session")
# @log_request_to_mongo(
# attr_name = "logs_mongo",
# project = constants.PROJECT_NAME,
# log_type = constants.MODULE_NAME,
# operation = "ytLnkAddApi",
# log_input = True,
# log_output = True,
# sensitive_keys = None
# )
# @log_chain_to_mongo(attr_name = "logs_mongo")
@should_not_be_under_maintenance(attr_name = "is_under_maintenance")
@validate_input(
header_validator = lambda x: SimpleCosecCredentialsHeaders(**x),
data_validator = lambda x: CosecMusterRollReportRequestData(**x)
)
@handle_cancelled_request()
async def muster_roll_report_generate(
inbound_headers: SimpleCosecCredentialsHeaders | dict = None,
inbound_data: CosecMusterRollReportRequestData | dict = None,
inbound_files: dict = None,
**kwargs
):
"""
To automate the web browser interaction with Cosec's web portal and return a JSON of the actual report content.
:param inbound_headers: auto-extracted by the decorators.
:param inbound_data: auto-extracted by the decorators.
:param inbound_files: auto-extracted by the decorators.
:param kwargs: Any number of extra inputs supplied by the decorators.
:return: A standard response structure.
"""
# Start by assuming failure:
success = False
report_data = None
# Figure out the paths:
base_dir = files.get_parent_directory(
files.get_file_directory(include_filename = False),
depth = 4
)
# Put together the directory for the drivers, the downloads, etc.:
chrome_driver_dir = os.path.join(base_dir, r"drivers/chrome")
user_data_dir = os.path.join(base_dir, r"browser/user_data")
downloads_dir = os.path.join(base_dir, r"downloads")
# Show all the paths for debugging:
current_app.printer("PATHS:", chrome_driver_dir, user_data_dir, downloads_dir)
# Create an instance of the automation object:
cosec = CosecWeb(
cosec_url = inbound_headers.cosecUrl,
username = inbound_headers.cosecUsername,
password = inbound_headers.cosecPassword,
driver_dir = chrome_driver_dir,
user_data_dir = user_data_dir,
downloads_dir = downloads_dir,
)
# Perform the login:
cosec.login(initial_sleep = 2.5)
# Get the in/out report:
report_path = cosec.get_muster_roll(
initial_sleep = 1.0,
on_date = inbound_data.onDate,
group_ids = inbound_data.groupIds,
download_timeout = 60.0
)
# Log out to end the cycle:
cosec.logout()
# Close the browser window:
cosec.quit()
# If we didn't get any path, the download failed:
if report_path is None:
success = False
# Convert the In/Out Summary to a Pandas DF:
else:
success = True
report_df = cosec.read_muster_roll_xls(report_path)
report_df = report_df[[
"User ID", "User Name", "Category Name",
"Grade Name", "Branch Name", "Department Name",
"Direct Reporting", "Level-1"
]]
report_data = report_df.to_dict(orient = "records")
# ┳┓
# ┣┫┏┓┏┏┓┏┓┏┓┏┏┓
# ┛┗┗ ┛┣┛┗┛┛┗┛┗
# ┛
if success: return ResponseModel(
status_code = StatusCodes.OK,
http_code = HttpCodes.SUCCESS,
data = report_data,
message = f"Successfully fetched {len(report_data)} records."
)
else: return ResponseModel(
status_code = StatusCodes.FAILED,
http_code = HttpCodes.INTERNAL_SERVER_ERROR,
message = f"Failed to fetch records. Please try again."
)
# *****************************************************************************************************************
# ***** ****
# *** MAIN PROGRAM ***
# ***** ****
# *****************************************************************************************************************
if __name__ == "__main__":
pass