51 lines
1.6 KiB
Python
51 lines
1.6 KiB
Python
from __future__ import annotations
|
|
|
|
import datetime
|
|
import uuid
|
|
|
|
from typing import TYPE_CHECKING, Optional
|
|
|
|
from sqlalchemy import Column, DateTime, UniqueConstraint
|
|
from sqlalchemy.dialects.postgresql import UUID
|
|
from sqlalchemy.orm import Mapped, relationship
|
|
from sqlalchemy.schema import ForeignKey
|
|
|
|
from .meta import Base
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
# if the target of the relationship is in another module
|
|
# that cannot normally be imported at runtime
|
|
from .client import Client
|
|
from .user import User
|
|
|
|
|
|
class LoginHistory(Base):
|
|
__tablename__ = "login_history"
|
|
__table_args__ = (UniqueConstraint("user_id", "client_id", "date"),)
|
|
id: uuid.UUID = Column("login_history_id", UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
|
user_id: uuid.UUID = Column("user_id", UUID(as_uuid=True), ForeignKey("users.id"), nullable=False)
|
|
client_id: uuid.UUID = Column(
|
|
"client_id",
|
|
UUID(as_uuid=True),
|
|
ForeignKey("clients.client_id"),
|
|
nullable=False,
|
|
)
|
|
date: datetime.datetime = Column("date", DateTime(), nullable=False)
|
|
|
|
user: Mapped["User"] = relationship("User", back_populates="login_history")
|
|
client: Mapped["Client"] = relationship("Client", back_populates="login_history")
|
|
|
|
def __init__(
|
|
self,
|
|
user_id: uuid.UUID,
|
|
client_id: uuid.UUID,
|
|
date: Optional[datetime.datetime] = None,
|
|
id_: Optional[uuid.UUID] = None,
|
|
) -> None:
|
|
self.user_id = user_id
|
|
self.client_id = client_id
|
|
self.date = datetime.datetime.utcnow() if date is None else date
|
|
if id_ is not None:
|
|
self.id = id_
|