kiwi-vpn/api/kiwi_vpn_api/db/models.py

67 lines
1.6 KiB
Python
Raw Normal View History

2022-03-20 03:45:40 +00:00
"""
SQLAlchemy representation of database contents.
"""
2022-03-19 23:56:11 +00:00
from __future__ import annotations
2022-03-25 23:03:56 +00:00
from sqlalchemy import (Column, DateTime, ForeignKey, Integer, String,
2022-03-17 17:06:00 +00:00
UniqueConstraint)
2022-03-17 22:47:31 +00:00
from sqlalchemy.ext.declarative import declarative_base
2022-03-25 23:03:56 +00:00
from sqlalchemy.orm import relationship
2022-03-17 17:06:00 +00:00
2022-03-17 22:47:31 +00:00
ORMBaseModel = declarative_base()
2022-03-17 17:06:00 +00:00
class UserCapability(ORMBaseModel):
__tablename__ = "user_capabilities"
user_name = Column(
String,
ForeignKey("users.name"),
primary_key=True,
index=True,
)
capability = Column(String, primary_key=True)
2022-03-25 15:50:45 +00:00
class User(ORMBaseModel):
__tablename__ = "users"
2022-03-17 17:06:00 +00:00
2022-03-25 15:50:45 +00:00
name = Column(String, primary_key=True, index=True)
password = Column(String, nullable=False)
2022-03-17 17:06:00 +00:00
country = Column(String(2))
state = Column(String)
city = Column(String)
organization = Column(String)
organizational_unit = Column(String)
2022-03-25 15:50:45 +00:00
2022-03-17 17:06:00 +00:00
email = Column(String)
2022-03-25 15:50:45 +00:00
capabilities: list[UserCapability] = relationship(
"UserCapability", lazy="joined", cascade="all, delete-orphan"
2022-03-23 15:30:22 +00:00
)
2022-03-25 15:50:45 +00:00
devices: list[Device] = relationship(
"Device", lazy="select", back_populates="owner"
2022-03-17 17:06:00 +00:00
)
2022-03-25 15:50:45 +00:00
class Device(ORMBaseModel):
__tablename__ = "devices"
2022-03-17 17:06:00 +00:00
id = Column(Integer, primary_key=True, autoincrement=True)
owner_name = Column(String, ForeignKey("users.name"))
2022-03-25 15:50:45 +00:00
name = Column(String)
type = Column(String)
2022-03-25 23:03:56 +00:00
expiry = Column(DateTime)
2022-03-22 16:29:02 +00:00
2022-03-25 15:50:45 +00:00
owner: User = relationship(
"User", lazy="joined", back_populates="distinguished_names"
2022-03-23 15:00:35 +00:00
)
2022-03-23 15:30:22 +00:00
2022-03-25 15:50:45 +00:00
UniqueConstraint(
owner_name,
name,
2022-03-23 15:30:22 +00:00
)