Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/db/import_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def main(

# Load mapping of employee to their job and their already planned shifts
prim_to_refberuf = import_solution.load_person_to_job(engine)
planned_map = import_solution.load_planned_shifts(planning_unit)
planned_map = import_solution.load_planned_shifts(planning_unit, from_date)

PE_ID = planning_unit
PLAN_ID = base_data["plan_id"]
Expand Down Expand Up @@ -71,9 +71,9 @@ def main(

filename = "test_file.json"

# Store JSON-file within given directory
# Store JSON-file within given directory (in the month folder)
json_output = json.dumps(output_json, ensure_ascii=False, indent=2, default=str)
store_path = import_solution.get_correct_path(filename)
store_path = import_solution.get_correct_path(filename, planning_unit, from_date)
with open(store_path, "w", encoding="utf-8") as f:
f.write(json_output)
# Print a message of completed export
Expand Down
23 changes: 18 additions & 5 deletions src/db/import_solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,12 @@
logging.basicConfig(level=logging.INFO)


def get_correct_path(filename: str, planning_unit: int) -> str:
"""Return the correct path to store the given file in."""
def get_correct_path(filename: str, planning_unit: int, from_date: date | None = None) -> str:
"""Return the correct path to store the given file in.

If `from_date` is provided the file will be placed inside the month folder
with format `MM_YYYY` (e.g. `11_2024`).
"""

# Get the defined folder names out of the .env-file
base_folder = os.getenv("BASE_OUTPUT_FOLDER")
Expand All @@ -21,6 +25,12 @@ def get_correct_path(filename: str, planning_unit: int) -> str:

# Create the output path to store the file in
target_dir = os.path.join("./", base_folder, str(planning_unit))

# Add month folder if date is provided
if from_date:
month_folder = f"{from_date.month:02d}_{from_date.year}"
target_dir = os.path.join(target_dir, month_folder)

target_dir = os.path.abspath(target_dir)
output_path = os.path.join(target_dir, filename)
return output_path
Expand All @@ -35,7 +45,8 @@ def load_json_files(start_date: date, end_date: date, planning_unit: int):
solution_dir = os.path.join("./", sol_folder)
solution_file = os.path.join(solution_dir, f"solution_{planning_unit}_{start_date}-{end_date}.json")

employee_file = get_correct_path("employees.json", planning_unit)
# employees.json is stored in the planning unit folder and in the month subfolder
employee_file = get_correct_path("employees.json", planning_unit, start_date)

with open(solution_file, encoding="utf-8") as f:
data = json.load(f)
Expand All @@ -44,13 +55,15 @@ def load_json_files(start_date: date, end_date: date, planning_unit: int):
return data, emp_data


def load_planned_shifts(planning_unit: int) -> dict[int, set[int]]:
def load_planned_shifts(planning_unit: int, start_date: date | None = None) -> dict[int, set[int]]:
"""
Returns Dict {Key : {Day1, Day2, …}},
based on planned_shifts in free_shifts_and_vacation_days.json.

If `start_date` is provided the file will be searched in the month folder for that date.
"""

file_path = get_correct_path("free_shifts_and_vacation_days.json", planning_unit)
file_path = get_correct_path("free_shifts_and_vacation_days.json", planning_unit, start_date)
with open(file_path, encoding="utf-8") as f:
data = json.load(f)["employees"]

Expand Down
Loading