2021-01-25 07:14:17 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import random
|
|
|
|
import string
|
|
|
|
import uuid
|
|
|
|
|
|
|
|
from datetime import datetime
|
|
|
|
from typing import List
|
|
|
|
|
|
|
|
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):
|
2021-09-11 00:32:09 +00:00
|
|
|
__tablename__ = "clients"
|
2021-01-25 07:14:17 +00:00
|
|
|
|
|
|
|
id = Column("client_id", UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
|
|
|
code = Column("code", Integer, unique=True, nullable=False)
|
|
|
|
name = Column("name", Unicode(255), unique=True, nullable=False)
|
|
|
|
enabled = Column("enabled", Boolean, nullable=False)
|
|
|
|
otp = Column("otp", Integer)
|
2021-09-06 15:06:36 +00:00
|
|
|
creation_date = Column("creation_date", DateTime(), nullable=False)
|
2021-01-25 07:14:17 +00:00
|
|
|
|
|
|
|
login_history: List[LoginHistory] = relationship("LoginHistory", order_by=desc(LoginHistory.date), backref="client")
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
code=None,
|
|
|
|
name=None,
|
|
|
|
enabled=False,
|
|
|
|
otp=None,
|
|
|
|
creation_date=None,
|
|
|
|
id_=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
|
|
|
|
self.id = id_
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def create(cls, db: Session):
|
|
|
|
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
|