mirror of
https://github.com/UofCBaja/BajaCloud.git
synced 2025-06-15 13:14:17 -06:00
feat(WriteDB): Added file lock, prevented race condition
This commit is contained in:
parent
b150eee58b
commit
bcdaa37d97
60
WriteDB.py
60
WriteDB.py
@ -1,10 +1,12 @@
|
|||||||
import pandas as pd
|
import pandas as pd
|
||||||
import json
|
import json
|
||||||
from openpyxl import load_workbook
|
from openpyxl import load_workbook
|
||||||
from send_email import send_email
|
#from send_email import send_email
|
||||||
|
from filelock import FileLock
|
||||||
|
|
||||||
# Define the path to the Excel file
|
# Define the path to the Excel file and the lock file
|
||||||
file_path = "./interview_database.xlsx"
|
file_path = "./interview_database.xlsx"
|
||||||
|
lock_path = "./interview_database.xlsx.lock" # Lock file for synchronization
|
||||||
|
|
||||||
def ReadDatabase():
|
def ReadDatabase():
|
||||||
"""
|
"""
|
||||||
@ -18,8 +20,10 @@ def ReadDatabase():
|
|||||||
|
|
||||||
``Contact``: ahmad.ahmad1@ucalgary.ca
|
``Contact``: ahmad.ahmad1@ucalgary.ca
|
||||||
"""
|
"""
|
||||||
# Load the Excel file into a pandas DataFrame with specific columns
|
# Use a file-based lock for thread-safe and process-safe access
|
||||||
df = pd.read_excel(file_path, usecols=['Date', 'Start Time', 'Slot', 'Interviewee Name', 'Interviewee Email', 'Meeting Duration'])
|
with FileLock(lock_path):
|
||||||
|
# Load the Excel file into a pandas DataFrame with specific columns
|
||||||
|
df = pd.read_excel(file_path, usecols=['Date', 'Start Time', 'Slot', 'Interviewee Name', 'Interviewee Email', 'Meeting Duration'])
|
||||||
|
|
||||||
# Initialize the dictionary to store structured data for available slots
|
# Initialize the dictionary to store structured data for available slots
|
||||||
interview_data = {}
|
interview_data = {}
|
||||||
@ -63,37 +67,39 @@ def AppendAppointment(date, start_time, interviewee_name, interviewee_email):
|
|||||||
|
|
||||||
# Check if the requested slot is available in the `available_slots` structure
|
# Check if the requested slot is available in the `available_slots` structure
|
||||||
if date in available_slots and start_time in available_slots[date]:
|
if date in available_slots and start_time in available_slots[date]:
|
||||||
# Load workbook and select "Sheet1" for updating appointments
|
with FileLock(lock_path): # Ensure process-safe access to the file
|
||||||
workbook = load_workbook(file_path)
|
# Load workbook and select "Sheet1" for updating appointments
|
||||||
sheet = workbook["Sheet1"]
|
workbook = load_workbook(file_path)
|
||||||
df = pd.read_excel(file_path)
|
sheet = workbook["Sheet1"]
|
||||||
|
df = pd.read_excel(file_path)
|
||||||
|
|
||||||
# Find and update the row that matches the provided date and start time
|
# Find and update the row that matches the provided date and start time
|
||||||
for index, row in df.iterrows():
|
for index, row in df.iterrows():
|
||||||
row_date = str(row['Date']).split(" ")[0]
|
row_date = str(row['Date']).split(" ")[0]
|
||||||
row_start_time = str(row['Start Time'])
|
row_start_time = str(row['Start Time'])
|
||||||
|
|
||||||
if row_date == date and row_start_time == start_time:
|
if row_date == date and row_start_time == start_time:
|
||||||
# Current entries for names and emails, and append new data with comma and space
|
# Current entries for names and emails, and append new data with comma and space
|
||||||
current_names = str(row['Interviewee Name']).strip()
|
current_names = str(row['Interviewee Name']).strip()
|
||||||
current_emails = str(row['Interviewee Email']).strip()
|
current_emails = str(row['Interviewee Email']).strip()
|
||||||
|
|
||||||
updated_names = f"{current_names}, {interviewee_name}" if current_names != "nan" else interviewee_name
|
updated_names = f"{current_names}, {interviewee_name}" if current_names != "nan" else interviewee_name
|
||||||
updated_emails = f"{current_emails}, {interviewee_email}" if current_emails != "nan" else interviewee_email
|
updated_emails = f"{current_emails}, {interviewee_email}" if current_emails != "nan" else interviewee_email
|
||||||
|
|
||||||
# Update the cells with new names and emails
|
# Update the cells with new names and emails
|
||||||
name_cell = sheet.cell(row=index + 2, column=df.columns.get_loc('Interviewee Name') + 1)
|
name_cell = sheet.cell(row=index + 2, column=df.columns.get_loc('Interviewee Name') + 1)
|
||||||
email_cell = sheet.cell(row=index + 2, column=df.columns.get_loc('Interviewee Email') + 1)
|
email_cell = sheet.cell(row=index + 2, column=df.columns.get_loc('Interviewee Email') + 1)
|
||||||
name_cell.value = updated_names
|
name_cell.value = updated_names
|
||||||
email_cell.value = updated_emails
|
email_cell.value = updated_emails
|
||||||
|
|
||||||
workbook.save(file_path)
|
workbook.save(file_path)
|
||||||
send_email(interviewee_email, interviewee_name, date, start_time)
|
#send_email(interviewee_email, interviewee_name, date, start_time)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
# If no slots available, return that the slot is unavailable
|
# If no slots available, return that the slot is unavailable
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def run_tests():
|
def run_tests():
|
||||||
"""
|
"""
|
||||||
Executes test cases to verify appointment scheduling and slot availability.
|
Executes test cases to verify appointment scheduling and slot availability.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user