(20260219) Updarted APIs to now show last-in time and loc also.

This commit is contained in:
2026-02-19 14:48:41 +05:30
parent 81021a4b38
commit d8aaf942b2
7 changed files with 360 additions and 218 deletions
@@ -307,10 +307,22 @@ async def in_out_report_generate(
"status": r.get("status"),
"firstIn": cosec_notes.get("firstIn"),
"firstInLoc": cosec_notes.get("firstInLoc"),
"lastIn": cosec_notes.get("lastIn"),
"lastInLoc": cosec_notes.get("lastInLoc"),
"lastOut": cosec_notes.get("lastOut"),
"lastOutLoc": cosec_notes.get("lastOutLoc"),
"workSeconds": cosec_notes.get("workSeconds"),
"workHours": cosec_notes.get("workHours"),
})
# Remove unwanted records:
from_ts = inbound_data.fromDate.timestamp()
to_ts = inbound_data.toDate.timestamp()
cumulative_register = [
cr for cr in cumulative_register
if from_ts <= cr["date"] <= to_ts
]
# 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:]))
File diff suppressed because one or more lines are too long
+1 -1
View File
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+18 -5
View File
@@ -569,6 +569,11 @@ def compute_work_done(
if work_reports[user_id][punch_date].get("first_in") is None:
work_reports[user_id][punch_date]["first_in"] = punch_ts
work_reports[user_id][punch_date]["first_in_loc"] = punch_loc
work_reports[user_id][punch_date]["last_in"] = punch_ts
work_reports[user_id][punch_date]["last_in_loc"] = punch_loc
else:
work_reports[user_id][punch_date]["last_in"] = punch_ts
work_reports[user_id][punch_date]["last_in_loc"] = punch_loc
# Handle the last out time:
if row["I/O Type"] == "Out":
@@ -589,19 +594,21 @@ def compute_work_done(
# Extract, clean and compute punch timing:
first_in = punch_info.get("first_in")
first_in_loc = punch_info.get("first_in_loc")
last_in = punch_info.get("last_in")
last_in_loc = punch_info.get("last_in_loc")
last_out = punch_info.get("last_out")
last_out_loc = punch_info.get("last_out_loc")
# When the user has a valid in-time, but no known out time,
# we assume that he worked a full day:
if first_in is not None and not last_out:
if first_in is not None and last_out is None:
last_out = first_in + min_work_seconds
work_seconds = last_out - first_in
work_seconds = min_work_seconds
work_ot_seconds = 0.0
# When the user has neither an in-time, nor an out-time,
# we assume that he was absent the whole day:
elif not first_in and not last_out:
elif first_in is None and last_out is None:
work_seconds = 0.0
work_ot_seconds = 0.0
@@ -628,6 +635,8 @@ def compute_work_done(
"work_date": punch_date,
"first_in": first_in,
"first_in_loc": first_in_loc,
"last_in": last_in,
"last_in_loc": last_in_loc,
"last_out": last_out,
"last_out_loc": last_out_loc,
"work_seconds": work_seconds,
@@ -647,6 +656,7 @@ def compute_work_done(
async def sync_attendance_to_tcaoff(
tcaoff_client: AsyncTheCAOffice,
cosec_in_out_summary: dict,
chunk_size: int = 10,
verbose: bool = False
) -> Dict[str, int]:
@@ -655,6 +665,7 @@ async def sync_attendance_to_tcaoff(
:param tcaoff_client: The asynchronous client object that interfaces with TCAOFF.
:param cosec_in_out_summary: he table that contains all the work done by all the employees on the date-range that
was selected.
:param chunk_size: The number of attendance marking requests to fire concurrently.
:param verbose: If True, internal debugging print will be more verbose.
:return: The dict that gives you the count of the successful and failed attendance marking API calls.
"""
@@ -707,9 +718,11 @@ async def sync_attendance_to_tcaoff(
json_notes = {
"cosec": {
"workSeconds": wr["work_seconds"],
"totHours": wr["work_hours"],
"workHours": wr["work_hours"],
"firstIn": wr["first_in"],
"firstInLoc": wr["first_in_loc"],
"lastIn": wr["last_in"],
"lastInLoc": wr["last_in_loc"],
"lastOut": wr["last_out"],
"lastOutLoc": wr["last_out_loc"],
}
@@ -724,7 +737,7 @@ async def sync_attendance_to_tcaoff(
yield lst[i:i + size]
count = 0
for chunk in chunks(tasks[:], size = 25):
for chunk in chunks(tasks[:], size = chunk_size):
count += 1
printer("Task Chunk:", count)
_res = await asyncio.gather(*chunk)
+8 -6
View File
@@ -149,12 +149,13 @@ async def yesterday_cron(
await common.sync_attendance_to_tcaoff(
tcaoff_client = tcaoff_client,
cosec_in_out_summary = json.from_file(common.PREV_DAY_IN_OUT_SUMMARY_CACHE_FILE),
chunk_size = 10
)
# If something goes wrong in the COSEC step:
except Exception as exception:
err_printer(exception)
if test_mode: raise
# if test_mode: raise
# Log out of TCAOFF:
success = await tcaoff_client.logout()
@@ -164,7 +165,7 @@ async def yesterday_cron(
except Exception as exception:
err_printer(exception)
await tcaoff_client.logout()
if test_mode: raise
# if test_mode: raise
# ---------------------------------------------------------------------------------------------------------------------
@@ -246,18 +247,19 @@ async def today_cron(
await common.sync_attendance_to_tcaoff(
tcaoff_client = tcaoff_client,
cosec_in_out_summary = json.from_file(common.IN_OUT_SUMMARY_CACHE_FILE),
chunk_size = 10
)
# If something goes wrong in the COSEC step:
except Exception as exception:
err_printer(exception)
if test_mode: raise
# if test_mode: raise
# If something goes wrong:
except Exception as exception:
err_printer(exception)
await tcaoff_client.logout()
if test_mode: raise
# if test_mode: raise
# ---------------------------------------------------------------------------------------------------------------------
@@ -284,12 +286,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
+156 -41
View File
@@ -136,7 +136,7 @@ class AsyncTheCAOffice:
pool = 60.0,
connect = 5.0,
write = 15.0,
read = 180.0
read = 60.0
),
headers = None
)
@@ -265,13 +265,22 @@ class AsyncTheCAOffice:
# ┣┫┏┓┏┓┏┓┏┣┓┏┓┏
# ┻┛┛ ┗┻┛┗┗┛┗┗ ┛
async def branch_list(self) -> List[Dict[str, Any]] | None:
async def branch_list(
self,
raise_exception: bool = False,
) -> List[Dict[str, Any]] | None:
"""
List the existing branches.
:param raise_exception: Whether to raise any exceptions, or to suppress them.
:return: A list of dictionaries where each dictionary describes on branch. None if the API call fails.
"""
# Start by assuming failure:
branches = None
try:
# Make the API call:
response = await self._http_client.post(
url = self.BRANCH_LIST_URL,
@@ -284,24 +293,39 @@ class AsyncTheCAOffice:
response_json = response.json()
branches = response_json["data"]["rs0"]
self._printer("Branches listed.")
return branches
# If the call failed:
else:
self._err_printer("Branch-list failed.")
return None
branches = None
# If something goes wrong:
except Exception as exception:
self._err_printer("Branch-list failed.", exception)
if raise_exception: raise
branches = None
# Done here:
return branches
async def branch_add(
self,
branch_name: str
branch_name: str,
raise_exception: bool = False,
) -> bool:
"""
Add a new branch.
:param branch_name: The name of the branch to add.
:param raise_exception: Whether to raise any exceptions, or to suppress them.
:return: True if logged out, else False.
"""
# Start by assuming failure:
success = False
try:
# Make the API call:
response = await self._http_client.post(
url = self.BRANCH_ADD_URL,
@@ -318,7 +342,7 @@ class AsyncTheCAOffice:
"Branch added.",
branch_name
)
return True
success = True
# If the call failed:
else:
@@ -328,20 +352,38 @@ class AsyncTheCAOffice:
branch_name,
response_json
)
return False
success = False
# If something goes wrong:
except Exception as exception:
self._err_printer("Branch NOT added.", exception, branch_name)
if raise_exception: raise
success = False
# Done here:
return success
# ┳┓
# ┃┃┏┓┏┓┏┓┏┓╋┏┳┓┏┓┏┓╋
# ┻┛┗ ┣┛┗┻┛ ┗┛┗┗┗ ┛┗┗
# ┛
async def department_list(self) -> List[Dict[str, Any]] | None:
async def department_list(
self,
raise_exception: bool = False,
) -> List[Dict[str, Any]] | None:
"""
List the existing departments.
:param raise_exception: Whether to raise any exceptions, or to suppress them.
:return: A list of dictionaries where each dictionary describes one department. None if the API call fails.
"""
# Start by assuming failure:
depts = None
try:
# Make the API call:
response = await self._http_client.post(
url = self.DEPT_LIST_URL,
@@ -354,24 +396,39 @@ class AsyncTheCAOffice:
response_json = response.json()
depts = response_json["data"]["rs0"]
self._printer("Depts. listed.")
return depts
# If the call failed:
else:
self._err_printer("Dept.-list failed.")
return None
depts = None
# If something goes wrong:
except Exception as exception:
self._err_printer("Dept.-list failed.", exception)
if raise_exception: raise
depts = None
# Done here:
return depts
async def department_add(
self,
department_name: str
department_name: str,
raise_exception: bool = False,
) -> bool:
"""
Add a new department.
:param department_name: The name of the department to add.
:param raise_exception: Whether to raise any exceptions, or to suppress them.
:return: True if logged out, else False.
"""
# Start by assuming failure:
success = False
try:
# Make the API call:
response = await self._http_client.post(
url = self.DEPT_ADD_URL,
@@ -388,7 +445,7 @@ class AsyncTheCAOffice:
"Dept. added.",
department_name
)
return True
success = True
# If the call failed:
else:
@@ -398,19 +455,37 @@ class AsyncTheCAOffice:
department_name,
response_json
)
return False
success = False
# If something goes wrong:
except Exception as exception:
self._err_printer("Dept. NOT added.", exception, department_name)
if raise_exception: raise
success = False
# Done here:
return success
# ┏┳┓
# ┃ ┏┓┏┓┏┳┓
# ┻ ┗ ┗┻┛┗┗
async def team_list(self) -> List[Dict[str, Any]] | None:
async def team_list(
self,
raise_exception: bool = False,
) -> List[Dict[str, Any]] | None:
"""
List the existing team members.
:param raise_exception: Whether to raise any exceptions, or to suppress them.
:return: A list of dictionaries where each dictionary describes one team-member. None if the API call fails.
"""
# Start by assuming failure:
teams = None
try:
# Make the API call:
response = await self._http_client.post(
url = self.TEAM_LIST_URL,
@@ -423,12 +498,20 @@ class AsyncTheCAOffice:
response_json = response.json()
teams = response_json["data"]["rs0"]
self._printer("Team listed.")
return teams
# If the call failed:
else:
self._err_printer("Team-list failed.")
return None
teams = None
# If something goes wrong:
except Exception as exception:
self._err_printer("Team-list filed.", exception)
if raise_exception: raise
teams = None
# Done here:
return teams
async def team_add(
self,
@@ -441,7 +524,8 @@ class AsyncTheCAOffice:
role: str,
username: str,
password: str,
applicant_notes: dict | list | str = None
applicant_notes: dict | list | str = None,
raise_exception: bool = False,
) -> bool:
"""
@@ -456,9 +540,15 @@ class AsyncTheCAOffice:
:param username: The unique username of the team member. Cannot be the same as anyone else.
:param password: The password for this team member's login.
:param applicant_notes: Optional notes about the team member.
:param raise_exception: Whether to raise any exceptions, or to suppress them.
:return: True if successful, else False.
"""
# Start by assuming failure:
success = False
try:
# Make the API call:
response = await self._http_client.post(
url = self.TEAM_ADD_URL,
@@ -485,7 +575,7 @@ class AsyncTheCAOffice:
team_name,
username,
)
return True
success = True
# If the call failed:
else:
@@ -496,7 +586,16 @@ class AsyncTheCAOffice:
username,
response_json
)
return False
success = False
# If something goes wrong:
except Exception as exception:
self._err_printer("Team NOT added.", exception)
if raise_exception: raise
success = False
# Done here:
return success
# ┏┓ ┓
# ┣┫╋╋┏┓┏┓┏┫┏┓┏┓┏┏┓
@@ -508,7 +607,8 @@ class AsyncTheCAOffice:
status: Literal["P", "H", "A", "OT"],
over_time: int | float,
attendance_date: datetime.datetime | None = None,
json_notes: dict = None
json_notes: dict = None,
raise_exception: bool = False
) -> bool:
"""
@@ -522,9 +622,15 @@ class AsyncTheCAOffice:
:param over_time: The amount of over-time work in hours.
:param attendance_date: The date of the attendance. If not given, today's date will be used.
:param json_notes: Optional notes about the attendance.
:param raise_exception: Whether to raise any exceptions, or to suppress them.
:return: True if the attendance was marked, else False.
"""
# Start by assuming failure:
success = False
try:
# Prepare the payload:
json_payload = {
"date": (
@@ -544,52 +650,55 @@ class AsyncTheCAOffice:
response = await self._http_client.post(
url = self.ATTENDANCE_MARK_URL,
headers = {"X-Session-Token": self.__session_token},
json = json_payload
json = json_payload,
timeout = httpx.Timeout(
pool = 60.0,
connect = 5.0,
write = 15.0,
read = 10.0
)
)
# Debugging:
if response.status_code not in [200]:
try:
response_json = response.json()
self._printer("TCAOFF Attendance-Mark", response_json)
except Exception as e:
self._printer("TCAOFF Attendance-Mark", e, response.content)
print("Payload:", json.to_string(json_payload))
# If the call succeeded:
if response.status_code in [200]:
self._printer(
"Attendance marked.",
user_id,
)
return True
self._printer("Attendance marked.", user_id, status, json_notes)
success = True
# If the call failed:
else:
try: response_json = response.json()
except Exception as e: response_json = response.content
self._err_printer(
"Attendance NOT added.",
user_id,
response_json
)
return False
self._err_printer("Attendance NOT added.", user_id, response_json)
success = False
# If something goes wrong:
except Exception as exception:
self._err_printer("Attendance NOT added.", exception, user_id)
if raise_exception: raise
success = False
# Done here:
return success
async def attendance_register(
self,
target_month: datetime.datetime,
raise_exception: bool = False,
) -> 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.
:param raise_exception: Whether to raise any exceptions, or to suppress them.
:return: The attendance register records if successful, else None.
"""
# Start by assuming failure:
attendance_register = None
try:
# Make the API call:
json_payload = {"month": target_month.strftime("%Y-%m-%d")}
response = await self._http_client.post(
@@ -617,6 +726,12 @@ class AsyncTheCAOffice:
)
attendance_register = None
# If something goes wrong:
except Exception as exception:
self._err_printer(exception)
if raise_exception: raise
attendance_register = None
# Done here:
return attendance_register