Files
ad/molodetz/database/media.py
T

55 lines
1.7 KiB
Python
Raw Normal View History

2026-10-05 09:36:20 +02:00
# retoor <retoor@molodetz.nl>
from molodetz.database.core import db, generate_uid, now_iso
def media_by_kind(kind):
if "media_items" not in db.tables:
return []
return list(db["media_items"].find(kind=kind, deleted_at=None, order_by=["position", "id"]))
def get_media_by_file(filename):
if "media_items" not in db.tables:
return None
return db["media_items"].find_one(filename=filename)
def upsert_media(filename, kind, caption, position, width, height, phash, thumb_directory, thumb_name):
existing = get_media_by_file(filename)
fields = {
"filename": filename,
"kind": kind,
"caption": caption,
"position": position,
"width": width,
"height": height,
"phash": phash,
"thumb_directory": thumb_directory,
"thumb_name": thumb_name,
"updated_at": now_iso(),
}
if existing:
fields["id"] = existing["id"]
db["media_items"].update(fields, ["id"])
return existing["uid"]
fields.update({"uid": generate_uid(), "created_at": now_iso(), "deleted_at": None, "deleted_by": None})
db["media_items"].insert(fields)
return fields["uid"]
def retire_media(filename):
existing = get_media_by_file(filename)
if not existing or existing.get("deleted_at"):
return False
stamp = now_iso()
db["media_items"].update({"id": existing["id"], "deleted_at": stamp, "deleted_by": "system", "updated_at": stamp}, ["id"])
return True
2026-10-05 09:36:20 +02:00
def count_media(kind=None):
if "media_items" not in db.tables:
return 0
if kind:
return db["media_items"].count(kind=kind, deleted_at=None)
return db["media_items"].count(deleted_at=None)