Add last_updated field to be able to track outdated information / lost items

This commit is contained in:
clonejo 2024-01-14 20:23:08 +01:00
parent 99df802a40
commit 1bf4eb801f
5 changed files with 22 additions and 2 deletions

View file

@ -1,4 +1,5 @@
import re
import time
from fastapi import Depends, FastAPI, Response
from fastapi.staticfiles import StaticFiles
@ -44,6 +45,8 @@ async def list_items(db: Session = Depends(get_db)):
@app.put("/api/items/{id}")
async def put_item(id: str, item: Item, db: Session = Depends(get_db)):
if item.last_updated is None:
item.last_updated = rough_timestamp()
if crud.put_item(db, id, item) == crud.PutItemResult.UPDATED:
return Response(b"", status_code=204)
return Response(b"", status_code=201)
@ -54,4 +57,10 @@ async def delete_item(id: str, db: Session = Depends(get_db)):
crud.delete_item(db, id)
def rough_timestamp() -> int:
"""Provides an current timestamp with reduced resolution, to improve anonymity."""
granularity = 2**20 # about 12 days
return int(time.time()) // granularity * granularity
app.mount("/", StaticFiles(directory="static", html=True), name="static")