Skip to content

Commit

Permalink
Merge pull request #3 from navariltd/develop4
Browse files Browse the repository at this point in the history
feat - handle punch logs seperate, handle response using flask rather frappe response handler
  • Loading branch information
maniamartial authored Jul 26, 2024
2 parents 759622f + 8df1580 commit 981e7f1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 15 deletions.
66 changes: 51 additions & 15 deletions navari_cams_biometric/cams_biometric/controllers/cams_call.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,40 @@
import frappe
from frappe import _
from dateutil import parser
from flask import Response

@frappe.whitelist(allow_guest=True)
def attendance():
rawdata = frappe.local.request.get_data(as_text=True)
stgid = frappe.local.form_dict.get('stgid')
print(f"Received data: {rawdata}") # Print raw data for debugging

if not rawdata:
frappe.local.response["http_status_code"] = 400
frappe.local.response["message"] = {"status": "error", "message": "No data provided"}
return
return Response(
json.dumps({"status": "done"}),
status=200,
mimetype='application/json'
)

try:
data = json.loads(rawdata)
except json.JSONDecodeError as e:
frappe.local.response["http_status_code"] = 400
frappe.local.response["message"] = {"status": "error", "message": "Invalid JSON data"}
return
return Response(
json.dumps({"status": "done"}),
status=200,
mimetype='application/json'
)

if "RealTime" in data:
ret = handle_attendance_log(stgid, rawdata)
else:
ret = "Else"
handle_attendance_log(stgid, rawdata)
elif "PunchLog" in data:
handle_punch_logs(stgid, data["PunchLog"]["Log"])

response = {
"status": ret
}

frappe.local.response["http_status_code"] = 200
frappe.local.response["message"] = response
return Response(
json.dumps({"status": "done"}),
status=200,
mimetype='application/json'
)

def handle_attendance_log(stgid, rawdata):
request_data = json.loads(rawdata)
Expand Down Expand Up @@ -65,6 +70,37 @@ def handle_attendance_log(stgid, rawdata):

return "done"

def handle_punch_logs(stgid, punch_logs):
for punch_log in punch_logs:
log_type_punch = punch_log["Type"]
log_type = 'OUT' if log_type_punch == 'CheckOut' else 'IN'

# Convert the datetime format using dateutil.parser
log_time = punch_log["LogTime"]
log_time_dt = parser.parse(log_time)
formatted_log_time = log_time_dt.strftime("%Y-%m-%d %H:%M:%S")

# Check if the employee check-in already exists
existing_checkin = frappe.db.exists("Employee Checkin", {
"employee": punch_log["UserID"],
"time": formatted_log_time,
"log_type": log_type
})

if not existing_checkin:
# Storing the values in Employee Checkin doctype
employee_checking = frappe.get_doc({
"doctype": "Employee Checkin",
"employee": punch_log["UserID"],
"time": formatted_log_time,
"log_type": log_type,
"custom_input_type": punch_log["InputType"]
})

employee_checking.insert(ignore_permissions=True)
frappe.db.commit()

return "done"

def add_user():
first_name=frappe.form_dict.get('first_name')
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ readme = "README.md"
dynamic = ["version"]
dependencies = [
# "frappe~=15.0.0" # Installed and managed by bench.
"flask==3.0.3"
]

[build-system]
Expand Down

0 comments on commit 981e7f1

Please sign in to comment.