One more addition.

This commit is contained in:
Christopher Neugebauer 2016-09-20 14:04:38 +10:00
parent bcd7043862
commit 2d469bb398

View file

@ -535,6 +535,7 @@ def attendee_data(request, form, user_id=None):
for profile in profiles: for profile in profiles:
by_user[profile.attendee.user] = profile by_user[profile.attendee.user] = profile
# Group the responses per-field.
for field in fields: for field in fields:
field_verbose = AttendeeProfile._meta.get_field(field).verbose_name field_verbose = AttendeeProfile._meta.get_field(field).verbose_name
@ -544,18 +545,31 @@ def attendee_data(request, form, user_id=None):
product_name = product + "__name" product_name = product + "__name"
category_name = product + "__category__name" category_name = product + "__category__name"
status_count = lambda status: Case(When(
attendee__user__cart__status=status,
then=Value(1),
),
default=Value(0),
output_field=models.fields.IntegerField(),
)
paid_count = status_count(commerce.Cart.STATUS_PAID)
unpaid_count = status_count(commerce.Cart.STATUS_ACTIVE)
p = profiles.order_by(product, field).values( p = profiles.order_by(product, field).values(
cart_status, product, product_name, category_name, field product, product_name, category_name, field
).annotate(count=Count("id")) ).annotate(
paid_count=Sum(paid_count),
unpaid_count=Sum(unpaid_count),
)
output.append(ListReport( output.append(ListReport(
"Grouped by %s" % field_verbose, "Grouped by %s" % field_verbose,
["Product", "Status", field_verbose, "count"], ["Product", field_verbose, "paid", "unpaid"],
[ [
( (
"%s - %s" % (i[category_name], i[product_name]), "%s - %s" % (i[category_name], i[product_name]),
status_display[i[cart_status]],
i[field], i[field],
i["count"] or 0, i["paid_count"] or 0,
i["unpaid_count"] or 0,
) )
for i in p for i in p
], ],