-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
433 lines (378 loc) · 13.6 KB
/
Copy pathapi.py
File metadata and controls
433 lines (378 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
"""REST API for Mindbaboon.
All endpoints require an API key in the `X-API-Key` header matching the
`MINDBABOON_API_KEY` environment variable. Returns JSON only.
"""
from functools import wraps
from datetime import datetime
import os
import socket
import secrets
import logging
from flask import Blueprint, request, jsonify
from database import (
get_db_connection,
get_iteration_slot,
set_iteration_slot,
get_setting,
set_setting,
)
from config import ITERATION_INTERVALS, VERSION
from scheduler import (
scheduler,
schedule_reminder,
remove_reminder,
get_next_run_for_goal,
reschedule_all_active,
)
WEEKDAY_NAMES = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
logger = logging.getLogger(__name__)
api_bp = Blueprint("api", __name__, url_prefix="/api")
VALID_ITERATIONS = set(ITERATION_INTERVALS.keys())
VALID_TIME_SPANS = {"weeks", "months", "specific_date"}
REQUIRED_GOAL_FIELDS = ("goal_name", "goal_description", "time_span",
"iteration", "next_steps", "reward")
def _validate_goal_payload(data):
"""Return error dict or None. All six user-facing fields must be present
and non-empty; time_span=specific_date requires end_date (YYYY-MM-DD)."""
if not isinstance(data, dict):
return {"error": "Body must be a JSON object"}
missing = [f for f in REQUIRED_GOAL_FIELDS
if not str(data.get(f, "")).strip()]
if missing:
return {"error": "missing required fields",
"missing": missing,
"required": list(REQUIRED_GOAL_FIELDS)}
if data["time_span"] not in VALID_TIME_SPANS:
return {"error": f"time_span must be one of {sorted(VALID_TIME_SPANS)}"}
if data["iteration"] not in VALID_ITERATIONS:
return {"error": f"iteration must be one of {sorted(VALID_ITERATIONS)}"}
if data["time_span"] == "specific_date":
ed = str(data.get("end_date", "")).strip()
try:
datetime.strptime(ed, "%Y-%m-%d")
except ValueError:
return {"error": "end_date (YYYY-MM-DD) is required when time_span=specific_date"}
return None
def require_api_key(view):
@wraps(view)
def wrapper(*args, **kwargs):
expected = os.getenv("MINDBABOON_API_KEY")
if not expected:
return jsonify({"error": "API disabled: MINDBABOON_API_KEY not set"}), 503
provided = request.headers.get("X-API-Key", "")
if not secrets.compare_digest(provided, expected):
return jsonify({"error": "Unauthorized"}), 401
return view(*args, **kwargs)
return wrapper
def goal_to_dict(row):
d = dict(row)
d["next_run"] = get_next_run_for_goal(d["id"])
return d
@api_bp.route("/health", methods=["GET"])
def health():
conn = get_db_connection()
try:
goal_count = conn.execute("SELECT COUNT(*) AS c FROM goals").fetchone()["c"]
active_count = conn.execute(
"SELECT COUNT(*) AS c FROM goals WHERE completed = 0"
).fetchone()["c"]
job_rows = conn.execute(
"SELECT id, next_run_time FROM apscheduler_jobs ORDER BY next_run_time ASC"
).fetchall()
finally:
conn.close()
jobs = [
{
"id": r["id"],
"next_run_time": datetime.fromtimestamp(r["next_run_time"]).isoformat()
if r["next_run_time"]
else None,
}
for r in job_rows
]
return jsonify(
{
"status": "ok",
"version": VERSION,
"pid": os.getpid(),
"hostname": socket.gethostname(),
"scheduler_running": scheduler.running,
"goals_total": goal_count,
"goals_active": active_count,
"scheduled_jobs": len(jobs),
"jobs": jobs,
"valid_iterations": sorted(VALID_ITERATIONS),
}
)
@api_bp.route("/goals", methods=["GET"])
@require_api_key
def list_goals():
include_completed = request.args.get("include_completed", "false").lower() == "true"
conn = get_db_connection()
try:
if include_completed:
rows = conn.execute("SELECT * FROM goals ORDER BY created_at DESC").fetchall()
else:
rows = conn.execute(
"SELECT * FROM goals WHERE completed = 0 ORDER BY created_at DESC"
).fetchall()
finally:
conn.close()
return jsonify([goal_to_dict(r) for r in rows])
@api_bp.route("/goals/<int:goal_id>", methods=["GET"])
@require_api_key
def get_goal(goal_id):
conn = get_db_connection()
try:
row = conn.execute("SELECT * FROM goals WHERE id = ?", (goal_id,)).fetchone()
finally:
conn.close()
if not row:
return jsonify({"error": "Goal not found"}), 404
return jsonify(goal_to_dict(row))
@api_bp.route("/goals", methods=["POST"])
@require_api_key
def create_goal():
data = request.get_json(silent=True) or {}
err = _validate_goal_payload(data)
if err:
return jsonify(err), 400
goal_name = data["goal_name"].strip()
iteration = data["iteration"]
goal_description = data["goal_description"]
time_span = data["time_span"]
end_date = data["end_date"] if time_span == "specific_date" else None
next_steps = data["next_steps"]
reward = data["reward"]
conn = get_db_connection()
try:
cur = conn.cursor()
cur.execute(
"""
INSERT INTO goals (
goal_name, goal_description, time_span, end_date,
iteration, next_steps, reward, completed, created_at, last_reminder_at
) VALUES (?, ?, ?, ?, ?, ?, ?, 0, ?, NULL)
""",
(
goal_name,
goal_description,
time_span,
end_date,
iteration,
next_steps,
reward,
datetime.now(),
),
)
conn.commit()
goal_id = cur.lastrowid
row = conn.execute("SELECT * FROM goals WHERE id = ?", (goal_id,)).fetchone()
finally:
conn.close()
schedule_reminder(goal_id, iteration)
return jsonify(goal_to_dict(row)), 201
@api_bp.route("/goals/<int:goal_id>", methods=["PATCH", "PUT"])
@require_api_key
def update_goal(goal_id):
"""Full-form update. All six fields required, same as creating a goal —
so nothing silently drops. For state toggles use the /snooze, /resume,
/complete endpoints instead."""
data = request.get_json(silent=True) or {}
err = _validate_goal_payload(data)
if err:
return jsonify(err), 400
conn = get_db_connection()
try:
row = conn.execute("SELECT * FROM goals WHERE id = ?", (goal_id,)).fetchone()
if not row:
return jsonify({"error": "Goal not found"}), 404
end_date = data["end_date"] if data["time_span"] == "specific_date" else None
iteration_changed = data["iteration"] != row["iteration"]
conn.execute(
"""
UPDATE goals SET
goal_name = ?, goal_description = ?, time_span = ?, end_date = ?,
iteration = ?, next_steps = ?, reward = ?
WHERE id = ?
""",
(
data["goal_name"].strip(),
data["goal_description"],
data["time_span"],
end_date,
data["iteration"],
data["next_steps"],
data["reward"],
goal_id,
),
)
conn.commit()
row = conn.execute("SELECT * FROM goals WHERE id = ?", (goal_id,)).fetchone()
finally:
conn.close()
if iteration_changed:
remove_reminder(goal_id)
schedule_reminder(goal_id, data["iteration"], send_confirmation=False)
return jsonify(goal_to_dict(row))
@api_bp.route("/goals/<int:goal_id>", methods=["DELETE"])
@require_api_key
def delete_goal(goal_id):
conn = get_db_connection()
try:
row = conn.execute("SELECT id FROM goals WHERE id = ?", (goal_id,)).fetchone()
if not row:
return jsonify({"error": "Goal not found"}), 404
# FK ON DELETE CASCADE removes child rows from goal_history and
# iteration_history; see database.py for the pragma.
conn.execute("DELETE FROM goals WHERE id = ?", (goal_id,))
conn.commit()
finally:
conn.close()
remove_reminder(goal_id)
return jsonify({"deleted": goal_id})
@api_bp.route("/goals/<int:goal_id>/complete", methods=["POST"])
@require_api_key
def complete_goal(goal_id):
data = request.get_json(silent=True) or {}
was_done = data.get("was_done", "")
next_steps = data.get("next_steps")
reward = data.get("reward")
mark_done = bool(data.get("mark_done", False))
conn = get_db_connection()
try:
row = conn.execute("SELECT * FROM goals WHERE id = ?", (goal_id,)).fetchone()
if not row:
return jsonify({"error": "Goal not found"}), 404
new_next_steps = next_steps if next_steps is not None else row["next_steps"]
new_reward = reward if reward is not None else row["reward"]
conn.execute(
"""
INSERT INTO iteration_history (iteration_id, status, updated_at)
VALUES (?, ?, ?)
""",
(goal_id, "Yes", datetime.now()),
)
conn.execute(
"""
INSERT INTO goal_history (goal_id, completed, was_done, next_steps, reward, timestamp)
VALUES (?, ?, ?, ?, ?, ?)
""",
(goal_id, "yes", was_done, new_next_steps, new_reward, datetime.now()),
)
conn.execute(
"""
UPDATE goals SET next_steps = ?, reward = ?, completed = ?, is_silenced = 0
WHERE id = ?
""",
(new_next_steps, new_reward, 1 if mark_done else row["completed"], goal_id),
)
conn.commit()
finally:
conn.close()
if mark_done:
remove_reminder(goal_id)
return jsonify({"ok": True, "goal_id": goal_id, "marked_done": mark_done})
@api_bp.route("/goals/<int:goal_id>/snooze", methods=["POST"])
@require_api_key
def snooze_goal(goal_id):
conn = get_db_connection()
try:
row = conn.execute("SELECT id FROM goals WHERE id = ?", (goal_id,)).fetchone()
if not row:
return jsonify({"error": "Goal not found"}), 404
conn.execute("UPDATE goals SET is_silenced = 1 WHERE id = ?", (goal_id,))
conn.commit()
finally:
conn.close()
return jsonify({"ok": True, "goal_id": goal_id, "silenced": True})
@api_bp.route("/goals/<int:goal_id>/resume", methods=["POST"])
@require_api_key
def resume_goal(goal_id):
conn = get_db_connection()
try:
row = conn.execute(
"SELECT iteration FROM goals WHERE id = ?", (goal_id,)
).fetchone()
if not row:
return jsonify({"error": "Goal not found"}), 404
conn.execute("UPDATE goals SET is_silenced = 0 WHERE id = ?", (goal_id,))
conn.commit()
iteration = row["iteration"]
finally:
conn.close()
if iteration:
schedule_reminder(goal_id, iteration)
return jsonify({"ok": True, "goal_id": goal_id, "silenced": False})
@api_bp.route("/settings", methods=["GET"])
@require_api_key
def get_settings():
slot = get_iteration_slot()
return jsonify(
{
"iteration_slot": {
**slot,
"weekday_name": WEEKDAY_NAMES[slot["weekday"]],
},
"default_email": get_setting("default_email"),
}
)
@api_bp.route("/settings", methods=["PATCH"])
@require_api_key
def update_settings():
data = request.get_json(silent=True) or {}
changed_slot = False
slot_update = data.get("iteration_slot") or {}
if slot_update:
wd = slot_update.get("weekday")
hr = slot_update.get("hour")
mn = slot_update.get("minute")
if wd is not None and not (0 <= int(wd) <= 6):
return jsonify({"error": "weekday must be 0..6 (0=Monday)"}), 400
if hr is not None and not (0 <= int(hr) <= 23):
return jsonify({"error": "hour must be 0..23"}), 400
if mn is not None and not (0 <= int(mn) <= 59):
return jsonify({"error": "minute must be 0..59"}), 400
set_iteration_slot(weekday=wd, hour=hr, minute=mn)
changed_slot = True
if "default_email" in data and data["default_email"]:
set_setting("default_email", data["default_email"])
import os as _os
_os.environ["DEFAULT_TO_ADDRESS"] = data["default_email"]
rescheduled = []
if changed_slot:
rescheduled = reschedule_all_active()
slot = get_iteration_slot()
return jsonify(
{
"iteration_slot": {**slot, "weekday_name": WEEKDAY_NAMES[slot["weekday"]]},
"rescheduled_goals": [{"goal_id": gid, "next_run": nxt} for gid, nxt in rescheduled],
}
)
@api_bp.route("/goals/<int:goal_id>/history", methods=["GET"])
@require_api_key
def goal_history(goal_id):
conn = get_db_connection()
try:
goal_rows = conn.execute(
"""
SELECT completed, was_done, next_steps, reward, timestamp
FROM goal_history WHERE goal_id = ? ORDER BY timestamp DESC
""",
(goal_id,),
).fetchall()
iter_rows = conn.execute(
"""
SELECT status, next_run, updated_at FROM iteration_history
WHERE iteration_id = ? ORDER BY updated_at DESC
""",
(goal_id,),
).fetchall()
finally:
conn.close()
return jsonify(
{
"goal_history": [dict(r) for r in goal_rows],
"iteration_history": [dict(r) for r in iter_rows],
}
)