I am trying to
Create a custom workflow:
Open (Draft)
Approved
Rejected
Cancelled
Auto-Approval
and Create a python script that automatically approves requests that are for 30 minutes or less. for the doctype out_of_office with these fields.
Employee ID (Link to Employee)
Departure Time (Time)
Arrival Time (Time)
Request Date (Date)
Reason for departure (Text)
I am having problem in implementing auto approval.
This is my hook.py
doc_events = {
“Out of Office”: {
“validate”: “out_of_office.out_of_office.out_of_office.doctype.out_of_office.out_of_office.validate”
}
}
This is my doctype → out_of_office.py
from datetime import datetime, timedelta
import frappe
from frappe.model.document import Document
class OutOfOffice(Document):
def validate(self):
frappe.logger().info(f"Starting validation for Out of Office request: {self.name}")
# Ensure departure and arrival times are set
if not self.departure_time or not self.arrival_time:
frappe.throw("Both Departure Time and Arrival Time must be specified.")
# Combine the request date with the departure and arrival times
departure = datetime.combine(self.request_date, self.departure_time)
arrival = datetime.combine(self.request_date, self.arrival_time)
# Log departure and arrival times
frappe.logger().info(f"Departure Time: {self.departure_time}, Arrival Time: {self.arrival_time}")
# Validate that arrival time is after departure time
if arrival <= departure:
frappe.throw("Arrival Time must be after Departure Time.")
# Calculate the duration
duration = arrival - departure
frappe.logger().info(f"Duration: {duration}")
# Set is_short_duration if the duration is less than 30 minutes
if duration < timedelta(minutes=30):
frappe.logger().info(f"Duration is less than 30 minutes, setting workflow_state to 'Auto Approve'")
self.is_short_duration = 1
self.workflow_state = 'Auto Approve'
self.docstatus = 1 # Set document status to Submitted
# Apply workflow transition to "Approved"
self.workflow_state = 'Approved'
frappe.logger().info(f"Setting workflow_state to 'Approved' and saving the document.")
self.save(ignore_permissions=True)
else:
frappe.logger().info(f"Duration is greater than or equal to 30 minutes, setting is_short_duration to 0.")
self.is_short_duration = 0
And the error I am facing is import error:out_of_office and many different errors. Could you please help me with what is the right way for auto approving the out of office leave using conditioned workflow.