- 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>
43 lines
1.6 KiB
Python
43 lines
1.6 KiB
Python
from sqlalchemy import Column, Text, Integer, String, DateTime, ForeignKey
|
|
from db.models import Base
|
|
import datetime
|
|
|
|
|
|
class MarkdownPatch(Base):
|
|
"""A markdown 'patch card' attached to a parent markdown document.
|
|
|
|
Its content is plain markdown text rendered below the parent's body.
|
|
Created/updated by admin|creator, deleted by admin; visibility is
|
|
inherited from the parent markdown (enforced in the API layer).
|
|
"""
|
|
__tablename__ = 'markdown_patch'
|
|
id = Column(Integer, primary_key=True)
|
|
markdown_id = Column(
|
|
Integer, ForeignKey('markdown.id'), nullable=False, index=True
|
|
)
|
|
title = Column(String(255), nullable=True)
|
|
content = Column(Text, nullable=False)
|
|
# 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)
|
|
order = Column(Integer, default=0)
|
|
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),
|
|
)
|
|
|
|
def to_dict(self):
|
|
return {
|
|
'id': self.id,
|
|
'markdown_id': self.markdown_id,
|
|
'title': self.title,
|
|
'content': self.content,
|
|
'author': self.author,
|
|
'last_modified_by': self.last_modified_by,
|
|
'order': self.order,
|
|
'created_at': self.created_at,
|
|
'updated_at': self.updated_at,
|
|
}
|