57 lines
1.8 KiB
Python
57 lines
1.8 KiB
Python
from __future__ import annotations
|
|
|
|
import random
|
|
import string
|
|
import uuid
|
|
|
|
from datetime import datetime
|
|
from typing import List, Optional
|
|
|
|
from sqlalchemy import Boolean, Column, DateTime, Integer, Unicode, desc
|
|
from sqlalchemy.dialects.postgresql import UUID
|
|
from sqlalchemy.orm import Session, relationship
|
|
|
|
from .login_history import LoginHistory
|
|
from .meta import Base
|
|
|
|
|
|
class Client(Base):
|
|
__tablename__ = "clients"
|
|
|
|
id: uuid.UUID = Column("client_id", UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
|
code: int = Column("code", Integer, unique=True, nullable=False)
|
|
name: str = Column("name", Unicode(255), unique=True, nullable=False)
|
|
enabled: bool = Column("enabled", Boolean, nullable=False)
|
|
otp: Optional[int] = Column("otp", Integer)
|
|
creation_date: datetime = Column("creation_date", DateTime(), nullable=False)
|
|
|
|
login_history: List["LoginHistory"] = relationship(
|
|
"LoginHistory", order_by=desc(LoginHistory.date), back_populates="client"
|
|
)
|
|
|
|
def __init__(
|
|
self,
|
|
code: int,
|
|
name: str,
|
|
enabled: bool,
|
|
otp: Optional[int],
|
|
creation_date: Optional[datetime] = None,
|
|
id_: Optional[uuid.UUID] = None,
|
|
) -> None:
|
|
self.code = code
|
|
self.name = name
|
|
self.enabled = enabled
|
|
self.otp = otp
|
|
self.creation_date = datetime.utcnow() if creation_date is None else creation_date
|
|
if id_ is not None:
|
|
self.id = id_
|
|
|
|
@classmethod
|
|
def create(cls, db: Session) -> "Client":
|
|
client_code = random.randint(1000, 9999)
|
|
otp = random.randint(1000, 9999)
|
|
name = "".join(random.choice(string.ascii_uppercase + string.digits) for x in range(6))
|
|
client = Client(client_code, name, False, otp)
|
|
db.add(client)
|
|
return client
|