Updated functionalities
This commit is contained in:
+98
-7
@@ -1,7 +1,8 @@
|
||||
import os
|
||||
import logging
|
||||
from app.utils.time_utils import now_eastern
|
||||
from flask import (Blueprint, render_template, redirect, url_for,
|
||||
flash, request, current_app, jsonify)
|
||||
flash, request, current_app, jsonify, abort)
|
||||
from flask_login import login_required, current_user
|
||||
from app import db
|
||||
from app.models.issue import Issue, IssueComment, IssueFollower
|
||||
@@ -21,6 +22,8 @@ from app.utils.sla import sla_status
|
||||
|
||||
bp = Blueprint('issues', __name__, url_prefix='/issues')
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
# ── Shared helper ─────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -65,10 +68,16 @@ def index():
|
||||
severity_filter = request.args.get('severity', '')
|
||||
status_filter = request.args.get('status', '')
|
||||
sla_filter = request.args.get('sla', '')
|
||||
facility_filter = request.args.get('facility_id', '')
|
||||
|
||||
if severity_filter:
|
||||
q = q.filter(Issue.severity == severity_filter)
|
||||
if status_filter:
|
||||
q = q.filter(Issue.status == status_filter)
|
||||
if facility_filter:
|
||||
q = q.join(Area, Issue.area_id == Area.id, isouter=True).filter(
|
||||
Area.facility_id == int(facility_filter)
|
||||
)
|
||||
|
||||
# SLA filter — applied in Python after DB query since SLA is computed
|
||||
issues_paged = q.paginate(page=page, per_page=25, error_out=False)
|
||||
@@ -86,12 +95,29 @@ def index():
|
||||
for f in IssueFollower.query.filter_by(user_id=current_user.id).all()
|
||||
}
|
||||
|
||||
# Facilities for the filter dropdown — scoped for customers, full list otherwise
|
||||
if current_user.role == 'customer':
|
||||
facility_ids = get_customer_scope(current_user) or []
|
||||
facilities = Facility.query.filter(
|
||||
Facility.id.in_(facility_ids), Facility.active == True
|
||||
).order_by(Facility.name).all()
|
||||
else:
|
||||
facilities = Facility.query.filter_by(active=True).order_by(Facility.name).all()
|
||||
|
||||
# Staff for quick-assign dropdown — same roles as the full issue form
|
||||
staff = User.query.filter(
|
||||
User.role.in_(['admin', 'director', 'inspector']), User.active == True
|
||||
).order_by(User.username).all()
|
||||
|
||||
return render_template('issues/list.html',
|
||||
issues=issues_paged,
|
||||
issue_items=filtered_items,
|
||||
severity_filter=severity_filter,
|
||||
status_filter=status_filter,
|
||||
sla_filter=sla_filter,
|
||||
facility_filter=facility_filter,
|
||||
facilities=facilities,
|
||||
staff=staff,
|
||||
followed_ids=followed_ids)
|
||||
|
||||
|
||||
@@ -100,7 +126,9 @@ def index():
|
||||
@bp.route('/<int:issue_id>', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def view(issue_id):
|
||||
issue = Issue.query.get_or_404(issue_id)
|
||||
issue = db.session.get(Issue, issue_id)
|
||||
if issue is None:
|
||||
abort(404)
|
||||
|
||||
if current_user.role == 'inspector' and issue.assigned_to != current_user.id:
|
||||
flash('Access denied. You can only view issues assigned to you.', 'danger')
|
||||
@@ -306,7 +334,9 @@ def view(issue_id):
|
||||
@bp.route('/<int:issue_id>/follow', methods=['POST'])
|
||||
@login_required
|
||||
def follow(issue_id):
|
||||
issue = Issue.query.get_or_404(issue_id)
|
||||
issue = db.session.get(Issue, issue_id)
|
||||
if issue is None:
|
||||
abort(404)
|
||||
if not issue.is_followed_by(current_user):
|
||||
follower = IssueFollower(issue_id=issue.id, user_id=current_user.id)
|
||||
db.session.add(follower)
|
||||
@@ -326,7 +356,9 @@ def follow(issue_id):
|
||||
@bp.route('/<int:issue_id>/unfollow', methods=['POST'])
|
||||
@login_required
|
||||
def unfollow(issue_id):
|
||||
issue = Issue.query.get_or_404(issue_id)
|
||||
issue = db.session.get(Issue, issue_id)
|
||||
if issue is None:
|
||||
abort(404)
|
||||
follower = issue.followers.filter_by(user_id=current_user.id).first()
|
||||
if follower:
|
||||
db.session.delete(follower)
|
||||
@@ -430,7 +462,9 @@ def create():
|
||||
@supervisor_required
|
||||
def verify(issue_id):
|
||||
"""Supervisor sign-off: confirms resolution is satisfactory and closes the issue."""
|
||||
issue = Issue.query.get_or_404(issue_id)
|
||||
issue = db.session.get(Issue, issue_id)
|
||||
if issue is None:
|
||||
abort(404)
|
||||
|
||||
if issue.status not in ('resolved', 'pending_verification'):
|
||||
flash('Only resolved or pending-verification issues can be verified.', 'warning')
|
||||
@@ -461,7 +495,9 @@ def verify(issue_id):
|
||||
@login_required
|
||||
def request_verification(issue_id):
|
||||
"""Inspector/assignee marks the issue as pending director verification."""
|
||||
issue = Issue.query.get_or_404(issue_id)
|
||||
issue = db.session.get(Issue, issue_id)
|
||||
if issue is None:
|
||||
abort(404)
|
||||
|
||||
if current_user.role == 'customer':
|
||||
flash('Access denied.', 'danger')
|
||||
@@ -559,7 +595,9 @@ def delete(issue_id):
|
||||
Restricted to admin and director roles. The deletion is recorded in
|
||||
the audit log before the record is removed so there is always a trace.
|
||||
"""
|
||||
issue = Issue.query.get_or_404(issue_id)
|
||||
issue = db.session.get(Issue, issue_id)
|
||||
if issue is None:
|
||||
abort(404)
|
||||
|
||||
# Snapshot fields needed for logging before deletion
|
||||
issue_id_snap = issue.id
|
||||
@@ -598,3 +636,56 @@ def delete(issue_id):
|
||||
|
||||
flash(f'Issue #{issue_id_snap} has been permanently deleted.', 'success')
|
||||
return redirect(url_for('issues.index'))
|
||||
|
||||
|
||||
# ── Quick-assign (AJAX) ───────────────────────────────────────────────────────
|
||||
|
||||
@bp.route('/<int:issue_id>/quick-assign', methods=['POST'])
|
||||
@login_required
|
||||
def quick_assign(issue_id):
|
||||
"""Inline assignee update from the issues list — returns JSON."""
|
||||
if current_user.role not in ('admin', 'director'):
|
||||
return jsonify({'ok': False, 'error': 'Permission denied'}), 403
|
||||
|
||||
issue = db.session.get(Issue, issue_id)
|
||||
if issue is None:
|
||||
abort(404)
|
||||
data = request.get_json(silent=True) or {}
|
||||
new_user_id = data.get('user_id') # int or None (unassign)
|
||||
|
||||
old_assigned_to = issue.assigned_to
|
||||
|
||||
if new_user_id:
|
||||
user = db.session.get(User, int(new_user_id))
|
||||
if not user:
|
||||
return jsonify({'ok': False, 'error': 'User not found'}), 404
|
||||
issue.assigned_to = user.id
|
||||
label = user.display_name
|
||||
else:
|
||||
issue.assigned_to = None
|
||||
label = '— Unassigned —'
|
||||
|
||||
db.session.commit()
|
||||
|
||||
# Notify new assignee if changed
|
||||
if new_user_id and old_assigned_to != issue.assigned_to:
|
||||
from app.utils.notifications import notify
|
||||
from app.models.notification import EVENT_ISSUE_ASSIGNED
|
||||
notify(
|
||||
recipient = user,
|
||||
title = f'Issue #{issue.id} Assigned to You',
|
||||
body = (f'You have been assigned Issue #{issue.id} '
|
||||
f'({issue.severity} severity) by {current_user.display_name}.'),
|
||||
link = url_for('issues.view', issue_id=issue.id),
|
||||
issue_id = issue.id,
|
||||
event_type = EVENT_ISSUE_ASSIGNED,
|
||||
send_email = True,
|
||||
)
|
||||
|
||||
log_action(ACTION_UPDATE, 'Issue', issue.id,
|
||||
f'#{issue.id}',
|
||||
f'quick-assign: assigned_to={label} by {current_user.username}')
|
||||
logger.info('ISSUE QUICK-ASSIGN | issue_id=%s | assigned_to=%s | by=%s',
|
||||
issue.id, label, current_user.username)
|
||||
|
||||
return jsonify({'ok': True, 'label': label})
|
||||
|
||||
Reference in New Issue
Block a user