views.py 2.25 KB
Newer Older
1 2 3
# standard library imports
from collections import Counter
# core django imports
4
from django.views.generic import TemplateView
5
from django.db.models import Sum, Count
6
# third party imports
7
from braces.views import LoginRequiredMixin
8
# imports from your apps
Daniel W Bond's avatar
Daniel W Bond committed
9
from lookouts.models import Lookout
10 11
from trades.models import Listing, Bid
from core.models import Student
Daniel W Bond's avatar
Daniel W Bond committed
12

Daniel W Bond's avatar
Daniel W Bond committed
13

Daniel W Bond's avatar
Daniel W Bond committed
14 15 16 17 18
class HomepageView(TemplateView):
    template_name = 'index.html'

    def get_context_data(self, **kwargs):
        context = super(HomepageView, self).get_context_data(**kwargs)
Daniel W Bond's avatar
Daniel W Bond committed
19
        if self.request.user.is_authenticated():
Daniel W Bond's avatar
Daniel W Bond committed
20
            context['lookouts'] = Lookout.objects.filter(owner=self.request.user.student)
Daniel W Bond's avatar
Daniel W Bond committed
21
        return context
22

Daniel W Bond's avatar
Daniel W Bond committed
23

24
class ChartsView(LoginRequiredMixin, TemplateView):
25
    template_name = 'charts.html'
26
    login_url = 'login'
27 28 29 30 31 32 33 34 35 36 37

    def get_context_data(self, **kwargs):
        context = super(ChartsView, self).get_context_data(**kwargs)

        all_listings = Listing.objects.exclude(cancelled=True)

        all_isbns = [listing.isbn for listing in all_listings]

        grossing = []
        # set to eliminate duplicates
        for isbn in set(all_isbns):
38 39
            # only want exchanged listings (not cancelled is assumed)
            listings = Listing.objects.exclude(exchanged=False).filter(isbn=isbn)
Daniel W Bond's avatar
Daniel W Bond committed
40
            # make list of all of that listing's final prices (assume no Nones)
41 42 43 44
            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
Daniel W Bond's avatar
Daniel W Bond committed
45 46 47
            grossing.append((isbn, listing_gross))

        total_proceeds = Listing.objects.aggregate(sum_price=Sum('winning_bid__price'))['sum_price']
48 49 50

        context['most_popular'] = Counter(all_isbns).most_common(20)
        # sort by the second element of the tuple, descending
Daniel W Bond's avatar
Daniel W Bond committed
51 52
        context['highest_grossing'] = sorted(grossing, key=lambda li: li[1],
                                             reverse=True)[:20]
53 54 55
        context['total_listings'] = all_listings.count()
        context['total_bids'] = Bid.objects.count()
        context['total_students'] = Student.objects.count()
Daniel W Bond's avatar
Daniel W Bond committed
56
        context['total_proceeds'] = total_proceeds
57
        return context