mirror of
https://github.com/furyhawk/agent_alpha.git
synced 2026-07-21 02:25:34 +00:00
- Implemented SQLAlchemy ORM models for User with role-based access control. - Created API routes for user creation, retrieval, updating, and listing. - Added input and output schemas for user data handling. - Included session management for users.
57 lines
1.6 KiB
Python
57 lines
1.6 KiB
Python
"""SQLAlchemy ORM models for Agent Alpha."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import uuid
|
|
from datetime import datetime, timezone
|
|
|
|
from sqlalchemy import Boolean, DateTime, String
|
|
from sqlalchemy.dialects.postgresql import UUID
|
|
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
|
|
|
|
|
|
class Base(DeclarativeBase):
|
|
"""Declarative base for all ORM models."""
|
|
|
|
|
|
class User(Base):
|
|
"""Application user with role-based access control.
|
|
|
|
Roles: ``admin``, ``user``, ``viewer``
|
|
"""
|
|
|
|
__tablename__ = "users"
|
|
|
|
id: Mapped[uuid.UUID] = mapped_column(
|
|
UUID(as_uuid=True),
|
|
primary_key=True,
|
|
default=uuid.uuid4,
|
|
)
|
|
username: Mapped[str] = mapped_column(
|
|
String(100), unique=True, nullable=False
|
|
)
|
|
display_name: Mapped[str] = mapped_column(String(200), nullable=False)
|
|
role: Mapped[str] = mapped_column(
|
|
String(20), nullable=False, default="user"
|
|
)
|
|
team: Mapped[str | None] = mapped_column(
|
|
String(100), nullable=True, default=None
|
|
)
|
|
is_active: Mapped[bool] = mapped_column(
|
|
Boolean, nullable=False, default=True
|
|
)
|
|
created_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True),
|
|
nullable=False,
|
|
default=lambda: datetime.now(timezone.utc),
|
|
)
|
|
updated_at: Mapped[datetime] = mapped_column(
|
|
DateTime(timezone=True),
|
|
nullable=False,
|
|
default=lambda: datetime.now(timezone.utc),
|
|
onupdate=lambda: datetime.now(timezone.utc),
|
|
)
|
|
|
|
def __repr__(self) -> str:
|
|
return f"<User {self.username!r} role={self.role!r}>"
|