(20260218) Updating APIs
This commit is contained in:
@@ -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()
|
||||
|
||||
# ┳┓
|
||||
# ┣┫┏┓┏┏┓┏┓┏┓┏┏┓
|
||||
|
||||
@@ -32,8 +32,8 @@
|
||||
|
||||
# To make sibling directories accessible for imports:
|
||||
import sys
|
||||
sys.path.append(".")
|
||||
sys.path.append("..")
|
||||
sys.path.append("../..")
|
||||
|
||||
# For making data behaviour_models:
|
||||
from pydantic import BaseModel, Field, field_validator, AwareDatetime
|
||||
|
||||
+1
-1
@@ -51,7 +51,7 @@ import pandas as pd
|
||||
from cosec_web.cosec_web import CosecWeb
|
||||
|
||||
# TCAOFF-related:
|
||||
from helpers import tcaoff
|
||||
from tcaoff import tcaoff
|
||||
|
||||
# My utils:
|
||||
from utils_v2.system import files
|
||||
|
||||
+1
-1
@@ -51,7 +51,7 @@ import pandas as pd
|
||||
from cosec_web.cosec_web import CosecWeb
|
||||
|
||||
# TCAOFF-related:
|
||||
from helpers import tcaoff
|
||||
from tcaoff import tcaoff
|
||||
|
||||
# My utils:
|
||||
from utils_v2.system import files
|
||||
|
||||
+1
-1
File diff suppressed because one or more lines are too long
Vendored
+1
-1
File diff suppressed because one or more lines are too long
+1
-1
File diff suppressed because one or more lines are too long
@@ -5,7 +5,7 @@ This is a one-time fix script where we need to fix some content of the accounts
|
||||
# Imports:
|
||||
import os
|
||||
import pandas as pd
|
||||
from helpers import tcaoff
|
||||
from tcaoff import tcaoff
|
||||
from utils_v2.string import json
|
||||
from utils_v2.system import files
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ This is a one-time fix script where we need to fix some content of the accounts
|
||||
# Imports:
|
||||
import os
|
||||
import pandas as pd
|
||||
from helpers import tcaoff
|
||||
from tcaoff import tcaoff
|
||||
from utils_v2.string import json
|
||||
from utils_v2.system import files
|
||||
|
||||
|
||||
+10
-5
@@ -51,7 +51,7 @@ import pandas as pd
|
||||
from cosec_web.cosec_web import CosecWeb
|
||||
|
||||
# TCAOFF-related:
|
||||
from helpers.async_tcaoff import AsyncTheCAOffice
|
||||
from tcaoff.async_tcaoff import AsyncTheCAOffice
|
||||
|
||||
# My utils:
|
||||
from utils_v2.system import files
|
||||
@@ -705,9 +705,14 @@ async def sync_attendance_to_tcaoff(
|
||||
over_time = wr["work_ot_hours"],
|
||||
attendance_date = wr["work_date"],
|
||||
json_notes = {
|
||||
"totHours": wr["work_hours"],
|
||||
"firstIn": wr["first_in"],
|
||||
"lastOut": wr["last_out"],
|
||||
"cosec": {
|
||||
"workSeconds": wr["work_seconds"],
|
||||
"totHours": wr["work_hours"],
|
||||
"firstIn": wr["first_in"],
|
||||
"firstInLoc": wr["first_in_loc"],
|
||||
"lastOut": wr["last_out"],
|
||||
"lastOutLoc": wr["last_out_loc"],
|
||||
}
|
||||
}
|
||||
)
|
||||
)
|
||||
@@ -743,4 +748,4 @@ async def sync_attendance_to_tcaoff(
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
pass
|
||||
print("SYS-PATH:", sys.path)
|
||||
|
||||
+3
-3
@@ -51,7 +51,7 @@ import pandas as pd
|
||||
from cosec_web.cosec_web import CosecWeb
|
||||
|
||||
# TCAOFF-related:
|
||||
from helpers.async_tcaoff import AsyncTheCAOffice
|
||||
from tcaoff.async_tcaoff import AsyncTheCAOffice
|
||||
|
||||
# My utils:
|
||||
from utils_v2.system import files
|
||||
@@ -284,12 +284,12 @@ async def set_scheduler(
|
||||
await yesterday_cron(
|
||||
cosec_creds = copy.deepcopy(cosec_creds),
|
||||
tcaoff_client = tcaoff_client,
|
||||
test_mode = test_mode
|
||||
# test_mode = test_mode
|
||||
)
|
||||
await today_cron(
|
||||
cosec_creds = copy.deepcopy(cosec_creds),
|
||||
tcaoff_client = tcaoff_client,
|
||||
test_mode = test_mode
|
||||
# test_mode = test_mode
|
||||
)
|
||||
printer("Test Done")
|
||||
return
|
||||
|
||||
@@ -108,8 +108,8 @@ class AsyncTheCAOffice:
|
||||
TEAM_LIST_URL = f"{BASE_URL}/team/list"
|
||||
TEAM_ADD_URL = f"{BASE_URL}/team/add"
|
||||
TEAM_UPDATE_URL = f"{BASE_URL}/team/update"
|
||||
ATTENDANCE_LIST_URL = f"{BASE_URL}/user/attendance/register"
|
||||
ATTENDANCE_MARK_URL = f"{BASE_URL}/user/attendance/mark"
|
||||
ATTENDANCE_REGISTER_URL = f"{BASE_URL}/user/attendance/register"
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
@@ -459,25 +459,6 @@ class AsyncTheCAOffice:
|
||||
:return: True if successful, else False.
|
||||
"""
|
||||
|
||||
# print("TEAM ADD:", json.to_string({
|
||||
# "branchId": branch_id,
|
||||
# "idDepartment": dept_id,
|
||||
# "reportingTo": reporting_to,
|
||||
# "name": team_name,
|
||||
# "email": email,
|
||||
# "phoneNo": phone_no,
|
||||
# "role": role,
|
||||
# "username": username,
|
||||
# "password": password,
|
||||
# "hierarchy": 1,
|
||||
# "applicantNotes": applicant_notes
|
||||
# }))
|
||||
# return False
|
||||
|
||||
# # Ensure that JSON notes are converted to string:
|
||||
# if isinstance(applicant_notes, (list, dict)):
|
||||
# applicant_notes = json.to_string(applicant_notes, no_space = True)
|
||||
|
||||
# Make the API call:
|
||||
response = await self._http_client.post(
|
||||
url = self.TEAM_ADD_URL,
|
||||
@@ -594,6 +575,51 @@ class AsyncTheCAOffice:
|
||||
)
|
||||
return False
|
||||
|
||||
async def attendance_register(
|
||||
self,
|
||||
target_month: datetime.datetime,
|
||||
) -> List[dict] | None:
|
||||
|
||||
"""
|
||||
To get the whole attendance register for a month for all the employees of an entity.
|
||||
:param target_month: The datetime which indicates the year and month in which the attendance needs to be
|
||||
checked.
|
||||
:return: The attendance register records if successful, else None.
|
||||
"""
|
||||
|
||||
# Start by assuming failure:
|
||||
attendance_register = None
|
||||
|
||||
# Make the API call:
|
||||
json_payload = {"month": target_month.strftime("%Y-%m-%d")}
|
||||
response = await self._http_client.post(
|
||||
url = self.ATTENDANCE_REGISTER_URL,
|
||||
headers = {"X-Session-Token": self.__session_token},
|
||||
json = json_payload,
|
||||
)
|
||||
|
||||
# If the call succeeded:
|
||||
if response.status_code in [200]:
|
||||
self._printer(
|
||||
"Attendance register fetched.",
|
||||
target_month,
|
||||
)
|
||||
attendance_register = response.json()["data"]["rs0"]
|
||||
|
||||
# If the call failed:
|
||||
else:
|
||||
try: response_data = response.json()
|
||||
except Exception as e: response_data = response.content
|
||||
self._err_printer(
|
||||
"Attendance register NOT fetched.",
|
||||
target_month,
|
||||
response_data
|
||||
)
|
||||
attendance_register = None
|
||||
|
||||
# Done here:
|
||||
return attendance_register
|
||||
|
||||
|
||||
# *****************************************************************************************************************
|
||||
# ***** ****
|
||||
@@ -625,7 +651,10 @@ if __name__ == "__main__":
|
||||
)
|
||||
|
||||
await client.login()
|
||||
branches = await client.branch_list()
|
||||
attendance = await client.attendance_register(
|
||||
target_month = datetime.datetime.now() - datetime.timedelta(months = 1),
|
||||
)
|
||||
print("ATTENDANCE:", json.to_string(attendance))
|
||||
await client.logout()
|
||||
|
||||
asyncio.run(main())
|
||||
Reference in New Issue
Block a user