|
| 1 | +from copy import deepcopy |
| 2 | +from datetime import timedelta |
| 3 | +from dateutil.parser import parse |
| 4 | +from pythonbrasil.site import logger |
| 5 | + |
| 6 | + |
| 7 | +def patch_process_trainings(): |
| 8 | + from collective.techevent.services.schedule import get |
| 9 | + |
| 10 | + def process_trainings(slots: list[dict]) -> list[dict]: |
| 11 | + """Break whole day training sessions as 2 slots.""" |
| 12 | + slot_limit = 14400 # 4 hours |
| 13 | + lunch_break = 60 * 60 * 1 # 1 hour lunch break |
| 14 | + response = [] |
| 15 | + for slot in slots: |
| 16 | + raw_start = slot["start"] |
| 17 | + raw_end = slot["end"] |
| 18 | + if slot.get("@type") != "Training" or not (raw_start and raw_end): |
| 19 | + response.append(slot) |
| 20 | + continue |
| 21 | + start = parse(raw_start) |
| 22 | + end = parse(raw_end) |
| 23 | + if (total_duration := (end - start).seconds) > slot_limit: |
| 24 | + # change the first slot end |
| 25 | + slot_end = start + timedelta(seconds=slot_limit) |
| 26 | + slot["end"] = slot_end.isoformat() |
| 27 | + response.append(slot) |
| 28 | + slot = deepcopy(slot) |
| 29 | + # change the second slot start |
| 30 | + new_start = slot_end + timedelta(seconds=lunch_break) |
| 31 | + slot["start"] = new_start.isoformat() |
| 32 | + slot_end = new_start + timedelta(seconds=(total_duration - slot_limit)) |
| 33 | + slot["end"] = slot_end.isoformat() |
| 34 | + response.append(slot) |
| 35 | + else: |
| 36 | + response.append(slot) |
| 37 | + return response |
| 38 | + |
| 39 | + # Make a reference the original function |
| 40 | + get._original_process_trainings = get.process_trainings |
| 41 | + # Replace the original function with the patched one |
| 42 | + get.process_trainings = process_trainings |
| 43 | + logger.info("Patched collective.techevent.services.schedule.get.process_trainings") |
| 44 | + |
| 45 | + |
| 46 | +def apply_patches(): |
| 47 | + patch_process_trainings() |
| 48 | + logger.info("All patches applied.") |
0 commit comments