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 re
import time
from fastapi import Depends, FastAPI, Response from fastapi import Depends, FastAPI, Response
from fastapi.staticfiles import StaticFiles from fastapi.staticfiles import StaticFiles
@ -44,6 +45,8 @@ 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:
item.last_updated = rough_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)
@ -54,4 +57,10 @@ 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:
"""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") app.mount("/", StaticFiles(directory="static", html=True), name="static")

View file

@ -1,4 +1,4 @@
from sqlalchemy import Boolean, Column, ForeignKey, String, Text from sqlalchemy import Boolean, Column, ForeignKey, Integer, String, Text
from database import Base from database import Base
@ -15,3 +15,4 @@ class Item(Base):
content = Column(Text) content = Column(Text)
note = Column(Text) note = Column(Text)
hidden = Column(Boolean, nullable=False) hidden = Column(Boolean, nullable=False)
last_updated = Column(Integer)

View file

@ -1,4 +1,4 @@
from pydantic import BaseModel from pydantic import BaseModel, Field
class Item(BaseModel): class Item(BaseModel):
@ -11,6 +11,7 @@ class Item(BaseModel):
content: str | None content: str | None
note: str | None note: str | None
hidden: bool hidden: bool
last_updated: int | None = Field(None)
class Config: class Config:
orm_mode = True orm_mode = True

View file

@ -43,6 +43,9 @@
<label for="hidden">Hide by default</label> <label for="hidden">Hide by default</label>
<input type="checkbox" id="hidden"> <input type="checkbox" id="hidden">
<label for="last_updated">Last updated</label>
<input id="last_updated" type="text" disabled>
</form> </form>
<button id="delete" class="btn red" autocomplete="off">Delete</button> <button id="delete" class="btn red" autocomplete="off">Delete</button>

View file

@ -39,6 +39,7 @@ function fillForm() {
document.getElementById('content').value = item.content; document.getElementById('content').value = item.content;
document.getElementById('note').value = item.note; document.getElementById('note').value = item.note;
document.getElementById('hidden').checked = item.hidden; document.getElementById('hidden').checked = item.hidden;
document.getElementById('last_updated').value = formatTimestamp(item.last_updated);
formCoordsToMap(); formCoordsToMap();
} }
@ -172,3 +173,8 @@ function formCoordsToMap() {
coordsToMap(coords_bl, coords_tr); coordsToMap(coords_bl, coords_tr);
} }
} }
function formatTimestamp(ts) {
const date = new Date(ts * 1000);
return date.toLocaleDateString();
}