Refactor pro metadata handling + update database schema

This commit is contained in:
Théophile Diot 2024-03-04 07:57:50 +00:00
parent 053202e131
commit bd07f573d3
No known key found for this signature in database
GPG key ID: 248FEA4BAE400D06
6 changed files with 45 additions and 116 deletions

File diff suppressed because one or more lines are too long

Before

Width:  |  Height:  |  Size: 905 KiB

After

Width:  |  Height:  |  Size: 907 KiB

View file

@ -41,6 +41,12 @@ try:
# ? Get version and integration of BunkerWeb
data: Dict[str, Any] = db.get_metadata()
data.pop("pro_expire", None)
data.pop("pro_services", None)
data.pop("pro_overlapped", None)
data.pop("pro_status", None)
db_config = db.get_config(methods=True, with_drafts=True)
services = db_config.get("SERVER_NAME", {"value": ""})["value"].split(" ")
multisite = db_config.get("MULTISITE", {"value": "no"})["value"] == "yes"

View file

@ -101,11 +101,15 @@ try:
if resp.headers.get("Content-Type") == "application/zip":
logger.info("🚀 Your BunkerWeb Pro license is valid, checking if there are new or updated pro plugins...")
db.set_is_pro(True)
db.set_pro_expire("")
db.set_pro_status("valid")
db.set_pro_overlapped(False)
db.set_pro_services("")
db.set_pro_metadata(
{
"is_pro": True,
"pro_expire": None,
"pro_status": "valid",
"pro_overlapped": False,
"pro_services": 0,
}
)
with BytesIO(resp.content) as plugin_content:
with ZipFile(plugin_content) as zf:
@ -116,11 +120,15 @@ try:
message = "Your BunkerWeb Pro license is not valid or has expired"
logger.warning(f"{message}, only checking if there are new or updated info about pro plugins...")
db.set_is_pro(False)
db.set_pro_expire("")
db.set_pro_status("invalid")
db.set_pro_overlapped(False)
db.set_pro_services("")
db.set_pro_metadata( # TODO: set other pro metadata than is_pro correctly
{
"is_pro": False,
"pro_expire": None,
"pro_status": "invalid",
"pro_overlapped": False,
"pro_services": 0,
}
)
plugins = resp.json()
for plugin in plugins["data"]:

View file

