""" 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 # Helpers: from backend.api.helpers import muster_roll # 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 # --------------------------------------------------------------------------------------------------------------------- async def close_browser() -> None: current_app.printer("Closing failed browser session.") current_app.cosec.quit() current_app.printer("Cleanup attempt completed.") # --------------------------------------------------------------------------------------------------------------------- @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( cleanup_coro = close_browser ) 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 # Get the muster roll data: report_data = muster_roll.load( cosec_url = inbound_headers.cosecUrl, username = inbound_headers.cosecUsername, password = inbound_headers.cosecPassword, group_ids = inbound_data.groupIds, on_date = inbound_data.onDate, ) # If data was loaded: if report_data is not None: report_data = report_data.copy() report_data = report_data[[ "User ID", "User Name", "Category Name", "Grade Name", "Branch Name", "Department Name", "Direct Reporting", "Level-1" ]] unique_branches = report_data["Branch Name"].unique().tolist() unique_depts = report_data[["Branch Name", "Department Name"]].drop_duplicates().to_dict(orient = "records") unique_reportees = report_data[["Branch Name", "Department Name", "Direct Reporting"]].drop_duplicates().to_dict(orient = "records") unique_combos = { "Branch Name": unique_branches, "Department Name": unique_depts, "Direct Reporting": unique_reportees } report_data = unique_combos success = True # ┳┓ # ┣┫┏┓┏┏┓┏┓┏┓┏┏┓ # ┛┗┗ ┛┣┛┗┛┛┗┛┗ # ┛ 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