05/25 Update inspectors contract assignment
This commit is contained in:
+62
-1
@@ -120,9 +120,24 @@ def list_users():
|
||||
.order_by(User.created_at.desc())
|
||||
.all()
|
||||
)
|
||||
|
||||
# Build a map of inspector_id -> assignment count for the Contracts column
|
||||
from app.models.inspector_assignment import InspectorAssignment
|
||||
from sqlalchemy import func
|
||||
rows = (
|
||||
db.session.query(
|
||||
InspectorAssignment.user_id,
|
||||
func.count(InspectorAssignment.id).label('cnt'),
|
||||
)
|
||||
.group_by(InspectorAssignment.user_id)
|
||||
.all()
|
||||
)
|
||||
inspector_contract_counts = {r.user_id: r.cnt for r in rows}
|
||||
|
||||
logger.info('AUTH | list_users | admin=%s | internal_users_count=%s',
|
||||
current_user.username, len(users))
|
||||
return render_template('auth/users.html', users=users)
|
||||
return render_template('auth/users.html', users=users,
|
||||
inspector_contract_counts=inspector_contract_counts)
|
||||
|
||||
|
||||
@bp.route('/users/new', methods=['GET', 'POST'])
|
||||
@@ -194,6 +209,52 @@ def edit_user(user_id):
|
||||
title='Edit User', director_editing=director_editing)
|
||||
|
||||
|
||||
@bp.route('/users/<int:user_id>/assign-contracts', methods=['GET', 'POST'])
|
||||
@login_required
|
||||
@admin_required
|
||||
def assign_inspector_contracts(user_id):
|
||||
user = db.session.get(User, user_id)
|
||||
if user is None or user.role != 'inspector':
|
||||
abort(404)
|
||||
|
||||
from app.models.project import Project
|
||||
from app.models.inspector_assignment import InspectorAssignment
|
||||
from app.utils.time_utils import now_eastern
|
||||
|
||||
projects = Project.query.filter_by(active=True).order_by(Project.name).all()
|
||||
|
||||
if request.method == 'POST':
|
||||
selected_ids = set(request.form.getlist('project_ids', type=int))
|
||||
existing = InspectorAssignment.query.filter_by(user_id=user_id).all()
|
||||
existing_pids = {a.project_id for a in existing}
|
||||
|
||||
for a in existing:
|
||||
if a.project_id not in selected_ids:
|
||||
db.session.delete(a)
|
||||
for pid in selected_ids:
|
||||
if pid not in existing_pids:
|
||||
db.session.add(InspectorAssignment(
|
||||
user_id = user_id,
|
||||
project_id = pid,
|
||||
created_at = now_eastern(),
|
||||
))
|
||||
|
||||
db.session.commit()
|
||||
log_action(ACTION_UPDATE, 'User', user.id, user.username,
|
||||
f'inspector_assignments={sorted(selected_ids)}')
|
||||
flash(f'Contract assignments updated for {user.display_name}.', 'success')
|
||||
return redirect(url_for('auth.list_users'))
|
||||
|
||||
assigned_pids = {
|
||||
a.project_id
|
||||
for a in InspectorAssignment.query.filter_by(user_id=user_id).all()
|
||||
}
|
||||
return render_template('auth/inspector_assignments.html',
|
||||
user=user,
|
||||
projects=projects,
|
||||
assigned_pids=assigned_pids)
|
||||
|
||||
|
||||
@bp.route('/users/<int:user_id>/delete', methods=['POST'])
|
||||
@login_required
|
||||
@admin_required
|
||||
|
||||
Reference in New Issue
Block a user