- Add User model with email unique constraint and relationships - Add ApiKey model with encrypted key storage and user relationship - Add UsageStats model with unique constraint (api_key_id, date, model) - Add ApiToken model with token_hash indexing - Configure all cascade delete relationships - Add 49 comprehensive tests with 95% coverage Models: - User: id, email, password_hash, created_at, updated_at, is_active - ApiKey: id, user_id, name, key_encrypted, is_active, created_at, last_used_at - UsageStats: id, api_key_id, date, model, requests_count, tokens_input, tokens_output, cost - ApiToken: id, user_id, token_hash, name, created_at, last_used_at, is_active Tests: 49 passed, coverage 95%
38 lines
1.3 KiB
Python
38 lines
1.3 KiB
Python
"""ApiToken model for OpenRouter API Key Monitor.
|
|
|
|
T10: ApiToken SQLAlchemy model
|
|
"""
|
|
from datetime import datetime
|
|
from sqlalchemy import Column, Integer, String, DateTime, Boolean, ForeignKey
|
|
from sqlalchemy.orm import relationship
|
|
|
|
from openrouter_monitor.database import Base
|
|
|
|
|
|
class ApiToken(Base):
|
|
"""API Token model for public API access.
|
|
|
|
Attributes:
|
|
id: Primary key
|
|
user_id: Foreign key to users table
|
|
token_hash: SHA-256 hash of the token (not the token itself)
|
|
name: Human-readable name for the token
|
|
created_at: Timestamp when token was created
|
|
last_used_at: Timestamp when token was last used
|
|
is_active: Whether the token is active
|
|
user: Relationship to user
|
|
"""
|
|
|
|
__tablename__ = "api_tokens"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
user_id = Column(Integer, ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True)
|
|
token_hash = Column(String(255), nullable=False, index=True)
|
|
name = Column(String(100), nullable=False)
|
|
created_at = Column(DateTime, default=datetime.utcnow)
|
|
last_used_at = Column(DateTime, nullable=True)
|
|
is_active = Column(Boolean, default=True, index=True)
|
|
|
|
# Relationships
|
|
user = relationship("User", back_populates="api_tokens", lazy="selectin")
|