Files
devplacepy/devplacepy/database/visibility.py
T
2026-11-05 17:59:21 +01:00

113 lines
3.3 KiB
Python

# retoor <retoor@molodetz.nl>
from sqlalchemy import or_
from .core import db, get_table, _index
PUBLISHED = "published"
HIDDEN = "hidden"
DRAFT = "draft"
OFFLINE = "offline"
VISIBILITY_VALUES = (PUBLISHED, HIDDEN, DRAFT, OFFLINE)
VISIBILITY_CHOICES = (PUBLISHED, HIDDEN, OFFLINE)
DEFAULT_VISIBILITY = PUBLISHED
LISTED_VALUES = (PUBLISHED,)
OPEN_VALUES = (PUBLISHED, HIDDEN)
VISIBILITY_COLUMNS = {
"posts": "visibility",
"gists": "visibility",
"projects": "visibility",
"news": "status",
}
TARGET_TABLES = {
"post": "posts",
"gist": "gists",
"project": "projects",
"news": "news",
}
VISIBILITY_LABELS = {
PUBLISHED: "Published",
HIDDEN: "Hidden",
DRAFT: "Draft",
OFFLINE: "Offline",
}
VISIBILITY_HINTS = {
PUBLISHED: "Listed everywhere and open to everyone.",
HIDDEN: "Not listed or searchable; anyone with the link can still open it.",
DRAFT: "Only you can see it. Not published yet.",
OFFLINE: "Taken offline for now. Only you can see it; publish it again at any time.",
}
def normalize_visibility(value, fallback: str = DEFAULT_VISIBILITY) -> str:
text = str(value or "").strip().lower()
return text if text in VISIBILITY_VALUES else fallback
def visibility_column(table_name: str) -> str | None:
return VISIBILITY_COLUMNS.get(table_name)
def item_visibility(table_name: str, item: dict | None) -> str:
column = visibility_column(table_name)
if not column or not item:
return DEFAULT_VISIBILITY
return normalize_visibility(item.get(column))
def is_listed(table_name: str, item: dict | None) -> bool:
return item_visibility(table_name, item) in LISTED_VALUES
def is_open(table_name: str, item: dict | None) -> bool:
return item_visibility(table_name, item) in OPEN_VALUES
def listed_clause(table, own_uid: str | None = None):
name = getattr(table, "name", None) or getattr(table.table, "name", None)
column_name = visibility_column(name)
if not column_name or not table.exists:
return None
columns = table.table.columns
if column_name not in columns:
return None
column = columns[column_name]
clause = or_(column.is_(None), column.in_(LISTED_VALUES))
if own_uid and "user_uid" in columns:
clause = or_(clause, columns.user_uid == own_uid)
return clause
def listed_sql(table_name: str, alias: str = "") -> str:
column = visibility_column(table_name)
if not column or table_name not in db.tables:
return "1 = 1"
if not get_table(table_name).has_column(column):
return "1 = 1"
prefix = f"{alias}." if alias else ""
return f"COALESCE({prefix}{column}, '{DEFAULT_VISIBILITY}') = '{PUBLISHED}'"
def ensure_visibility_columns() -> None:
for table_name, column in VISIBILITY_COLUMNS.items():
if table_name == "news" or table_name not in db.tables:
continue
table = get_table(table_name)
if not table.has_column(column):
table.create_column_by_example(column, "")
_index(db, table_name, f"idx_{table_name}_visibility", [column])
def visibility_options(values=VISIBILITY_CHOICES) -> list[dict]:
return [
{"value": v, "label": VISIBILITY_LABELS[v], "hint": VISIBILITY_HINTS[v]}
for v in values
]
def visibility_label(value) -> str:
return VISIBILITY_LABELS[normalize_visibility(value)]