Review

Sortable canning-batch QC board

QC managers want to re-order the canning-batch board by ABV, defect rate, or package time from a dropdown. Threads the sort query param into the existing raw SQL that joins batch rows to QC stats.

DjangoTier 2securitysqldjango

Click a line to flag it, pick one or more labels, then submit. If the change looks correct, approve it.

qc/views.py+7-6
1818def canning_board(request):
1919 plant_id = request.session["plant_id"]
20- batches = Batch.objects.raw(
21- "SELECT b.*, s.defect_rate FROM brew_batch b "
22- "JOIN brew_qc_stats s ON s.batch_id = b.id "
23- "WHERE b.plant_id = %s ORDER BY b.packaged_at DESC",
24- [plant_id],
25- )
20+ sort = request.GET.get("sort", "b.packaged_at")
21+ batches = Batch.objects.raw(
22+ f"SELECT b.*, s.defect_rate FROM brew_batch b "
23+ f"JOIN brew_qc_stats s ON s.batch_id = b.id "
24+ f"WHERE b.plant_id = %s ORDER BY {sort}",
25+ [plant_id],
26+ )
2627 return render(request, "qc/board.html", {"batches": batches})