06/08 Add Inspectors in Customer's dashboard
This commit is contained in:
@@ -183,10 +183,12 @@ def index():
|
|||||||
Issue.reported_at <= end,
|
Issue.reported_at <= end,
|
||||||
)).group_by(Issue.status).all()
|
)).group_by(Issue.status).all()
|
||||||
|
|
||||||
# Top inspectors by inspection count — inspector sees only their own row
|
# Top inspectors — scoped per role:
|
||||||
top_inspectors = []
|
# customer → inspectors who worked at the customer's assigned facilities
|
||||||
if current_user.role != 'customer':
|
# inspector → own row only
|
||||||
|
# others → org-wide top 10 (filtered by inspector_filter if set)
|
||||||
top_insp_q = db.session.query(
|
top_insp_q = db.session.query(
|
||||||
|
User.full_name,
|
||||||
User.username,
|
User.username,
|
||||||
func.count(Inspection.id).label('count'),
|
func.count(Inspection.id).label('count'),
|
||||||
func.avg(Inspection.overall_score).label('avg_score'),
|
func.avg(Inspection.overall_score).label('avg_score'),
|
||||||
@@ -196,9 +198,8 @@ def index():
|
|||||||
Inspection.inspection_date <= end,
|
Inspection.inspection_date <= end,
|
||||||
Inspection.status == 'completed',
|
Inspection.status == 'completed',
|
||||||
)
|
)
|
||||||
if is_inspector:
|
top_insp_q = _scope_insp(top_insp_q)
|
||||||
top_insp_q = top_insp_q.filter(Inspection.inspector_id == current_user.id)
|
top_inspectors = top_insp_q.group_by(User.id, User.full_name, User.username)\
|
||||||
top_inspectors = top_insp_q.group_by(User.id, User.username)\
|
|
||||||
.order_by(func.count(Inspection.id).desc()).limit(10).all()
|
.order_by(func.count(Inspection.id).desc()).limit(10).all()
|
||||||
|
|
||||||
# Recent issues (critical/high) — scoped for customers
|
# Recent issues (critical/high) — scoped for customers
|
||||||
@@ -224,7 +225,7 @@ def index():
|
|||||||
daily_scores=[{'day': str(r.day), 'avg': round(float(r.avg), 2), 'count': r.count} for r in daily_scores],
|
daily_scores=[{'day': str(r.day), 'avg': round(float(r.avg), 2), 'count': r.count} for r in daily_scores],
|
||||||
issue_severity=[{'severity': r.severity, 'count': r.count} for r in issue_severity],
|
issue_severity=[{'severity': r.severity, 'count': r.count} for r in issue_severity],
|
||||||
issue_status=[{'status': r.status, 'count': r.count} for r in issue_status],
|
issue_status=[{'status': r.status, 'count': r.count} for r in issue_status],
|
||||||
top_inspectors=[{'username': r.username, 'count': r.count, 'avg_score': round(float(r.avg_score), 2) if r.avg_score else None} for r in top_inspectors],
|
top_inspectors=[{'display_name': (r.full_name.strip() if r.full_name and r.full_name.strip() else r.username), 'count': r.count, 'avg_score': round(float(r.avg_score), 2) if r.avg_score else None} for r in top_inspectors],
|
||||||
critical_issues=critical_issues,
|
critical_issues=critical_issues,
|
||||||
is_inspector=is_inspector,
|
is_inspector=is_inspector,
|
||||||
inspectors=inspectors,
|
inspectors=inspectors,
|
||||||
|
|||||||
@@ -126,7 +126,7 @@
|
|||||||
<tbody>
|
<tbody>
|
||||||
{% for row in top_inspectors %}
|
{% for row in top_inspectors %}
|
||||||
<tr>
|
<tr>
|
||||||
<td>{{ row.username }}</td>
|
<td>{{ row.display_name }}</td>
|
||||||
<td>{{ row.count }}</td>
|
<td>{{ row.count }}</td>
|
||||||
<td>
|
<td>
|
||||||
{% if row.avg_score %}
|
{% if row.avg_score %}
|
||||||
|
|||||||
Reference in New Issue
Block a user