Spaces:
Sleeping
Sleeping
Download auth/models.py from Coeinterns/bank-statement-api: direct link, hf CLI and curl.
- Browser
- Download file 3.58 kB
-
https://huggingface.co/spaces/Coeinterns/bank-statement-api/resolve/main/auth/models.py
- Command line
-
hf download hf://spaces/Coeinterns/bank-statement-api/auth/models.py
-
curl -L -o models.py https://huggingface.co/spaces/Coeinterns/bank-statement-api/resolve/main/auth/models.py
3.58 kB
| """ | |
| auth/models.py β SQLAlchemy ORM models for users, sessions, and audit logs. | |
| """ | |
| import uuid | |
| from datetime import datetime | |
| from sqlalchemy import ( | |
| BigInteger, | |
| Boolean, | |
| Column, | |
| DateTime, | |
| ForeignKey, | |
| Index, | |
| Integer, | |
| String, | |
| Text, | |
| ) | |
| from sqlalchemy.orm import relationship | |
| from auth.database import Base | |
| class User(Base): | |
| __tablename__ = "users" | |
| id = Column(String, primary_key=True, default=lambda: str(uuid.uuid4())) | |
| username = Column(String(100), unique=True, nullable=False, index=True) | |
| email = Column(String(200), unique=True, nullable=True) | |
| hashed_password = Column(Text, nullable=False) | |
| full_name = Column(String(200), nullable=True) | |
| role = Column(String(50), nullable=False, default="tester") # 'tester' | 'admin' | 'manager' | |
| is_active = Column(Boolean, nullable=False, default=True) | |
| created_at = Column(DateTime, nullable=False, default=datetime.utcnow) | |
| last_login_at = Column(DateTime, nullable=True) | |
| sessions = relationship("UserSession", back_populates="user", cascade="all, delete-orphan") | |
| audit_logs = relationship("AuditLog", back_populates="user") | |
| def __repr__(self) -> str: | |
| return f"<User id={self.id} username={self.username} role={self.role}>" | |
| class UserSession(Base): | |
| __tablename__ = "user_sessions" | |
| id = Column(String, primary_key=True, default=lambda: str(uuid.uuid4())) | |
| user_id = Column(String, ForeignKey("users.id"), nullable=False, index=True) | |
| token_jti = Column(String(200), unique=True, nullable=False) # JWT jti claim | |
| created_at = Column(DateTime, nullable=False, default=datetime.utcnow) | |
| expires_at = Column(DateTime, nullable=False) | |
| is_revoked = Column(Boolean, nullable=False, default=False) | |
| ip_address = Column(String(50), nullable=True) | |
| user_agent = Column(Text, nullable=True) | |
| user = relationship("User", back_populates="sessions") | |
| def __repr__(self) -> str: | |
| return f"<UserSession id={self.id} user_id={self.user_id} revoked={self.is_revoked}>" | |
| class AuditLog(Base): | |
| __tablename__ = "audit_logs" | |
| id = Column(BigInteger, primary_key=True, autoincrement=True) | |
| user_id = Column(String, ForeignKey("users.id"), nullable=True, index=True) | |
| username = Column(String(100), nullable=True) # denormalised copy | |
| action = Column(String(200), nullable=False) | |
| resource_type = Column(String(100), nullable=True) | |
| resource_id = Column(String(200), nullable=True) | |
| http_method = Column(String(10), nullable=True) | |
| endpoint = Column(String(300), nullable=True) | |
| request_summary = Column(Text, nullable=True) | |
| response_status = Column(Integer, nullable=True) | |
| ip_address = Column(String(50), nullable=True) | |
| user_agent = Column(Text, nullable=True) | |
| occurred_at = Column(DateTime, nullable=False, default=datetime.utcnow, index=True) | |
| duration_ms = Column(Integer, nullable=True) | |
| # ββ UAT audit enrichments βββββββββββββββββββββββββββββββββββββββββββββββββ | |
| extra_data = Column(Text, nullable=True) # JSON blob: file names, tx counts, export format β¦ | |
| session_duration_seconds = Column(Integer, nullable=True) # loginβlogout delta (set on logout) | |
| user = relationship("User", back_populates="audit_logs") | |
| def __repr__(self) -> str: | |
| return f"<AuditLog id={self.id} action={self.action} username={self.username}>" | |