Files
cosec_automation/backend/api/blueprints/reports/muster_roll.py
T

232 lines
8.1 KiB
Python

"""
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()
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.
"""
# Check for authorization:
proj_dir = files.get_file_directory(include_filename = False)
proj_dir = files.get_parent_directory(proj_dir, depth = 4)
creds_file = os.path.join(proj_dir, "creds", "cosec.json")
creds = json.from_file(creds_file)["creds"]
if not (
inbound_headers.cosecUsername == creds["username"]
and inbound_headers.cosecPassword == creds["password"]
): return ResponseModel(
status_code = StatusCodes.FAILED,
http_code = HttpCodes.UNAUTHORIZED,
message = f"Unauthorized.",
)
# Start by assuming failure:
success = False
report_data = None
# Figure out the paths:
proj_dir = files.get_parent_directory(
files.get_file_directory(include_filename = False),
depth = 4
)
cache_file = os.path.join(proj_dir, "local", "cache", "muster_roll_cache.json")
# Read the report:
try: report_data = json.from_file(cache_file)
except Exception as e: pass
# Note down the status of success or failure:
success = True if report_data else False
# ┳┓
# ┣┫┏┓┏┏┓┏┓┏┓┏┏┓
# ┛┗┗ ┛┣┛┗┛┛┗┛┗
# ┛
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