refactor: moving more models to mapped_column
This commit is contained in:
parent
ea020d6edd
commit
75b2c1adf0
9 changed files with 272 additions and 94 deletions
|
@ -1,21 +1,36 @@
|
|||
from datetime import datetime
|
||||
from typing import List
|
||||
from typing import List, TypedDict, Optional, TYPE_CHECKING
|
||||
|
||||
from sqlalchemy import and_
|
||||
from sqlalchemy.orm import Mapped, aliased, mapped_column, relationship
|
||||
|
||||
from app.brm.brn import BRN
|
||||
from app.extensions import db
|
||||
from app.models import AbstractConfiguration
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from app.models.bridges import BridgeConf
|
||||
from app.models.mirrors import Origin, Proxy, SmartProxy, StaticOrigin
|
||||
from app.models.onions import Eotk, Onion
|
||||
|
||||
|
||||
class GroupDict(TypedDict):
|
||||
Id: int
|
||||
GroupName: str
|
||||
Description: str
|
||||
ActiveOriginCount: int
|
||||
|
||||
|
||||
class Group(AbstractConfiguration):
|
||||
group_name = db.Column(db.String(80), unique=True, nullable=False)
|
||||
eotk = db.Column(db.Boolean())
|
||||
group_name: Mapped[str] = db.Column(db.String(80), unique=True, nullable=False)
|
||||
eotk: Mapped[bool]
|
||||
|
||||
origins = db.relationship("Origin", back_populates="group")
|
||||
statics = db.relationship("StaticOrigin", back_populates="group")
|
||||
eotks = db.relationship("Eotk", back_populates="group")
|
||||
onions = db.relationship("Onion", back_populates="group")
|
||||
smart_proxies = db.relationship("SmartProxy", back_populates="group")
|
||||
pools = db.relationship("Pool", secondary="pool_group", back_populates="groups")
|
||||
origins: Mapped[List["Origin"]] = relationship("Origin", back_populates="group")
|
||||
statics: Mapped[List["StaticOrigin"]] = relationship("StaticOrigin", back_populates="group")
|
||||
eotks: Mapped[List["Eotk"]] = relationship("Eotk", back_populates="group")
|
||||
onions: Mapped[List["Onion"]] = relationship("Onion", back_populates="group")
|
||||
smart_proxies: Mapped[List["SmartProxy"]] = relationship("SmartProxy", back_populates="group")
|
||||
pools: Mapped[List["Pool"]] = relationship("Pool", secondary="pool_group", back_populates="groups")
|
||||
|
||||
@classmethod
|
||||
def csv_header(cls) -> List[str]:
|
||||
|
@ -33,25 +48,29 @@ class Group(AbstractConfiguration):
|
|||
resource_id=str(self.id)
|
||||
)
|
||||
|
||||
def to_dict(self):
|
||||
active_origins = [o for o in self.origins if o.destroyed is None]
|
||||
def to_dict(self) -> GroupDict:
|
||||
active_origins_query = (
|
||||
db.session.query(aliased(Origin))
|
||||
.filter(and_(Origin.group_id == self.id, Origin.destroyed.is_(None)))
|
||||
)
|
||||
active_origins_count = active_origins_query.count()
|
||||
return {
|
||||
"Id": self.id,
|
||||
"GroupName": self.group_name,
|
||||
"Description": self.description,
|
||||
"ActiveOriginCount": len(active_origins),
|
||||
"ActiveOriginCount": active_origins_count,
|
||||
}
|
||||
|
||||
|
||||
class Pool(AbstractConfiguration):
|
||||
pool_name = db.Column(db.String(80), unique=True, nullable=False)
|
||||
api_key = db.Column(db.String(80), nullable=False)
|
||||
redirector_domain = db.Column(db.String(128), nullable=True)
|
||||
pool_name: Mapped[str] = mapped_column(db.String, unique=True)
|
||||
api_key: Mapped[str]
|
||||
redirector_domain: Mapped[Optional[str]]
|
||||
|
||||
bridgeconfs = db.relationship("BridgeConf", back_populates="pool")
|
||||
proxies = db.relationship("Proxy", back_populates="pool")
|
||||
lists = db.relationship("MirrorList", back_populates="pool")
|
||||
groups = db.relationship("Group", secondary="pool_group", back_populates="pools")
|
||||
bridgeconfs: Mapped[List["BridgeConf"]] = relationship("BridgeConf", back_populates="pool")
|
||||
proxies: Mapped[List["Proxy"]] = relationship("Proxy", back_populates="pool")
|
||||
lists: Mapped[List["MirrorList"]] = relationship("MirrorList", back_populates="pool")
|
||||
groups: Mapped[List[Group]] = relationship("Group", secondary="pool_group", back_populates="pools")
|
||||
|
||||
@classmethod
|
||||
def csv_header(cls) -> List[str]:
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue