Напоминалки: таблица reminders и CRUD в database.py
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
+76
@@ -192,6 +192,21 @@ class DatabaseManager:
|
||||
)
|
||||
""")
|
||||
|
||||
# Table for admin reminders (настраиваемые напоминания: дата, текст, периодичность)
|
||||
cursor.execute("""
|
||||
CREATE TABLE IF NOT EXISTS reminders (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
text TEXT NOT NULL,
|
||||
remind_at TEXT NOT NULL, -- якорная дата первого срабатывания, не меняется
|
||||
next_at TEXT NOT NULL, -- следующее срабатывание, двигается вперёд
|
||||
recurrence TEXT NOT NULL, -- 'once' | 'weekly' | 'monthly' | 'yearly'
|
||||
recipients TEXT NOT NULL, -- JSON: список vk_id, либо строка "all"
|
||||
created_by INTEGER NOT NULL,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
active INTEGER NOT NULL DEFAULT 1
|
||||
)
|
||||
""")
|
||||
|
||||
conn.commit()
|
||||
|
||||
# ---------- Редактируемые ссылки (кнопки записи/оплаты) ----------
|
||||
@@ -606,6 +621,67 @@ class DatabaseManager:
|
||||
cursor.execute("SELECT user_id FROM admins ORDER BY user_id")
|
||||
return [r[0] for r in cursor.fetchall()]
|
||||
|
||||
# --- Напоминалки для админов (дата/время, периодичность) ---
|
||||
def add_reminder(self, text: str, remind_at: str, recurrence: str,
|
||||
recipients, created_by: int) -> int:
|
||||
"""recipients — список vk_id (list[int]) либо строка 'all'."""
|
||||
with self._get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(
|
||||
"INSERT INTO reminders (text, remind_at, next_at, recurrence, recipients, created_by) "
|
||||
"VALUES (?, ?, ?, ?, ?, ?)",
|
||||
(text, remind_at, remind_at, recurrence, json.dumps(recipients), created_by)
|
||||
)
|
||||
conn.commit()
|
||||
return cursor.lastrowid
|
||||
|
||||
def _rows_with_parsed_recipients(self, cursor) -> list:
|
||||
rows = [dict(r) for r in cursor.fetchall()]
|
||||
for r in rows:
|
||||
r["recipients"] = json.loads(r["recipients"])
|
||||
return rows
|
||||
|
||||
def get_active_reminders(self) -> list:
|
||||
"""Активные напоминания для отображения в админке, по next_at."""
|
||||
with self._get_connection() as conn:
|
||||
conn.row_factory = sqlite3.Row
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(
|
||||
"SELECT id, text, remind_at, next_at, recurrence, recipients, created_by "
|
||||
"FROM reminders WHERE active = 1 ORDER BY next_at"
|
||||
)
|
||||
return self._rows_with_parsed_recipients(cursor)
|
||||
|
||||
def get_due_reminders(self, now_str: str) -> list:
|
||||
"""Активные напоминания, чьё время (next_at) уже наступило."""
|
||||
with self._get_connection() as conn:
|
||||
conn.row_factory = sqlite3.Row
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(
|
||||
"SELECT id, text, remind_at, next_at, recurrence, recipients, created_by "
|
||||
"FROM reminders WHERE active = 1 AND next_at <= ?", (now_str,)
|
||||
)
|
||||
return self._rows_with_parsed_recipients(cursor)
|
||||
|
||||
def update_reminder_next_at(self, reminder_id: int, next_at: str):
|
||||
with self._get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("UPDATE reminders SET next_at = ? WHERE id = ?", (next_at, reminder_id))
|
||||
conn.commit()
|
||||
|
||||
def deactivate_reminder(self, reminder_id: int):
|
||||
with self._get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("UPDATE reminders SET active = 0 WHERE id = ?", (reminder_id,))
|
||||
conn.commit()
|
||||
|
||||
def delete_reminder(self, reminder_id: int) -> bool:
|
||||
with self._get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("DELETE FROM reminders WHERE id = ?", (reminder_id,))
|
||||
conn.commit()
|
||||
return cursor.rowcount > 0
|
||||
|
||||
# --- Быстрые ответы (custom FAQ) ---
|
||||
def add_custom_faq(self, keyword: str, answer: str) -> int:
|
||||
with self._get_connection() as conn:
|
||||
|
||||
Reference in New Issue
Block a user