@ -223,8 +223,8 @@ class Database:
return ""
def set_is_pro(self, value: bool = False) -> str:
"""Set the is_pro value"""
def set_pro_metadata(self, data: Dict[Literal["is_pro", "pro_expire", "pro_status", "pro_overlapped", "pro_services"], Any] = {}) -> str:
"""Set the pro metadata values"""
with self.__db_session() as session:
try:
metadata = session.query(Metadata).get(1)
@ -232,109 +232,14 @@ class Database:
if not metadata:
return "The metadata are not set yet, try again"
metadata.is_pro = value
for key, value in data.items():
setattr(metadata, key, value)
session.commit()
except BaseException:
return format_exc()
return ""
def set_pro_expire(self, value: str) -> str:
"""Set the pro_expire value"""
with self.__db_session() as session:
try:
metadata = session.query(Metadata).get(1)
if not metadata:
return "The metadata are not set yet, try again"
metadata.pro_expire = value
session.commit()
except BaseException:
return format_exc()
return ""
def get_pro_expire(self) -> str:
with self.__db_session() as session:
try:
metadata = session.query(Metadata).with_entities(Metadata.pro_expire).filter_by(id=1).first()
return metadata.pro_expire
except (ProgrammingError, OperationalError):
return ""
def set_pro_services(self, value: str) -> str:
"""Set the pro_services value"""
with self.__db_session() as session:
try:
metadata = session.query(Metadata).get(1)
if not metadata:
return "The metadata are not set yet, try again"
metadata.pro_services = value
session.commit()
except BaseException:
return format_exc()
return ""
def get_pro_services(self) -> str:
with self.__db_session() as session:
try:
metadata = session.query(Metadata).with_entities(Metadata.pro_services).filter_by(id=1).first()
return metadata.pro_services
except (ProgrammingError, OperationalError):
return ""
def set_pro_overlapped(self, value: bool = False) -> str:
"""Set the pro_overlapped value"""
with self.__db_session() as session:
try:
metadata = session.query(Metadata).get(1)
if not metadata:
return "The metadata are not set yet, try again"
metadata.pro_overlapped = value
session.commit()
except BaseException:
return format_exc()
return ""
def get_pro_overlapped(self) -> str:
with self.__db_session() as session:
try:
metadata = session.query(Metadata).with_entities(Metadata.pro_overlapped).filter_by(id=1).first()
return metadata.pro_overlapped
except (ProgrammingError, OperationalError):
return ""
def set_pro_status(self, value: str) -> str:
"""Set the pro_status value"""
with self.__db_session() as session:
try:
metadata = session.query(Metadata).get(1)
if not metadata:
return "The metadata are not set yet, try again"
metadata.pro_status = value
session.commit()
except BaseException:
return format_exc()
return ""
def get_pro_status(self) -> str:
with self.__db_session() as session:
try:
metadata = session.query(Metadata).with_entities(Metadata.pro_status).filter_by(id=1).first()
return metadata.pro_status
except (ProgrammingError, OperationalError):
return ""
def is_scheduler_first_start(self) -> bool:
"""Check if it's the scheduler's first start"""
with self.__db_session() as session:
@ -386,7 +291,16 @@ class Database:
def get_metadata(self) -> Dict[str, str]:
"""Get the metadata from the database"""
data = {"version": "1.5.6", "integration": "unknown", "database_version": "Unknown", "is_pro": "no"}
data = {
"version": "1.5.6",
"integration": "unknown",
"database_version": "Unknown",
"is_pro": "no",
"pro_expire": None,
"pro_services": 0,
"pro_overlapped": False,
"pro_status": "invalid",
}
database = self.database_uri.split(":")[0].split("+")[0]
with self.__db_session() as session:
with suppress(ProgrammingError, OperationalError):

View file

@ -41,6 +41,7 @@ INTEGRATIONS_ENUM = Enum(
)
STREAM_TYPES_ENUM = Enum("no", "yes", "partial", name="stream_types_enum")
PLUGIN_TYPES_ENUM = Enum("core", "external", "pro", name="plugin_types_enum")
PRO_STATUS_ENUM = Enum("invalid", "valid", "expired", name="pro_status_enum")
Base = declarative_base()
@ -270,9 +271,9 @@ class Metadata(Base):
id = Column(Integer, primary_key=True, default=1)
is_initialized = Column(Boolean, nullable=False)
is_pro = Column(Boolean, default=False, nullable=False)
pro_expire = Column(String(32), default="", nullable=False)
pro_status = Column(String(32), default="", nullable=False)
pro_services = Column(String(256), default="", nullable=False)
pro_expire = Column(DateTime, nullable=True)
pro_status = Column(PRO_STATUS_ENUM, default="invalid", nullable=False)
pro_services = Column(Integer, default=0, nullable=False)
pro_overlapped = Column(Boolean, default=False, nullable=False)
first_config_saved = Column(Boolean, nullable=False)
autoconf_loaded = Column(Boolean, default=False, nullable=True)

View file

@ -334,7 +334,7 @@ def inject_variables():
return dict(
dark_mode=app.config["DARK_MODE"],
script_nonce=app.config["SCRIPT_NONCE"],
is_pro_version=db.get_metadata()["is_pro"],
is_pro_version=db.get_metadata()["is_pro"] == "yes",
pro_status=db.get_metadata()["pro_status"],
pro_services=db.get_metadata()["pro_services"],
pro_expire=db.get_metadata()["pro_expire"],