Hi,
I am more of a power user trying to develop, so your help is MUCH appreciated. Trying to learn as I go, hands on.
I create a JS custom button that triggers a python method that I have been tweaking in several ways (both using new_doc and db.sql), breaking it into two methods (one to get dates and the other to save as below), but once I get the code working, it runs great for a few records (adding 4-ish meeting dates). But when I need to create a longer list, I face a timeout issue.
Essentially, I want to schedule regular meetings between two user defined dates in the specified days of the week between these dates, always at the same (to and from) times.
Here is the code:
@frappe.whitelist()
def get_meeting_dates(self):
meeting_dates = []
"""Returns a list of meeting dates and also creates a child document for each meeting date with meeting time"""
days_of_week = [self.monday, self.tuesday, self.wednesday, self.thursday, self.friday, self.saturday, self.sunday]
current_date = datetime.strptime(self.c_datestart, "%Y-%m-%d")
final_date = datetime.strptime(self.c_dateend, "%Y-%m-%d")
while current_date <= final_date:
if days_of_week[current_date.weekday()]:
meeting_dates.append(current_date)
current_date += timedelta(days=1)
return meeting_dates
@frappe.whitelist()
def save_dates(self):
"""Define variables"""
meeting_dates = self.get_meeting_dates()
from_time = self.from_time
to_time = self.to_time
cs = self.name
pf = "cs_meetinfo"
pt = "Course Schedule"
# Clear existing meeting dates
frappe.db.sql(
"DELETE FROM `tabCourse Schedule Meeting Dates` WHERE parent=%s", self.name
)
print("Deleted existing meeting dates")
# Create new meeting dates
for date in meeting_dates:
name= cs+"-"+str(date)
frappe.db.sql(
"INSERT INTO `tabCourse Schedule Meeting Dates` (name, parent, parentfield, parenttype, cs_meetdate, cs_fromtime, cs_totime) VALUES (%s, %s, %s, %s, %s, %s, %s)",
(name, cs, pf, pt, date, from_time, to_time)
)
print(date)
Thanks so much for ANY help!