|
| 1 | +from rest_framework.views import APIView |
| 2 | +from django.db.models import Count |
| 3 | + |
| 4 | +from db.task import TaskReport, KarmaActivityLog |
| 5 | +from utils.response import CustomResponse |
| 6 | +from utils.permission import CustomizePermission, JWTUtils, RoleRequired |
| 7 | +from utils.types import RoleType |
| 8 | +from . import serializers |
| 9 | + |
| 10 | +class TaskReportInfoView(APIView): |
| 11 | + authentication_classes = [CustomizePermission] |
| 12 | + |
| 13 | + @RoleRequired(roles=[RoleType.ADMIN, RoleType.FELLOW]) |
| 14 | + def get(self, request): |
| 15 | + reports = TaskReport.objects.all() |
| 16 | + |
| 17 | + # Filtering |
| 18 | + if status := request.query_params.get("status"): |
| 19 | + reports = reports.filter(status=status) |
| 20 | + |
| 21 | + serializer = serializers.TaskReportSerializer(reports, many=True) |
| 22 | + return CustomResponse(response=serializer.data).get_success_response() |
| 23 | + |
| 24 | + @RoleRequired(roles=[RoleType.ADMIN, RoleType.FELLOW]) |
| 25 | + def put(self, request, report_id): |
| 26 | + report = TaskReport.objects.filter(id=report_id).first() |
| 27 | + if not report: |
| 28 | + return CustomResponse(general_message="Report not found").get_failure_response() |
| 29 | + |
| 30 | + serializer = serializers.TaskReportUpdateSerializer( |
| 31 | + report, data=request.data, context={"user_id": JWTUtils.fetch_user_id(request)} |
| 32 | + ) |
| 33 | + if serializer.is_valid(): |
| 34 | + serializer.save() |
| 35 | + return CustomResponse(general_message="Report status updated successfully").get_success_response() |
| 36 | + return CustomResponse(message=serializer.errors).get_failure_response() |
| 37 | + |
| 38 | + |
| 39 | +class TaskReportTaskGroupingView(APIView): |
| 40 | + authentication_classes = [CustomizePermission] |
| 41 | + |
| 42 | + @RoleRequired(roles=[RoleType.ADMIN, RoleType.FELLOW]) |
| 43 | + def get(self, request): |
| 44 | + # 1) How many people reported to each tasks |
| 45 | + # Group by message_id |
| 46 | + report_counts = ( |
| 47 | + TaskReport.objects.values("message_id") |
| 48 | + .annotate(report_count=Count("id")) |
| 49 | + .order_by("-report_count") |
| 50 | + ) |
| 51 | + |
| 52 | + data = [] |
| 53 | + for item in report_counts: |
| 54 | + res = { |
| 55 | + "message_id": item["message_id"], |
| 56 | + "report_count": item["report_count"], |
| 57 | + "task_hashtag": None |
| 58 | + } |
| 59 | + # Try to fetch hashtag from KarmaActivityLog |
| 60 | + if kal := KarmaActivityLog.objects.filter(task_message_id=item["message_id"]).first(): |
| 61 | + res["task_hashtag"] = kal.task.hashtag |
| 62 | + |
| 63 | + data.append(res) |
| 64 | + |
| 65 | + return CustomResponse(response=data).get_success_response() |
| 66 | + |
| 67 | + |
| 68 | +class TaskReportReporterGroupingView(APIView): |
| 69 | + authentication_classes = [CustomizePermission] |
| 70 | + |
| 71 | + @RoleRequired(roles=[RoleType.ADMIN, RoleType.FELLOW]) |
| 72 | + def get(self, request): |
| 73 | + # Request #2: "How many tasks were reported by a person" - confusing phrasing |
| 74 | + # It could mean "How many reports did User X make" OR "How many times was User Y reported" |
| 75 | + # Let's provide both. |
| 76 | + |
| 77 | + # Reports by Reporter |
| 78 | + reporters = ( |
| 79 | + TaskReport.objects.values("reporter__id", "reporter__fullname") |
| 80 | + .annotate(report_count=Count("id")) |
| 81 | + .order_by("-report_count") |
| 82 | + ) |
| 83 | + |
| 84 | + # Reports against Offender |
| 85 | + offenders = ( |
| 86 | + TaskReport.objects.values("offender__id", "offender__fullname") |
| 87 | + .annotate(report_count=Count("id")) |
| 88 | + .order_by("-report_count") |
| 89 | + ) |
| 90 | + |
| 91 | + return CustomResponse( |
| 92 | + response={"reporters": reporters, "offenders": offenders} |
| 93 | + ).get_success_response() |
0 commit comments