Compare commits
2 commits
main
...
carpool-ev
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
737f3f4a34 | ||
|
|
4d9b217dd2 |
|
|
@ -5,14 +5,14 @@ import os.path
|
||||||
import re
|
import re
|
||||||
from glob import glob
|
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 datetime import datetime
|
||||||
|
|
||||||
from amarillo.models.Carpool import Carpool
|
from amarillo.models.Carpool import Carpool
|
||||||
from amarillo.routers.users import verify_permission_for_same_agency_or_admin
|
from amarillo.routers.users import verify_permission_for_same_agency_or_admin
|
||||||
from amarillo.services.oauth2 import get_current_agency
|
from amarillo.services.oauth2 import get_current_agency
|
||||||
from amarillo.tests.sampledata import examples
|
from amarillo.tests.sampledata import examples
|
||||||
|
from amarillo.services.hooks import run_on_create, run_on_delete
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
@ -32,10 +32,12 @@ router = APIRouter(
|
||||||
"description": "Agency does not exist"},
|
"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:
|
requesting_agency_id: str = Depends(get_current_agency)) -> Carpool:
|
||||||
await verify_permission_for_same_agency_or_admin(carpool.agency, requesting_agency_id)
|
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}.")
|
logger.info(f"POST trip {carpool.agency}:{carpool.id}.")
|
||||||
await assert_agency_exists(carpool.agency)
|
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"},
|
"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)
|
await verify_permission_for_same_agency_or_admin(agency_id, requesting_agency_id)
|
||||||
|
|
||||||
logger.info(f"Delete trip {agency_id}:{carpool_id}.")
|
logger.info(f"Delete trip {agency_id}:{carpool_id}.")
|
||||||
await assert_agency_exists(agency_id)
|
await assert_agency_exists(agency_id)
|
||||||
await assert_carpool_exists(agency_id, carpool_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)
|
return await _delete_carpool(agency_id, carpool_id)
|
||||||
|
|
||||||
|
|
@ -90,12 +94,6 @@ async def _delete_carpool(agency_id: str, carpool_id: str):
|
||||||
await save_carpool(cp, 'data/trash')
|
await save_carpool(cp, 'data/trash')
|
||||||
logger.info(f"Saved carpool {agency_id}:{carpool_id} in trash.")
|
logger.info(f"Saved carpool {agency_id}:{carpool_id} in trash.")
|
||||||
os.remove(f"data/carpool/{agency_id}/{carpool_id}.json")
|
os.remove(f"data/carpool/{agency_id}/{carpool_id}.json")
|
||||||
|
|
||||||
try:
|
|
||||||
from amarillo.plugins.metrics import trips_deleted_counter
|
|
||||||
trips_deleted_counter.inc()
|
|
||||||
except ImportError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
async def store_carpool(carpool: Carpool) -> Carpool:
|
async def store_carpool(carpool: Carpool) -> Carpool:
|
||||||
|
|
@ -104,17 +102,6 @@ async def store_carpool(carpool: Carpool) -> Carpool:
|
||||||
await set_lastUpdated_if_unset(carpool)
|
await set_lastUpdated_if_unset(carpool)
|
||||||
await save_carpool(carpool)
|
await save_carpool(carpool)
|
||||||
|
|
||||||
try:
|
|
||||||
from amarillo.plugins.metrics import trips_created_counter, trips_updated_counter
|
|
||||||
if(carpool_exists):
|
|
||||||
# logger.info("Incrementing trips updated")
|
|
||||||
trips_updated_counter.inc()
|
|
||||||
else:
|
|
||||||
# logger.info("Incrementing trips created")
|
|
||||||
trips_created_counter.inc()
|
|
||||||
except ImportError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
return carpool
|
return carpool
|
||||||
|
|
||||||
async def set_lastUpdated_if_unset(carpool):
|
async def set_lastUpdated_if_unset(carpool):
|
||||||
|
|
@ -155,4 +142,6 @@ async def delete_agency_carpools_older_than(agency_id, timestamp):
|
||||||
if os.path.getmtime(carpool_file_name) < timestamp:
|
if os.path.getmtime(carpool_file_name) < timestamp:
|
||||||
m = re.search(r'([a-zA-Z0-9_-]+)\.json$', carpool_file_name)
|
m = re.search(r'([a-zA-Z0-9_-]+)\.json$', carpool_file_name)
|
||||||
# TODO log deletion
|
# TODO log deletion
|
||||||
|
cp = await load_carpool(agency_id, m[1])
|
||||||
|
run_on_delete(cp)
|
||||||
await _delete_carpool(agency_id, m[1])
|
await _delete_carpool(agency_id, m[1])
|
||||||
|
|
|
||||||
|
|
@ -37,11 +37,6 @@ class CarpoolService():
|
||||||
if cp and self.is_outdated(cp):
|
if cp and self.is_outdated(cp):
|
||||||
logger.info("Purge outdated offer %s", key)
|
logger.info("Purge outdated offer %s", key)
|
||||||
self.delete(cp.agency, cp.id)
|
self.delete(cp.agency, cp.id)
|
||||||
try:
|
|
||||||
from amarillo.plugins.metrics import trips_deleted_counter
|
|
||||||
trips_deleted_counter.inc()
|
|
||||||
except ImportError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
def get(self, agency_id: str, carpool_id: str):
|
def get(self, agency_id: str, carpool_id: str):
|
||||||
return self.carpools.get(f"{agency_id}:{carpool_id}")
|
return self.carpools.get(f"{agency_id}:{carpool_id}")
|
||||||
|
|
|
||||||
27
amarillo/services/hooks.py
Normal file
27
amarillo/services/hooks.py
Normal 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)
|
||||||
Loading…
Reference in a new issue