File size: 3,195 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
67
68
69
70
71
# src/routes/amendment.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.amendment import Amendment
from src.models.legislation import Legislation # Needed for linking amendments

amendment_bp = Blueprint("amendment", __name__, url_prefix="/amendments", template_folder="../templates/amendments")

@amendment_bp.route("/")
@login_required
def list_amendments():
    """Lists all proposed amendments."""
    # Query amendments, potentially filter by status or legislation later
    amendments = Amendment.query.order_by(Amendment.last_updated.desc()).all()
    return render_template("list_amendments.html", amendments=amendments)

@amendment_bp.route("/propose", methods=["GET", "POST"])
@login_required
def propose_amendment():
    """Handles proposing a new amendment for existing legislation."""
    if request.method == "POST":
        legislation_id = request.form.get("legislation_id")
        proposed_changes = request.form.get("proposed_changes")
        rationale = request.form.get("rationale")

        if not legislation_id or not proposed_changes:
            flash("Legislation ID and Proposed Changes are required.", "warning")
            # Re-render form, passing back existing legislation list
            legislations = Legislation.query.filter_by(status="Active").order_by(Legislation.title).all()
            return render_template("propose_amendment.html", legislations=legislations, legislation_id=legislation_id, proposed_changes=proposed_changes, rationale=rationale)

        # Get user ID from session
        author_id = session.get("user_id")

        new_amendment = Amendment(
            legislation_id=int(legislation_id),
            proposed_changes=proposed_changes,
            rationale=rationale,
            author_id=author_id
        )
        
        # Placeholder: In Step 10, we could call Groq here to analyze the proposed change or suggest wording
        # analysis_result = call_groq_to_analyze_amendment(original_text, proposed_changes)

        try:
            db.session.add(new_amendment)
            db.session.commit()
            flash(f"Amendment proposed successfully for Legislation ID {legislation_id}.", "success")
            return redirect(url_for("amendment.view_amendment", amendment_id=new_amendment.id))
        except Exception as e:
            db.session.rollback()
            flash(f"Error proposing amendment: {e}", "danger")

    # GET request: Show the form
    # Only allow amending active legislation
    legislations = Legislation.query.filter_by(status="Active").order_by(Legislation.title).all()
    return render_template("propose_amendment.html", legislations=legislations)

@amendment_bp.route("/<int:amendment_id>")
@login_required
def view_amendment(amendment_id):
    """Displays a single amendment for viewing."""
    amendment = Amendment.query.get_or_404(amendment_id)
    # Add logic for review/approval later if needed
    return render_template("view_amendment.html", amendment=amendment)

# Add routes for updating status (review, approve, reject) later if required