Coverage for src / local_deep_research / research_scheduler / routes.py: 90%

35 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-04-14 23:55 +0000

1""" 

2API routes for document scheduler management. 

3""" 

4 

5from flask import Blueprint, jsonify, session 

6from loguru import logger 

7 

8from ..web.auth.decorators import login_required 

9from .document_scheduler import get_document_scheduler 

10 

11# Create blueprint 

12scheduler_bp = Blueprint("document_scheduler", __name__) 

13 

14# NOTE: Routes use session["username"] (not .get()) intentionally. 

15# @login_required guarantees the key exists; direct access fails fast 

16# if the decorator is ever removed. 

17# Helper functions (not decorated) keep .get() for safety. 

18 

19 

20def get_current_username(): 

21 """Get current username from session.""" 

22 return session.get("username") 

23 

24 

25@scheduler_bp.route("/api/scheduler/status", methods=["GET"]) 

26@login_required 

27def get_scheduler_status(): 

28 """Get the current status of the document scheduler for the current user.""" 

29 try: 

30 username = get_current_username() 

31 if not username: 31 ↛ 32line 31 didn't jump to line 32 because the condition on line 31 was never true

32 return jsonify({"error": "User not authenticated"}), 401 

33 

34 scheduler = get_document_scheduler() 

35 status = scheduler.get_status(username) 

36 return jsonify(status) 

37 except Exception: 

38 logger.exception("Error getting scheduler status") 

39 return jsonify({"error": "Failed to get scheduler status"}), 500 

40 

41 

42@scheduler_bp.route("/api/scheduler/run-now", methods=["POST"]) 

43@login_required 

44def trigger_manual_run(): 

45 """Trigger a manual processing run of the document scheduler for the current user.""" 

46 try: 

47 username = get_current_username() 

48 if not username: 48 ↛ 49line 48 didn't jump to line 49 because the condition on line 48 was never true

49 return jsonify({"error": "User not authenticated"}), 401 

50 

51 scheduler = get_document_scheduler() 

52 success, message = scheduler.trigger_manual_run(username) 

53 

54 if success: 

55 return jsonify({"message": message}) 

56 return jsonify({"error": message}), 400 

57 except Exception: 

58 logger.exception("Error triggering manual run") 

59 return jsonify({"error": "Failed to trigger manual run"}), 500