- APIKey.alias (unique, required). Creating with an existing alias renews that key: same key string kept, validity reset to 15d, reactivated, name/roles updated (response has renewed=true). - get_actor(): X-API-Key -> key alias, Bearer -> 'admin'. - markdown & patch create/update record author / created_at / updated_at / last_modified_by from the actor. - Idempotent run_migrations() (information_schema-guarded ALTERs + backfill) so existing tables/data gain the new columns on startup; create_all still covers fresh DBs. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
48 lines
1.9 KiB
Python
48 lines
1.9 KiB
Python
import uuid
|
|
|
|
from sqlalchemy import Column, Text, Integer, String, DateTime, ForeignKey, UniqueConstraint
|
|
from db.models import Base
|
|
import datetime
|
|
|
|
class Markdown(Base):
|
|
__tablename__ = 'markdown'
|
|
id = Column(Integer, primary_key=True)
|
|
title = Column(String(255), nullable=False)
|
|
content = Column(Text, nullable=False)
|
|
created_at = Column(DateTime, default=lambda: datetime.datetime.now(datetime.UTC))
|
|
updated_at = Column(
|
|
DateTime,
|
|
default=lambda: datetime.datetime.now(datetime.UTC),
|
|
onupdate=lambda: datetime.datetime.now(datetime.UTC),
|
|
)
|
|
# Actor strings: alias of the API key, or 'admin' for KC-logged-in UI.
|
|
author = Column(String(255), nullable=True)
|
|
last_modified_by = Column(String(255), nullable=True)
|
|
path_id = Column(Integer, ForeignKey('path.id'), nullable=False)
|
|
order = Column(String(36), default=lambda: str(uuid.uuid4()))
|
|
shortcut = Column(String(36), default="")
|
|
setting_id = Column(Integer, ForeignKey("markdown_setting.id"), nullable=True)
|
|
__table_args__ = (UniqueConstraint('path_id', 'title', name="unique_path_id_title"),)
|
|
def to_dict(self):
|
|
return {
|
|
'id': self.id,
|
|
'title': self.title,
|
|
'content': self.content,
|
|
'created_at': self.created_at,
|
|
'updated_at': self.updated_at,
|
|
'author': self.author,
|
|
'last_modified_by': self.last_modified_by,
|
|
'path_id': self.path_id,
|
|
'order': self.order,
|
|
'shortcut': self.shortcut,
|
|
'setting_id': self.setting_id,
|
|
}
|
|
__payload__ = {
|
|
'dev': [
|
|
{'id': 1, 'title': 'index', 'content': '{"markdown": ""}', 'created_at': datetime.datetime.utcnow, 'path_id': 1 },
|
|
],
|
|
'prod': [
|
|
{'id': 1, 'title': 'index', 'content': '{"markdown": ""}', 'created_at': datetime.datetime.utcnow, 'path_id': 1},
|
|
]
|
|
}
|