from datetime import datetime from sqlalchemy import Column, DateTime, ForeignKey, Integer, String from app.database import Base class InviteCode(Base): """A code an administrator issues so one person can register. Single use by default: the point of invite-only is knowing who came in, and a code that works forever is a password shared by everyone who has seen it. A code is never deleted once used — who it let in is the record worth keeping. """ __tablename__ = "invite_codes" id = Column(Integer, primary_key=True, index=True) code = Column(String(32), unique=True, nullable=False, index=True) note = Column(String(200), nullable=True) # who it was meant for created_by = Column(Integer, ForeignKey("users.id", ondelete="SET NULL"), nullable=True) created_at = Column(DateTime, default=datetime.utcnow) #: Set when someone registers with it. Present means spent. used_by = Column(Integer, ForeignKey("users.id", ondelete="SET NULL"), nullable=True) used_at = Column(DateTime, nullable=True) #: An administrator can withdraw a code that has not been used. revoked_at = Column(DateTime, nullable=True)