Enhance question review functionality with latest review status annotation and display in templates
This commit is contained in:
+31
-9
@@ -3199,6 +3199,20 @@ class GenericViewBase:
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# Annotate with latest review status using a DB subquery for efficiency
|
||||
try:
|
||||
from django.db.models import OuterRef, Subquery
|
||||
ct = ContentType.objects.get_for_model(self.question_object)
|
||||
latest_status_subq = (
|
||||
QuestionReview.objects.filter(content_type=ct, object_id=OuterRef("pk"))
|
||||
.order_by("-created_on")
|
||||
.values("status")[:1]
|
||||
)
|
||||
qs = qs.annotate(latest_review_status=Subquery(latest_status_subq))
|
||||
except Exception:
|
||||
# annotation failed — we'll fall back to per-object lookup below
|
||||
pass
|
||||
|
||||
# Optionally filter by review status
|
||||
results = []
|
||||
for question in qs:
|
||||
@@ -3210,19 +3224,15 @@ class GenericViewBase:
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
ct = ContentType.objects.get_for_model(question)
|
||||
latest = (
|
||||
QuestionReview.objects.filter(content_type=ct, object_id=question.pk)
|
||||
.order_by("-created_on").first()
|
||||
)
|
||||
|
||||
# Determine latest status — prefer annotated value if present
|
||||
latest_status = getattr(question, "latest_review_status", None)
|
||||
match = False
|
||||
if status == "ANY":
|
||||
match = True
|
||||
elif status == "UNREVIEWED":
|
||||
match = latest is None
|
||||
match = latest_status is None
|
||||
else:
|
||||
if latest is not None and latest.status == status:
|
||||
if latest_status is not None and latest_status == status:
|
||||
match = True
|
||||
|
||||
if match:
|
||||
@@ -3241,7 +3251,19 @@ class GenericViewBase:
|
||||
|
||||
results.append(question)
|
||||
|
||||
return render(request, f"{self.app_name}/question_review_list.html", {"questions": results, "app_name": self.app_name, "filters": {"category": category, "status": status}})
|
||||
# Provide human-readable labels for statuses to the template
|
||||
status_choices = {code: label for code, label in QuestionReview.StatusChoices.choices}
|
||||
|
||||
return render(
|
||||
request,
|
||||
f"{self.app_name}/question_review_list.html",
|
||||
{
|
||||
"questions": results,
|
||||
"app_name": self.app_name,
|
||||
"filters": {"category": category, "status": status},
|
||||
"status_choices": status_choices,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@method_decorator(user_passes_test(lambda u: u.is_superuser))
|
||||
|
||||
Reference in New Issue
Block a user