46 lines
1.4 KiB
Python
46 lines
1.4 KiB
Python
# 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 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)
|