use current month for last_updated value

This commit is contained in:
jomo 2024-02-01 00:03:33 +01:00
parent f1384b72f2
commit 621db6ad2c
2 changed files with 8 additions and 7 deletions

12
main.py
View file

@ -1,5 +1,6 @@
import re import re
import time from calendar import timegm
from datetime import date
from fastapi import Depends, FastAPI, Response from fastapi import Depends, FastAPI, Response
from fastapi.staticfiles import StaticFiles from fastapi.staticfiles import StaticFiles
@ -46,7 +47,7 @@ async def list_items(db: Session = Depends(get_db)):
@app.put("/api/items/{id}") @app.put("/api/items/{id}")
async def put_item(id: str, item: Item, db: Session = Depends(get_db)): async def put_item(id: str, item: Item, db: Session = Depends(get_db)):
if item.last_updated is None: if item.last_updated is None:
item.last_updated = rough_timestamp() item.last_updated = month_timestamp()
if crud.put_item(db, id, item) == crud.PutItemResult.UPDATED: if crud.put_item(db, id, item) == crud.PutItemResult.UPDATED:
return Response(b"", status_code=204) return Response(b"", status_code=204)
return Response(b"", status_code=201) return Response(b"", status_code=201)
@ -57,10 +58,9 @@ async def delete_item(id: str, db: Session = Depends(get_db)):
crud.delete_item(db, id) crud.delete_item(db, id)
def rough_timestamp() -> int: def month_timestamp() -> int:
"""Provides an current timestamp with reduced resolution, to improve anonymity.""" """Provides the timestamp of the current month's beginning (for improved privacy)"""
granularity = 2**20 # about 12 days return timegm(date.today().replace(day=1).timetuple())
return int(time.time()) // granularity * granularity
app.mount("/", StaticFiles(directory="static", html=True), name="static") app.mount("/", StaticFiles(directory="static", html=True), name="static")

View file

@ -176,5 +176,6 @@ function formCoordsToMap() {
function formatTimestamp(ts) { function formatTimestamp(ts) {
const date = new Date(ts * 1000); const date = new Date(ts * 1000);
return date.toLocaleDateString(); // using Swedish format as a hack to get an iso formatted date
return date.toLocaleDateString("sv", {timeZone: "UTC"}).replace(/\-\d+$/,'')
} }