lint: reformat python code with black
This commit is contained in:
parent
331beb01b4
commit
a406a7974b
88 changed files with 2579 additions and 1608 deletions
|
@ -10,8 +10,7 @@ from tldextract import extract
|
|||
from werkzeug.datastructures import FileStorage
|
||||
|
||||
from app.brm.brn import BRN
|
||||
from app.brm.utils import (create_data_uri, normalize_color,
|
||||
thumbnail_uploaded_image)
|
||||
from app.brm.utils import create_data_uri, normalize_color, thumbnail_uploaded_image
|
||||
from app.extensions import db
|
||||
from app.models import AbstractConfiguration, AbstractResource, Deprecation
|
||||
from app.models.base import Group, Pool
|
||||
|
@ -19,10 +18,10 @@ from app.models.onions import Onion
|
|||
from app.models.types import AwareDateTime
|
||||
|
||||
country_origin = db.Table(
|
||||
'country_origin',
|
||||
"country_origin",
|
||||
db.metadata,
|
||||
db.Column('country_id', db.ForeignKey('country.id'), primary_key=True),
|
||||
db.Column('origin_id', db.ForeignKey('origin.id'), primary_key=True),
|
||||
db.Column("country_id", db.ForeignKey("country.id"), primary_key=True),
|
||||
db.Column("origin_id", db.ForeignKey("origin.id"), primary_key=True),
|
||||
extend_existing=True,
|
||||
)
|
||||
|
||||
|
@ -45,7 +44,9 @@ class Origin(AbstractConfiguration):
|
|||
|
||||
group: Mapped[Group] = relationship("Group", back_populates="origins")
|
||||
proxies: Mapped[List[Proxy]] = relationship("Proxy", back_populates="origin")
|
||||
countries: Mapped[List[Country]] = relationship("Country", secondary=country_origin, back_populates='origins')
|
||||
countries: Mapped[List[Country]] = relationship(
|
||||
"Country", secondary=country_origin, back_populates="origins"
|
||||
)
|
||||
|
||||
@property
|
||||
def brn(self) -> BRN:
|
||||
|
@ -54,13 +55,18 @@ class Origin(AbstractConfiguration):
|
|||
product="mirror",
|
||||
provider="conf",
|
||||
resource_type="origin",
|
||||
resource_id=self.domain_name
|
||||
resource_id=self.domain_name,
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def csv_header(cls) -> List[str]:
|
||||
return super().csv_header() + [
|
||||
"group_id", "domain_name", "auto_rotation", "smart", "assets", "country"
|
||||
"group_id",
|
||||
"domain_name",
|
||||
"auto_rotation",
|
||||
"smart",
|
||||
"assets",
|
||||
"country",
|
||||
]
|
||||
|
||||
def destroy(self) -> None:
|
||||
|
@ -84,30 +90,41 @@ class Origin(AbstractConfiguration):
|
|||
@property
|
||||
def risk_level(self) -> Dict[str, int]:
|
||||
if self.risk_level_override:
|
||||
return {country.country_code: self.risk_level_override for country in self.countries}
|
||||
return {
|
||||
country.country_code: self.risk_level_override
|
||||
for country in self.countries
|
||||
}
|
||||
frequency_factor = 0.0
|
||||
recency_factor = 0.0
|
||||
recent_deprecations = (
|
||||
db.session.query(Deprecation)
|
||||
.join(Proxy,
|
||||
Deprecation.resource_id == Proxy.id)
|
||||
.join(Proxy, Deprecation.resource_id == Proxy.id)
|
||||
.join(Origin, Origin.id == Proxy.origin_id)
|
||||
.filter(
|
||||
Origin.id == self.id,
|
||||
Deprecation.resource_type == 'Proxy',
|
||||
Deprecation.deprecated_at >= datetime.now(tz=timezone.utc) - timedelta(hours=168),
|
||||
Deprecation.reason != "destroyed"
|
||||
Deprecation.resource_type == "Proxy",
|
||||
Deprecation.deprecated_at
|
||||
>= datetime.now(tz=timezone.utc) - timedelta(hours=168),
|
||||
Deprecation.reason != "destroyed",
|
||||
)
|
||||
.distinct(Proxy.id)
|
||||
.all()
|
||||
)
|
||||
for deprecation in recent_deprecations:
|
||||
recency_factor += 1 / max((datetime.now(tz=timezone.utc) - deprecation.deprecated_at).total_seconds() // 3600, 1)
|
||||
recency_factor += 1 / max(
|
||||
(
|
||||
datetime.now(tz=timezone.utc) - deprecation.deprecated_at
|
||||
).total_seconds()
|
||||
// 3600,
|
||||
1,
|
||||
)
|
||||
frequency_factor += 1
|
||||
risk_levels: Dict[str, int] = {}
|
||||
for country in self.countries:
|
||||
risk_levels[country.country_code.upper()] = int(
|
||||
max(1, min(10, frequency_factor * recency_factor))) + country.risk_level
|
||||
risk_levels[country.country_code.upper()] = (
|
||||
int(max(1, min(10, frequency_factor * recency_factor)))
|
||||
+ country.risk_level
|
||||
)
|
||||
return risk_levels
|
||||
|
||||
def to_dict(self) -> OriginDict:
|
||||
|
@ -128,13 +145,15 @@ class Country(AbstractConfiguration):
|
|||
product="country",
|
||||
provider="iso3166-1",
|
||||
resource_type="alpha2",
|
||||
resource_id=self.country_code
|
||||
resource_id=self.country_code,
|
||||
)
|
||||
|
||||
country_code: Mapped[str]
|
||||
risk_level_override: Mapped[Optional[int]]
|
||||
|
||||
origins = db.relationship("Origin", secondary=country_origin, back_populates='countries')
|
||||
origins = db.relationship(
|
||||
"Origin", secondary=country_origin, back_populates="countries"
|
||||
)
|
||||
|
||||
@property
|
||||
def risk_level(self) -> int:
|
||||
|
@ -144,29 +163,39 @@ class Country(AbstractConfiguration):
|
|||
recency_factor = 0.0
|
||||
recent_deprecations = (
|
||||
db.session.query(Deprecation)
|
||||
.join(Proxy,
|
||||
Deprecation.resource_id == Proxy.id)
|
||||
.join(Proxy, Deprecation.resource_id == Proxy.id)
|
||||
.join(Origin, Origin.id == Proxy.origin_id)
|
||||
.join(Origin.countries)
|
||||
.filter(
|
||||
Country.id == self.id,
|
||||
Deprecation.resource_type == 'Proxy',
|
||||
Deprecation.deprecated_at >= datetime.now(tz=timezone.utc) - timedelta(hours=168),
|
||||
Deprecation.reason != "destroyed"
|
||||
Deprecation.resource_type == "Proxy",
|
||||
Deprecation.deprecated_at
|
||||
>= datetime.now(tz=timezone.utc) - timedelta(hours=168),
|
||||
Deprecation.reason != "destroyed",
|
||||
)
|
||||
.distinct(Proxy.id)
|
||||
.all()
|
||||
)
|
||||
for deprecation in recent_deprecations:
|
||||
recency_factor += 1 / max((datetime.now(tz=timezone.utc) - deprecation.deprecated_at).total_seconds() // 3600, 1)
|
||||
recency_factor += 1 / max(
|
||||
(
|
||||
datetime.now(tz=timezone.utc) - deprecation.deprecated_at
|
||||
).total_seconds()
|
||||
// 3600,
|
||||
1,
|
||||
)
|
||||
frequency_factor += 1
|
||||
return int(max(1, min(10, frequency_factor * recency_factor)))
|
||||
|
||||
|
||||
class StaticOrigin(AbstractConfiguration):
|
||||
group_id = mapped_column(db.Integer, db.ForeignKey("group.id"), nullable=False)
|
||||
storage_cloud_account_id = mapped_column(db.Integer(), db.ForeignKey("cloud_account.id"), nullable=False)
|
||||
source_cloud_account_id = mapped_column(db.Integer(), db.ForeignKey("cloud_account.id"), nullable=False)
|
||||
storage_cloud_account_id = mapped_column(
|
||||
db.Integer(), db.ForeignKey("cloud_account.id"), nullable=False
|
||||
)
|
||||
source_cloud_account_id = mapped_column(
|
||||
db.Integer(), db.ForeignKey("cloud_account.id"), nullable=False
|
||||
)
|
||||
source_project = mapped_column(db.String(255), nullable=False)
|
||||
auto_rotate = mapped_column(db.Boolean, nullable=False)
|
||||
matrix_homeserver = mapped_column(db.String(255), nullable=True)
|
||||
|
@ -182,30 +211,34 @@ class StaticOrigin(AbstractConfiguration):
|
|||
product="mirror",
|
||||
provider="aws",
|
||||
resource_type="static",
|
||||
resource_id=self.domain_name
|
||||
resource_id=self.domain_name,
|
||||
)
|
||||
|
||||
group = db.relationship("Group", back_populates="statics")
|
||||
storage_cloud_account = db.relationship("CloudAccount", back_populates="statics",
|
||||
foreign_keys=[storage_cloud_account_id])
|
||||
source_cloud_account = db.relationship("CloudAccount", back_populates="statics",
|
||||
foreign_keys=[source_cloud_account_id])
|
||||
storage_cloud_account = db.relationship(
|
||||
"CloudAccount",
|
||||
back_populates="statics",
|
||||
foreign_keys=[storage_cloud_account_id],
|
||||
)
|
||||
source_cloud_account = db.relationship(
|
||||
"CloudAccount", back_populates="statics", foreign_keys=[source_cloud_account_id]
|
||||
)
|
||||
|
||||
def destroy(self) -> None:
|
||||
# TODO: The StaticMetaAutomation will clean up for now, but it should probably happen here for consistency
|
||||
super().destroy()
|
||||
|
||||
def update(
|
||||
self,
|
||||
source_project: str,
|
||||
description: str,
|
||||
auto_rotate: bool,
|
||||
matrix_homeserver: Optional[str],
|
||||
keanu_convene_path: Optional[str],
|
||||
keanu_convene_logo: Optional[FileStorage],
|
||||
keanu_convene_color: Optional[str],
|
||||
clean_insights_backend: Optional[Union[str, bool]],
|
||||
db_session_commit: bool,
|
||||
self,
|
||||
source_project: str,
|
||||
description: str,
|
||||
auto_rotate: bool,
|
||||
matrix_homeserver: Optional[str],
|
||||
keanu_convene_path: Optional[str],
|
||||
keanu_convene_logo: Optional[FileStorage],
|
||||
keanu_convene_color: Optional[str],
|
||||
clean_insights_backend: Optional[Union[str, bool]],
|
||||
db_session_commit: bool,
|
||||
) -> None:
|
||||
if isinstance(source_project, str):
|
||||
self.source_project = source_project
|
||||
|
@ -235,19 +268,29 @@ class StaticOrigin(AbstractConfiguration):
|
|||
elif isinstance(keanu_convene_logo, FileStorage):
|
||||
if keanu_convene_logo.filename: # if False, no file was uploaded
|
||||
keanu_convene_config["logo"] = create_data_uri(
|
||||
thumbnail_uploaded_image(keanu_convene_logo), keanu_convene_logo.filename)
|
||||
thumbnail_uploaded_image(keanu_convene_logo),
|
||||
keanu_convene_logo.filename,
|
||||
)
|
||||
else:
|
||||
raise ValueError("keanu_convene_logo must be a FileStorage")
|
||||
try:
|
||||
if isinstance(keanu_convene_color, str):
|
||||
keanu_convene_config["color"] = normalize_color(keanu_convene_color) # can raise ValueError
|
||||
keanu_convene_config["color"] = normalize_color(
|
||||
keanu_convene_color
|
||||
) # can raise ValueError
|
||||
else:
|
||||
raise ValueError() # re-raised below with message
|
||||
except ValueError:
|
||||
raise ValueError("keanu_convene_path must be a str containing an HTML color (CSS name or hex)")
|
||||
self.keanu_convene_config = json.dumps(keanu_convene_config, separators=(',', ':'))
|
||||
raise ValueError(
|
||||
"keanu_convene_path must be a str containing an HTML color (CSS name or hex)"
|
||||
)
|
||||
self.keanu_convene_config = json.dumps(
|
||||
keanu_convene_config, separators=(",", ":")
|
||||
)
|
||||
del keanu_convene_config # done with this temporary variable
|
||||
if clean_insights_backend is None or (isinstance(clean_insights_backend, bool) and not clean_insights_backend):
|
||||
if clean_insights_backend is None or (
|
||||
isinstance(clean_insights_backend, bool) and not clean_insights_backend
|
||||
):
|
||||
self.clean_insights_backend = None
|
||||
elif isinstance(clean_insights_backend, bool) and clean_insights_backend:
|
||||
self.clean_insights_backend = "metrics.cleaninsights.org"
|
||||
|
@ -260,7 +303,9 @@ class StaticOrigin(AbstractConfiguration):
|
|||
self.updated = datetime.now(tz=timezone.utc)
|
||||
|
||||
|
||||
ResourceStatus = Union[Literal["active"], Literal["pending"], Literal["expiring"], Literal["destroyed"]]
|
||||
ResourceStatus = Union[
|
||||
Literal["active"], Literal["pending"], Literal["expiring"], Literal["destroyed"]
|
||||
]
|
||||
|
||||
|
||||
class ProxyDict(TypedDict):
|
||||
|
@ -271,12 +316,16 @@ class ProxyDict(TypedDict):
|
|||
|
||||
|
||||
class Proxy(AbstractResource):
|
||||
origin_id: Mapped[int] = mapped_column(db.Integer, db.ForeignKey("origin.id"), nullable=False)
|
||||
origin_id: Mapped[int] = mapped_column(
|
||||
db.Integer, db.ForeignKey("origin.id"), nullable=False
|
||||
)
|
||||
pool_id: Mapped[Optional[int]] = mapped_column(db.Integer, db.ForeignKey("pool.id"))
|
||||
provider: Mapped[str] = mapped_column(db.String(20), nullable=False)
|
||||
psg: Mapped[Optional[int]] = mapped_column(db.Integer, nullable=True)
|
||||
slug: Mapped[Optional[str]] = mapped_column(db.String(20), nullable=True)
|
||||
terraform_updated: Mapped[Optional[datetime]] = mapped_column(AwareDateTime(), nullable=True)
|
||||
terraform_updated: Mapped[Optional[datetime]] = mapped_column(
|
||||
AwareDateTime(), nullable=True
|
||||
)
|
||||
url: Mapped[Optional[str]] = mapped_column(db.String(255), nullable=True)
|
||||
|
||||
origin: Mapped[Origin] = relationship("Origin", back_populates="proxies")
|
||||
|
@ -289,13 +338,18 @@ class Proxy(AbstractResource):
|
|||
product="mirror",
|
||||
provider=self.provider,
|
||||
resource_type="proxy",
|
||||
resource_id=str(self.id)
|
||||
resource_id=str(self.id),
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def csv_header(cls) -> List[str]:
|
||||
return super().csv_header() + [
|
||||
"origin_id", "provider", "psg", "slug", "terraform_updated", "url"
|
||||
"origin_id",
|
||||
"provider",
|
||||
"psg",
|
||||
"slug",
|
||||
"terraform_updated",
|
||||
"url",
|
||||
]
|
||||
|
||||
def to_dict(self) -> ProxyDict:
|
||||
|
@ -329,5 +383,5 @@ class SmartProxy(AbstractResource):
|
|||
product="mirror",
|
||||
provider=self.provider,
|
||||
resource_type="smart_proxy",
|
||||
resource_id=str(1)
|
||||
resource_id=str(1),
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue