File size: 3,097 Bytes
0a40ab8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# src/routes/recommendation.py

from flask import Blueprint, render_template, request, redirect, url_for, flash, session
from src.extensions import db
from src.decorators import login_required
from src.models.recommendation import Recommendation
from src.models.legislation import Legislation # For linking recommendations
from src.utils.groq_client import generate_recommendation # Import Groq utility

recommendation_bp = Blueprint("recommendation", __name__, url_prefix="/recommendations", template_folder="../templates/recommendations")

@recommendation_bp.route("/")
@login_required
def list_recommendations():
    """Lists all generated recommendations."""
    recommendations = Recommendation.query.order_by(Recommendation.generated_at.desc()).all()
    return render_template("list_recommendations.html", recommendations=recommendations)

@recommendation_bp.route("/generate", methods=["GET", "POST"])
@login_required
def generate_recommendations():
    """Form to trigger generation of new recommendations using Groq."""
    if request.method == "POST":
        generation_criteria = request.form.get("generation_criteria", "General legislative review") # Default criteria if none provided
        
        flash(f"Generating recommendation based on criteria: 	'{generation_criteria}' using AI... This may take a moment.", "info")
        
        # Call Groq to generate recommendation
        rec_type, details, rationale, priority = generate_recommendation(generation_criteria)

        if rec_type == "Error" or not details:
            flash(f'AI recommendation generation failed: {details or "No details provided."}', "danger")
            return redirect(url_for("recommendation.generate_recommendations"))

        # Create and save the recommendation
        new_rec = Recommendation(
            recommendation_type=rec_type,
            recommendation_details=details,
            rationale=rationale,
            priority=priority,
            generated_by_user_id=session.get("user_id")
            # target_legislation_id could be added later if AI identifies specific laws
        )
        try:
            db.session.add(new_rec)
            db.session.commit()
            flash("AI successfully generated a new recommendation.", "success")
            return redirect(url_for("recommendation.view_recommendation", recommendation_id=new_rec.id))
        except Exception as e:
            db.session.rollback()
            flash(f"Error saving generated recommendation: {e}", "danger")
            return redirect(url_for("recommendation.list_recommendations"))

    # GET request: Show the form
    return render_template("generate_recommendations.html")

@recommendation_bp.route("/<int:recommendation_id>")
@login_required
def view_recommendation(recommendation_id):
    """Displays the details of a specific recommendation."""
    recommendation = Recommendation.query.get_or_404(recommendation_id)
    return render_template("view_recommendation.html", recommendation=recommendation)

# Add routes for updating status (e.g., reviewed, implemented) later if needed