Files
proc-rota/leave.py
T

355 lines
14 KiB
Python

import csv
from datetime import datetime, timedelta
import re
import sys
from requests import Session
from loguru import logger
from rich.pretty import pprint
from rota_generator.workers import (
Worker,
NotAvailableToWork,
NonWorkingDays,
WorkRequests,
PreferenceNotToWork,
OutOfProgramme,
)
date_re = r"[\d]{1,2}\/[\d]{1,2}\/[\d]{2}"
live_rota = True
def load_leave(Rota):
with Session() as s:
shifts = Rota.get_shift_names()
workers = {}
headers = {"Cache-Control": "no-cache", "Pragma": "no-cache"}
if live_rota:
download = s.get(
"https://docs.google.com/spreadsheets/d/e/2PACX-1vQtNrp9F1fjtAh8vFX-W_R3RlD9H-2P_vCLMoHrVKR0e24yabbsdrD65VjwxoHlV1Qnatmw1NeghQHG/pub?gid=1199196967&single=true&output=csv",
headers=headers,
)
# download = s.get("https://docs.google.com/spreadsheets/d/e/2PACX-1vSRx9VWXSlRubPyA0RhiI-Oqf5eHNYYEc6rFzlraDbR5_8qqr5g13-4uV-gn4u-TjZxiSMv1fBUaESq/pub?gid=814517272&single=true&output=csv", headers=headers)
decoded_content = download.content.decode("utf-8")
reader = csv.reader(decoded_content.splitlines(), delimiter=",")
else:
with open("/home/ross/Downloads/PROC Draft RK version.csv") as f:
reader = csv.reader(f.read().splitlines(), delimiter=",")
n = 0
leave = {}
requests = {}
site_prefs = {}
order = []
n = 1
for row in reader:
if n < 10:
print(row)
row_title = row[0]
print(row_title)
row_date = row[0]
r = row[2:]
# header, extract names
if n == 1:
names = r
order = names
for name in names:
name = name.strip()
workers[name] = {}
workers[name]["leave"] = []
workers[name]["requests"] = []
workers[name]["site_pref"] = []
workers[name]["shift_balance_extra"] = {}
workers[name]["bank_holiday_extra"] = 0
workers[name]["end_date"] = None
workers[name]["start_date"] = None
workers[name]["pair"] = None
n = n + 1
continue
for i in range(len(r)):
try:
a = order[i].strip()
except IndexError:
continue
worker = workers[a]
lower_item = r[i].lower().strip()
if lower_item == "derriford":
lower_item = "plymouth"
if lower_item == "derriford twilights":
lower_item = "plymouth_twilights"
if (
"PROC site" in row_title
or row_title == "PROC nights from"
or "proc nights" in row_title.lower()
):
worker["site_pref"] = lower_item
elif row_title in (
"Rotation",
"Site",
"Placement",
"Placement location",
"Placement location",
):
worker["site"] = lower_item
elif (
row_title in ("Grade", "Grade (ST)")
or "Year of training" in row_title
):
worker["grade"] = lower_item.lstrip("ST")
elif (
row_title
in (
"%FTE",
"%FTE on-call",
"FTE",
"FTE on-call",
"Full-time equivalent (FTE)",
)
or "on-call commitment" in row_title
):
if lower_item:
if lower_item.endswith("%"):
lower_item = float(lower_item.rstrip("%")) / 100
worker["fte"] = lower_item
elif (
"NWD" in row_title or row_title == "If <100% FTE, specify days off:"
):
worker["nwd"] = lower_item
elif row_title == "Flexible NWD":
worker["flexible_nwd"] = lower_item
elif row_title in ("End Date", "CCT date", "CCT Date"):
if lower_item:
try:
date = datetime.strptime(lower_item, "%d/%m/%y").date()
except ValueError:
try:
date = datetime.strptime(lower_item, "%d-%m-%y").date()
except ValueError:
try:
date = datetime.strptime(
lower_item, "%d/%m/%Y"
).date()
except ValueError:
print(
f"Cannot parse date: '{lower_item}' (n: {n}, row: {row})"
)
sys.exit(1)
worker["end_date"] = date
else:
worker["end_date"] = None
elif row_title in ("Start date",):
if lower_item:
try:
date = datetime.strptime(lower_item, "%d/%m/%y").date()
except ValueError:
date = datetime.strptime(lower_item, "%d/%m/%Y").date()
except ValueError:
logger.warning(
f"Cannot parse date: {lower_item} (n: {n}, row: {row})"
)
raise ValueError(f"Cannot parse date: {lower_item}")
worker["start_date"] = date
else:
worker["start_date"] = None
elif "OOP" in row_title or "out of programme" in row_title.lower():
if lower_item in (
"OOPT 1 START",
"OOPT 1 END",
"OOPT 2 START",
"OOPT 2 END",
):
continue
worker["oop"] = lower_item
elif row_title == "Group":
worker["group"] = lower_item
elif row_title == "Pair":
if lower_item == "":
worker["pair"] = None
else:
worker["pair"] = int(lower_item)
elif row_title.startswith("Extra_"):
if lower_item:
shift = row_title[len("Extra_") :]
if shift == "twilight":
shift = f"{worker['site']}_{shift}"
elif shift == "weekend":
shift = f"weekend_{worker['site']}"
worker["shift_balance_extra"][shift] = float(lower_item)
elif row_title == "bank_holidays_worked":
if lower_item:
worker["bank_holiday_extra"] = int(lower_item)
elif re.match(date_re, row_date) is not None:
if lower_item != "":
try:
try:
date = datetime.strptime(row_date, "%d/%m/%y").date()
except ValueError:
date = datetime.strptime(row_date, "%d/%m/%Y").date()
# This may be easier to do as a dict
if lower_item in shifts:
worker["requests"].append(
WorkRequests(date=date, shift=lower_item)
)
elif lower_item in ("nights only"):
if date.weekday() < 5:
worker["requests"].append(
WorkRequests(date=date, shift="night_weekday")
)
else:
worker["requests"].append(
WorkRequests(date=date, shift="night_weekend")
)
elif lower_item in (
"work_request",
"happy to work",
"offered",
"may be",
"volunteered",
):
worker["requests"].append(
WorkRequests(date=date, shift="*")
)
else:
worker["leave"].append(
NotAvailableToWork(date=date, reason=lower_item)
)
except ValueError as e:
print(f"Error with: {lower_item}")
print(f"{row=}")
raise e
n = n + 1
return workers
# load_leave()
def load_academy(Rota):
"""
Some trainees spend month blocks in the academy. This function loads that information from a google sheet that published it as a csv file.
"""
url = "https://docs.google.com/spreadsheets/d/e/2PACX-1vQtNrp9F1fjtAh8vFX-W_R3RlD9H-2P_vCLMoHrVKR0e24yabbsdrD65VjwxoHlV1Qnatmw1NeghQHG/pub?gid=1113837782&single=true&output=csv"
with Session() as s:
headers = {"Cache-Control": "no-cache", "Pragma": "no-cache"}
download = s.get(url, headers=headers)
decoded = download.content.decode("utf-8")
reader = csv.reader(decoded.splitlines(), delimiter=',')
n = 0
names = []
# collect date rows (these are the month start boundaries) and their cells
date_rows: list[datetime.date] = []
date_row_cells: list[list[str]] = []
for row in reader:
n += 1
if n == 1:
# header row: first cell is column title, remaining are worker names
names = [c.strip() for c in row[1:]]
continue
if not row:
continue
row_title = row[0].strip()
cells = row[1:]
# If this row is a date row like '02/03/2026' then treat it as a month-start boundary
if re.match(date_re, row_title):
try:
try:
d = datetime.strptime(row_title, "%d/%m/%Y").date()
except ValueError:
d = datetime.strptime(row_title, "%d/%m/%y").date()
except Exception:
continue
date_rows.append(d)
# normalize cells length to match names
row_cells = [ (cells[i] if i < len(cells) else "").strip() for i in range(len(names)) ]
date_row_cells.append(row_cells)
# If no date rows found, nothing to do
if not date_rows:
return
# Identify twilight shift names available in the rota
twilight_shift_names = [s.name for s in Rota.get_shifts() if "twilight" in s.name]
# For each date boundary, compute its end (next boundary - 1 day) and register avoids where cell=='academy'
for idx, start_date in enumerate(date_rows):
if idx + 1 < len(date_rows):
end_date = date_rows[idx + 1] - timedelta(days=1)
else:
end_date = Rota.rota_end_date
cells = date_row_cells[idx]
for i, cell in enumerate(cells):
if i >= len(names):
break
v = (cell or "").lower()
if "academy" in v:
worker_name = names[i]
matching_workers = [w for w in Rota.workers if w.name.strip() == worker_name.strip()]
if not matching_workers:
logger.warning(f"Academy data: worker '{worker_name}' not found in rota, skipping")
continue
worker = matching_workers[0]
# choose twilight shifts relevant to the worker's site
candidate_shifts = []
for sh_name in twilight_shift_names:
try:
sh = Rota.get_shift_by_name(sh_name)
except Exception:
continue
if hasattr(worker, "site") and worker.site in sh.sites:
candidate_shifts.append(sh_name)
elif sh_name.startswith(f"{worker.site}_"):
candidate_shifts.append(sh_name)
if not candidate_shifts:
candidate_shifts = twilight_shift_names.copy()
# register as a date range with a reason 'academy'
Rota.add_avoid_shifts_for_workers_on_dates(
names=[worker.name], shifts=candidate_shifts, start_date=start_date, end_date=end_date, reason="academy"
)