added info column

This commit is contained in:
2025-10-18 16:32:47 -04:00
parent 4e2527f75b
commit 4396c185b2
3 changed files with 41 additions and 12 deletions

View File

@@ -9,8 +9,10 @@ import string
import psycopg
import asyncio
DEBUG = False # Reveal debug tools such as /docs swagger UI
ENV = dotenv_values(".env") # .env file
VERSION = "v1.0.0" # version number
VERSION = "v1.0.1" # version number
ALPHABET = string.ascii_uppercase + string.digits #alphabet and numbers for token generation
try:
LICENSE_KEY_PARTS = 5 if "NUM_KEY_CHUNKS" not in ENV else int(ENV["NUM_KEY_CHUNKS"]) #number of chunks in the new generated license keys
@@ -48,6 +50,7 @@ with psycopg.connect(connect_statement) as conn:
issue_timestamp TIMESTAMPTZ NOT NULL,
expiration_timestamp TIMESTAMPTZ,
last_used_timestamp TIMESTAMPTZ,
info TEXT,
is_active BOOLEAN NOT NULL DEFAULT TRUE
)
"""
@@ -62,8 +65,10 @@ with psycopg.connect(connect_statement) as conn:
)
conn.commit()
#app = FastAPI(docs_url=None, redoc_url=None, openapi_url=None)
app = FastAPI()
if DEBUG:
app = FastAPI()
else:
app = FastAPI(docs_url=None, redoc_url=None, openapi_url=None)
logger.info("Started FastAPI.")
@@ -83,6 +88,7 @@ async def create_license_key(
is_active: bool = True,
credentials: HTTPAuthorizationCredentials = Depends(security),
expiration_date: Optional[datetime] = None,
info: Optional[str] = None,
):
if (
credentials is None
@@ -114,15 +120,17 @@ async def create_license_key(
issue_timestamp,
expiration_timestamp,
last_used_timestamp,
info,
is_active
)
VALUES (%s, %s, %s, %s, %s)
VALUES (%s, %s, %s, %s, %s, %s)
""",
(
license_key,
issued_at,
expiration_ts,
None,
info,
is_active,
),
)
@@ -132,7 +140,17 @@ async def create_license_key(
VALUES (%s, %s)
""",
(
f"create_license_key key={license_key} active={is_active} expiration={expiration_ts.isoformat() if expiration_ts else 'none'}",
(
"create_license_key key={license_key} active={is_active} expiration={expiration} info={info}"
.format(
license_key=license_key,
is_active=is_active,
expiration=expiration_ts.isoformat()
if expiration_ts
else "none",
info=info if info is not None else "none",
)
),
issued_at,
),
)
@@ -151,6 +169,7 @@ async def create_license_key(
"license_key": license_key,
"expiration_timestamp": expiration_ts.isoformat() if expiration_ts else None,
"is_active": is_active,
"info": info,
}
@app.get("/is_valid")
@@ -401,7 +420,7 @@ async def export_license_keys(
with conn.cursor() as cur:
cur.execute(
"""
SELECT key, issue_timestamp, expiration_timestamp, is_active
SELECT key, issue_timestamp, expiration_timestamp, info, is_active
FROM license_keys
ORDER BY issue_timestamp ASC
"""
@@ -417,15 +436,16 @@ async def export_license_keys(
detail="Failed to export license keys.",
) from exc
lines = ["license_key,issue_timestamp,expiration_timestamp,is_active"]
for license_key, issue_ts, expiration_ts, is_active in rows:
lines = ["license_key,issue_timestamp,expiration_timestamp,info,is_active"]
for license_key, issue_ts, expiration_ts, info_value, is_active in rows:
lines.append(
"{key},{issue},{expiration},{active}".format(
"{key},{issue},{expiration},{info},{active}".format(
key=license_key,
issue=issue_ts.isoformat(),
expiration=expiration_ts.isoformat()
if expiration_ts
else "",
info=(info_value or "").replace("\n", " ").replace("\r", " ").replace(",", " "),
active="true" if is_active else "false",
)
)