(20260218) Updating APIs

This commit is contained in:
2026-02-18 18:16:29 +05:30
parent 0b0c75d4fa
commit 81021a4b38
14 changed files with 231 additions and 64 deletions
+160 -27
View File
@@ -68,11 +68,13 @@ from utils_v2.api.async_quart import (
from backend.models.api.common import SimpleCosecCredentialsHeaders
from backend.models.api.reports.in_out_report import CosecInOutReportRequestData
# Cosec-related:
from cosec_web.cosec_web import CosecWeb
# # Cosec-related:
# from cosec_web.cosec_web import CosecWeb
# Common:
from backend.shared import constants
# from backend.shared import constants
from tcaoff.async_tcaoff import AsyncTheCAOffice
from scripts import common
# To work with dat and time:
import time
@@ -121,6 +123,95 @@ def init(blueprint_setup_state):
# ---------------------------------------------------------------------------------------------------------------------
# @in_out_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: CosecInOutReportRequestData(**x)
# )
# @handle_cancelled_request()
# async def in_out_report_generate(
# inbound_headers: SimpleCosecCredentialsHeaders | dict = None,
# inbound_data: CosecInOutReportRequestData | 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", "in_out_summary_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."
# )
# ---------------------------------------------------------------------------------------------------------------------
@in_out_report_bp.route("/generate", methods = ["GET"])
@set_api_version(api_version = "1.0.0")
@read_input(sanitize_headers = False, sanitize_data = False)
@@ -137,7 +228,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),
# header_validator = lambda x: SimpleCosecCredentialsHeaders(**x),
data_validator = lambda x: CosecInOutReportRequestData(**x)
)
@handle_cancelled_request()
@@ -157,37 +248,79 @@ async def in_out_report_generate(
: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.",
)
# # Check for authorization:
# creds = json.from_file(common.COSEC_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
# Figure out a list of dates for the attendance-register API:
# LOCAL TEST = http://127.0.0.1:5000//cosec/reports/in-out/generate?fromDate=20250218&toDate=20260218&groupIds=2,3,4
from_date = inbound_data.fromDate
to_date = inbound_data.toDate
dates_list = []
while True:
from_date = from_date.replace(day = 1)
dates_list.append(from_date)
if (
from_date.year == to_date.year and
from_date.month == to_date.month
): break
if from_date.month < 12: from_date = from_date.replace(month = from_date.month + 1)
else: from_date = from_date.replace(year = from_date.year + 1, month = 1)
print("MODEL:", inbound_data)
print(f"DATES ({len(dates_list)})")
for d in dates_list: print(d)
# Login to TCAOFF:
creds = json.from_file(common.TCAOFF_CREDS_FILE)["creds"]
tcaoff_client = AsyncTheCAOffice(
username = creds["username"],
password = creds["password"],
)
cache_file = os.path.join(proj_dir, "local", "cache", "in_out_summary_cache.json")
if await tcaoff_client.login():
# Read the report:
try: report_data = json.from_file(cache_file)
except Exception as e: pass
# Get the attendance register for each of these target dates.
# Remember that TCAOFF gives you the whole month instead of just one date:
cumulative_register = []
tasks = [tcaoff_client.attendance_register(d) for d in dates_list]
result_sets = await asyncio.gather(*tasks)
for rs in result_sets:
for r in rs:
# cumulative_register.append(r)
cosec_notes = json.from_string(r.get("json_notes", "{}")).get("cosec", {})
ar_dt = r.get("ar_date")
if ar_dt: ar_dt = datetime.datetime.strptime(ar_dt, "%Y-%m-%d").timestamp()
cumulative_register.append({
"date": ar_dt,
"empId": r.get("pseudonym"),
"empName": r.get("full_name"),
"status": r.get("status"),
"firstIn": cosec_notes.get("firstIn"),
"firstInLoc": cosec_notes.get("firstInLoc"),
"lastOut": cosec_notes.get("lastOut"),
"lastOutLoc": cosec_notes.get("lastOutLoc"),
})
# Note down the status of success or failure:
success = True if report_data else False
# Sort the records:
cumulative_register = sorted(cumulative_register, key = lambda x: x["date"])
print(f"REGISTER ({len(cumulative_register)}):", json.to_string(cumulative_register[-10:]))
# Note down successful actions:
success = True
report_data = cumulative_register
# Don't forget to log-out at the end:
await tcaoff_client.logout()
# ┳┓
# ┣┫┏┓┏┏┓┏┓┏┓┏┏┓