forked from retoor/devplacepy
refactor: split cli.py into package (ref.md 3.7)
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
from devplacepy.utils import strip_html
|
||||
from devplacepy.cli._shared import _audit_cli
|
||||
|
||||
|
||||
def cmd_news_clear(args):
|
||||
from devplacepy.database import db
|
||||
|
||||
deleted = {}
|
||||
for table in ("news", "news_images", "news_sync"):
|
||||
if table in db.tables:
|
||||
count = db[table].count()
|
||||
db[table].delete()
|
||||
deleted[table] = count
|
||||
print(f"Deleted {count} rows from '{table}'")
|
||||
else:
|
||||
print(f"Table '{table}' does not exist, skipping")
|
||||
_audit_cli("cli.news.clear", "CLI cleared all news data", metadata={"deleted": deleted})
|
||||
print("News data cleared")
|
||||
|
||||
|
||||
def cmd_news_sanitize(args):
|
||||
from devplacepy.database import db
|
||||
|
||||
if "news" not in db.tables:
|
||||
print("News table does not exist")
|
||||
return
|
||||
news_table = db["news"]
|
||||
updated = 0
|
||||
for row in news_table.all():
|
||||
desc = (strip_html(row.get("description", "") or ""))[:5000]
|
||||
content = (strip_html(row.get("content", "") or ""))[:10000]
|
||||
if desc != row.get("description", "") or content != row.get("content", ""):
|
||||
news_table.update(
|
||||
{"id": row["id"], "description": desc, "content": content}, ["id"]
|
||||
)
|
||||
updated += 1
|
||||
_audit_cli("cli.news.sanitize", f"CLI sanitized {updated} news articles", metadata={"count": updated})
|
||||
print(f"Sanitized {updated} news article(s)")
|
||||
|
||||
|
||||
def register_news(subparsers):
|
||||
news = subparsers.add_parser("news", help="News management")
|
||||
news_sub = news.add_subparsers(title="action", dest="action")
|
||||
news_clear = news_sub.add_parser(
|
||||
"clear", help="Delete all news from local database"
|
||||
)
|
||||
news_clear.set_defaults(func=cmd_news_clear)
|
||||
news_sanitize = news_sub.add_parser(
|
||||
"sanitize", help="Strip HTML from all existing news descriptions and content"
|
||||
)
|
||||
news_sanitize.set_defaults(func=cmd_news_sanitize)
|
||||
Reference in New Issue
Block a user