Carpool event hooks
All checks were successful
Amarillo/amarillo-gitea/amarillo-core/pipeline/head This commit looks good

This commit is contained in:
Csaba 2024-04-15 14:31:17 +02:00
parent 66cc746937
commit 4d9b217dd2
2 changed files with 37 additions and 4 deletions

View file

@ -5,14 +5,14 @@ import os.path
import re
from glob import glob
from fastapi import APIRouter, Body, HTTPException, status, Depends
from fastapi import APIRouter, Body, HTTPException, status, Depends, BackgroundTasks
from datetime import datetime
from amarillo.models.Carpool import Carpool
from amarillo.routers.users import verify_permission_for_same_agency_or_admin
from amarillo.services.oauth2 import get_current_agency
from amarillo.tests.sampledata import examples
from amarillo.services.hooks import run_on_create, run_on_delete
logger = logging.getLogger(__name__)
@ -32,10 +32,12 @@ router = APIRouter(
"description": "Agency does not exist"},
})
async def post_carpool(carpool: Carpool = Body(..., examples=examples),
async def post_carpool(background_tasks: BackgroundTasks, carpool: Carpool = Body(..., examples=examples),
requesting_agency_id: str = Depends(get_current_agency)) -> Carpool:
await verify_permission_for_same_agency_or_admin(carpool.agency, requesting_agency_id)
background_tasks.add_task(run_on_create, carpool)
logger.info(f"POST trip {carpool.agency}:{carpool.id}.")
await assert_agency_exists(carpool.agency)
@ -73,12 +75,14 @@ async def get_carpool(agency_id: str, carpool_id: str, api_key: str = Depends(ge
"description": "Carpool or agency not found"},
},
)
async def delete_carpool(agency_id: str, carpool_id: str, requesting_agency_id: str = Depends(get_current_agency)):
async def delete_carpool(background_tasks: BackgroundTasks, agency_id: str, carpool_id: str, requesting_agency_id: str = Depends(get_current_agency)):
await verify_permission_for_same_agency_or_admin(agency_id, requesting_agency_id)
logger.info(f"Delete trip {agency_id}:{carpool_id}.")
await assert_agency_exists(agency_id)
await assert_carpool_exists(agency_id, carpool_id)
cp = await load_carpool(agency_id, carpool_id)
background_tasks.add_task(run_on_delete, cp)
return await _delete_carpool(agency_id, carpool_id)
@ -155,4 +159,6 @@ async def delete_agency_carpools_older_than(agency_id, timestamp):
if os.path.getmtime(carpool_file_name) < timestamp:
m = re.search(r'([a-zA-Z0-9_-]+)\.json$', carpool_file_name)
# TODO log deletion
cp = await load_carpool(agency_id, m[1])
run_on_delete(cp)
await _delete_carpool(agency_id, m[1])

View file

@ -0,0 +1,27 @@
from typing import List
from amarillo.models.Carpool import Carpool
class CarpoolEvents:
def on_create(cp : Carpool):
pass
def on_update(cp : Carpool):
pass
def on_delete(cp : Carpool):
pass
carpool_event_listeners : List[CarpoolEvents] = []
def register_carpool_event_listener(cpe : CarpoolEvents):
carpool_event_listeners.append(cpe)
def run_on_create(cp: Carpool):
for cpe in carpool_event_listeners:
cpe.on_create(cp)
def run_on_update(cp: Carpool):
for cpe in carpool_event_listeners:
cpe.on_update(cp)
def run_on_delete(cp: Carpool):
for cpe in carpool_event_listeners:
cpe.on_delete(cp)