minor update

This commit is contained in:
2025-08-26 15:20:44 +05:30
parent 7fbab77cda
commit c1ab295514
10 changed files with 151 additions and 2 deletions
+47 -1
View File
@@ -3,10 +3,11 @@ from django.contrib.auth import authenticate, login
from django.contrib.auth.decorators import login_required, user_passes_test
from django.contrib.auth.forms import AuthenticationForm
from django.db import IntegrityError
from django.db.models import Exists, OuterRef
from django.db.models import Exists, OuterRef, Count
from django.http import JsonResponse
from django.shortcuts import render, redirect, get_object_or_404
from django.utils import timezone
from django.utils.timezone import now, timedelta
from django.views.decorators.http import require_POST
from .models import Issue, User, Vote, Comment, Department
from .forms import CitizenRegistrationForm, IssueForm, CommentForm
@@ -337,6 +338,51 @@ def delete_fake_issue(request, issue_id):
messages.success(request, f"✅ Issue deleted and user {reporter.username} has been banned for 7 days.")
return redirect("manage_issues")
@login_required
@user_passes_test(superadmin_check)
def superadmin_reports(request):
# 1. Total issues reported
total_issues = Issue.objects.count()
# 2. Issues per status
status_counts = (
Issue.objects.values('status')
.annotate(count=Count('id'))
.order_by()
)
# 3. Top 5 departments with most assigned issues
top_departments = (
Issue.objects.values('department__name')
.annotate(count=Count('id'))
.order_by('-count')[:5]
)
# 4. Top citizens by number of reports
top_citizens = (
Issue.objects.values('reporter__username')
.annotate(count=Count('id'))
.order_by('-count')[:5]
)
# 5. Issues over time (last 30 days)
last_30_days = now() - timedelta(days=30)
issues_last_30_days = (
Issue.objects.filter(created_at__gte=last_30_days)
.extra(select={'day': "date(created_at)"})
.values('day')
.annotate(count=Count('id'))
.order_by('day')
)
context = {
"total_issues": total_issues,
"status_counts": list(status_counts),
"top_departments": list(top_departments),
"top_citizens": list(top_citizens),
"issues_last_30_days": list(issues_last_30_days),
}
return render(request, "core/superadmin_reports.html", context)
def resolver_check(user):
return user.is_resolver