Commit ea8a1c43 authored by Daniel W Bond's avatar Daniel W Bond

arranged chartsview so keys are added to get_context_data as they are...

arranged chartsview so keys are added to get_context_data as they are calculated for greater clarity, not all at the end
parent 38c61d9a
......@@ -31,27 +31,28 @@ class ChartsView(TemplateView):
all_listings = Listing.objects.exclude(cancelled=True)
all_isbns = [listing.isbn for listing in all_listings]
context['most_popular'] = Counter(all_isbns).most_common(20)
grossing = []
# only want exchanged listings (not cancelled is assumed)
all_exchanged = Listing.objects.exclude(exchanged=False)
# set to eliminate duplicates
for isbn in set(all_isbns):
# only want exchanged listings (not cancelled is assumed)
listings = Listing.objects.exclude(exchanged=False).filter(isbn=isbn)
listings = all_exchanged.filter(isbn=isbn)
# make list of all of that listing's final prices (assume no Nones)
listing_winning_bids = [listing.final_price() for listing in listings]
# add all those together
listing_gross = sum(listing_winning_bids)
# make a tuple of the isbn and gross and add it to the list
grossing.append((isbn, listing_gross))
context['highest_grossing'] = sorted(grossing, key=lambda li: li[1],
reverse=True)[:20]
total_proceeds = Listing.objects.aggregate(sum_price=Sum('winning_bid__price'))['sum_price']
context['total_proceeds'] = total_proceeds
context['most_popular'] = Counter(all_isbns).most_common(20)
# sort by the second element of the tuple, descending
context['highest_grossing'] = sorted(grossing, key=lambda li: li[1],
reverse=True)[:20]
context['total_listings'] = all_listings.count()
context['total_bids'] = Bid.objects.count()
context['total_students'] = Student.objects.count()
context['total_proceeds'] = total_proceeds
return context
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment