добавил статус
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2026-05-05 21:24:45 +10:00
parent 46350c1c09
commit ef453f661a
3 changed files with 50 additions and 15 deletions

View File

@@ -324,42 +324,75 @@ def create_table_add_sourse():
cur.execute("""
CREATE TABLE IF NOT EXISTS sourse (
url TEXT PRIMARY KEY,
promt TEXT
promt TEXT,
status BOOLEAN DEFAULT FALSE
);
""")
print("Таблица sourse создана или уже существует")
finally:
pass
def add_sources(url: str, promt: str):
def add_sources(url: str, promt: str, status: bool = False):
conn = get_connection()
try:
with conn.cursor() as cur:
cur.execute("""
INSERT INTO sourse (url, promt)
VALUES (%s, %s)
INSERT INTO sourse (url, promt, status)
VALUES (%s, %s, %s)
ON CONFLICT (url) DO UPDATE SET
promt = EXCLUDED.promt
""", (url, promt))
promt = EXCLUDED.promt,
status = EXCLUDED.status
""", (url, promt, status))
conn.commit()
finally:
pass
def get_all_sources(category: str):
"""Возвращает все записи из таблицы sourse"""
"""Возвращает все записи из таблицы sourse. Сначала показываются записи со status=false"""
conn = get_connection()
try:
with conn.cursor(cursor_factory=RealDictCursor) as cur:
if category == "all":
cur.execute("SELECT * FROM sourse")
cur.execute("""
SELECT * FROM sourse
ORDER BY status ASC, url ASC
""")
else:
cur.execute("SELECT * FROM sourse WHERE promt = %s", (category,))
cur.execute("""
SELECT * FROM sourse
WHERE promt = %s
ORDER BY status ASC, url ASC
""", (category,))
rows = cur.fetchall()
sources = [{"url": row["url"], "promt": row["promt"]} for row in rows]
sources = [{"url": row["url"], "promt": row["promt"], "status": row["status"]} for row in rows]
return {"sources": sources}
except Exception as e:
print(f"Ошибка при получении источников: {e}")
return {"error": str(e), "sources": []}
finally:
pass
def update_source_status(url: str, status: bool = True):
"""Обновляет статус источника по URL"""
conn = get_connection()
try:
with conn.cursor() as cur:
cur.execute("""
UPDATE sourse SET status = %s WHERE url = %s
""", (status, url))
updated = cur.rowcount
conn.commit()
return {"message": f"Статус обновлён для {url}", "updated_rows": updated}
except Exception as e:
print(f"Ошибка при обновлении статуса: {e}")
return {"error": str(e), "updated_rows": 0}
finally:
pass
def delete_sources(url: str):
"""Удаляет источник по URL из таблицы sourse"""
conn = get_connection()