many improvements

This commit is contained in:
Ross
2022-07-11 19:46:40 +01:00
parent 0cebbfddf6
commit dc975b991c
25 changed files with 1949 additions and 863 deletions
+67 -42
View File
@@ -1,7 +1,7 @@
import datetime
from collections import defaultdict
from typing import Iterable, List, Literal, Optional
from pydantic import BaseModel, Extra
from pydantic import BaseModel, Extra, validator
from rich.pretty import pprint
@@ -15,29 +15,51 @@ if TYPE_CHECKING:
from rota.shifts import RotaBuilder
days = Literal["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
whole_days = [
"sunday",
"monday",
"tuesday",
"wednesday",
"thursday",
"friday",
"saturday",
]
class NotAvailableToWork(BaseModel):
date: datetime.date
reason: str = "unknown"
class NonWorkingDays(BaseModel):
day: days
start_date: datetime.date | None = None
end_date: datetime.date | None = None
@validator("day")
def day_in(cls, day):
for whole_day in whole_days:
if day.lower() in whole_day:
return whole_day[:3].capitalize()
raise ValueError("Invalid day")
class WorkRequests(BaseModel):
date: datetime.date
shift: str = ""
class PreferenceNotToWork(BaseModel):
date: datetime.date
reason: str = ""
class OutOfProgramme(BaseModel):
start_date: datetime.date
end_date: datetime.date
reason: str = ""
class Worker(BaseModel):
name: str
site: str
@@ -45,7 +67,7 @@ class Worker(BaseModel):
# rules is easier.
grade: int
# We can either have a user generated ID
id: Optional[int]=None
id: Optional[int] = None
fte: int = 100
nwds: list[NonWorkingDays] = []
start_date: datetime.date | None = None
@@ -54,7 +76,7 @@ class Worker(BaseModel):
not_available_to_work: list[NotAvailableToWork] = []
pref_not_to_work: list[PreferenceNotToWork] = []
work_requests: list[WorkRequests] = []
remote_site: str = "plymouth", # We set a default proc_site
remote_site: str = ("plymouth",) # We set a default proc_site
previous_shifts: dict = {}
shift_balance_extra: dict = {}
bank_holiday_extra: int = 0
@@ -63,27 +85,27 @@ class Worker(BaseModel):
class Config:
extra = Extra.allow
# def __init__(
# self,
# #Rota: "RotaBuilder", # Aim to remove (workers should be independent of rotas)
# ):
#
# self.name = name
# self.site = site
# self.grade = grade
# self.fte = fte
# self.nwd = nwd
# self.pair = pair
# self.shift_balance_extra = shift_balance_extra
# self.bank_holiday_extra = bank_holiday_extra
# self.start_date
#
# self.remote_site = remote_site
#
# self.previous_shifts = previous_shifts
#
#
# # days_to_work = Rota.rota_days_length
# def __init__(
# self,
# #Rota: "RotaBuilder", # Aim to remove (workers should be independent of rotas)
# ):
#
# self.name = name
# self.site = site
# self.grade = grade
# self.fte = fte
# self.nwd = nwd
# self.pair = pair
# self.shift_balance_extra = shift_balance_extra
# self.bank_holiday_extra = bank_holiday_extra
# self.start_date
#
# self.remote_site = remote_site
#
# self.previous_shifts = previous_shifts
#
#
# # days_to_work = Rota.rota_days_length
def load_rota(self, Rota):
self.proportion_rota_to_work = 1
@@ -96,6 +118,9 @@ class Worker(BaseModel):
if self.start_date >= self.end_date:
raise ValueError("End date must be after start date")
if self.name == "Steph bailey":
print(self.start_date, self.end_date)
# if no start date default to the start of the rota
if self.start_date is None:
self.calculated_start_date = Rota.start_date
@@ -144,9 +169,7 @@ class Worker(BaseModel):
if isinstance(end_oop, datetime.date):
end_oop_date = end_oop
else:
end_oop_date = datetime.datetime.strptime(
end_oop, "%d/%m/%y"
).date()
end_oop_date = datetime.datetime.strptime(end_oop, "%d/%m/%y").date()
if start_oop_date >= end_oop_date:
raise ValueError("End OOP date must be after start date")
@@ -178,10 +201,7 @@ class Worker(BaseModel):
# loop throught dates converting to week / day combination
for item in self.pref_not_to_work:
days_from_start = (
item.date
- Rota.start_date
).days
days_from_start = (item.date - Rota.start_date).days
week = days_from_start // 7 + 1
day = Rota.days[(days_from_start % 7)]
# Weight the value to take into account the number of preferences
@@ -195,22 +215,20 @@ class Worker(BaseModel):
# print(not_available_to_work)
# loop throught dates converting to week / day combination
for item in self.not_available_to_work:
days_from_start = (
item.date
- Rota.start_date
).days
days_from_start = (item.date - Rota.start_date).days
week = days_from_start // 7 + 1
day = Rota.days[(days_from_start % 7)]
Rota.unavailable_to_work.add((self.id, week, day))
Rota.unavailable_to_work_reason[(self.id, week, day)] = item.reason
for item in self.work_requests:
days_from_start = (
item.date
- Rota.start_date
).days
week = days_from_start // 7 + 1
day = Rota.days[(days_from_start % 7)]
days_from_start = (item.date - Rota.start_date).days
week = days_from_start // 7 + 1
day = Rota.days[(days_from_start % 7)]
if item.shift == "*":
for shift in Rota.get_shifts_for_worker_site(self.site):
Rota.work_requests.add((self.id, week, day, shift.name))
else:
Rota.work_requests.add((self.id, week, day, item.shift))
# Calculate the proportion of the rota that is being worked
@@ -236,6 +254,9 @@ class Worker(BaseModel):
self.non_working_day_list.append((item.day, start_date, end_date))
if days_to_work < 1:
self.fte_adj = 0
def __lt__(self, other) -> bool:
return (self.site, self.grade, self.fte_adj, self.name) < (
other.site,
@@ -246,7 +267,11 @@ class Worker(BaseModel):
def __str__(self) -> str:
nwds = ", ".join([str(i) for i, start, end in self.non_working_day_list]) if self.non_working_day_list is not None else ""
nwds = (
", ".join([str(i) for i, start, end in self.non_working_day_list])
if self.non_working_day_list is not None
else ""
)
return "{} {} [{}] REMOTE SITE:{}".format(
self.name,
(self.site, self.grade, self.fte_adj),