PoliSage / src /routes /amendment.py
yasserrmd's picture
Upload 80 files
0a40ab8 verified
# 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