Commit dab012de authored by Daniel W Bond's avatar Daniel W Bond
Browse files

added (cpu intensive) charts page

parent 48e33740
from django.conf.urls import patterns, include, url from django.conf.urls import patterns, include, url
from .views import HomepageView from .views import HomepageView, ChartsView
from django.views.generic import TemplateView from django.views.generic import TemplateView
from django.contrib import admin from django.contrib import admin
...@@ -22,7 +22,8 @@ urlpatterns = patterns('', ...@@ -22,7 +22,8 @@ urlpatterns = patterns('',
url(r'^search/', include('haystack.urls'), name='search'), url(r'^search/', include('haystack.urls'), name='search'),
### static pages ### ### static pages ###
url(r'^$', HomepageView.as_view(template_name='index.html'), name = 'homepage'), url(r'^$', HomepageView.as_view(), name = 'homepage'),
url(r'^charts/?$', ChartsView.as_view(), name = 'charts'),
url(r'^about/?$', TemplateView.as_view(template_name='about.html'), name='about'), url(r'^about/?$', TemplateView.as_view(template_name='about.html'), name='about'),
url(r'^privacy/?$', TemplateView.as_view(template_name='privacy.html'), name='privacy'), url(r'^privacy/?$', TemplateView.as_view(template_name='privacy.html'), name='privacy'),
url(r'^privacy/opt-out/?$', 'core.views.privacy_opt_out', name='privacy_opt_out'), url(r'^privacy/opt-out/?$', 'core.views.privacy_opt_out', name='privacy_opt_out'),
......
from lookouts.models import Lookout from lookouts.models import Lookout
from trades.models import Listing, Bid
from core.models import Student
from django.views.generic import TemplateView from django.views.generic import TemplateView
from collections import Counter
class HomepageView(TemplateView): class HomepageView(TemplateView):
template_name = 'index.html' template_name = 'index.html'
...@@ -10,3 +14,33 @@ class HomepageView(TemplateView): ...@@ -10,3 +14,33 @@ class HomepageView(TemplateView):
if self.request.user.is_authenticated(): if self.request.user.is_authenticated():
context['lookouts'] = Lookout.objects.filter(owner=self.request.user.student) context['lookouts'] = Lookout.objects.filter(owner=self.request.user.student)
return context return context
class ChartsView(TemplateView):
template_name = 'charts.html'
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):
# only want sold listings (not cancelled is assumed)
listings = Listing.objects.exclude(sold=False).filter(isbn=isbn)
# make a list of all of that listing's final prices (assuming 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['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()
return context
{% extends 'layouts/base.html' %}
{% block title %}
SRCT Bookshare • Charts
{% endblock %}
{% block content %}
{% load humanize %}
{% load trades_extras %}
<div class="page-header" id="banner">
<div class="row">
<div class="col-lg-12 text-center">
<h1><strong>SRCT</strong>BOOKSHARE</h1>
<p class="lead"><strong>Listing Charts</strong></p>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12 text-center">
<p><h2>Featuring <strong>{{ total_bids|intcomma }} bids</strong> on <strong>{{ total_listings|intcomma }} listings</strong> from <strong>{{ total_students|intcomma }} students</strong> (so far).</h2></p>
</div>
</div>
<legend></legend>
<div class="row">
<div class="col-md-6">
<div class="panel panel-primary">
<div class="panel-heading text-center">
<h3><strong>Most Popular</strong></h3>
</div>
<div class="panel-body">
<table class="table table-hover text-center">
<thead>
<tr>
<td class="col-md-8"><h4><strong>Title</strong></h4></td>
<td class="col-md-4"><h4><strong>Listings</strong></h4></td>
</tr>
</thead>
<tbody>
{% for isbn, occurrence in most_popular %}
<tr>
<td><h5><a href="/search/?q={{ isbn }}">{{ isbn|isbn_name|title }}</a></h5></td>
<td><h5>{{ occurrence }}</h5></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
<div class="col-md-6">
<div class="panel panel-primary">
<div class="panel-heading text-center">
<h3><strong>Highest-Grossing</strong></h3>
</div>
<div class="panel-body">
<table class="table table-hover text-center">
<thead>
<tr>
<td class="col-md-8"><h4><strong>Title</strong></h4></td>
<td class="col-md-4"><h4><strong>Proceeds</strong></h4></td>
</tr>
</thead>
<tbody>
{% for isbn, total_sum in highest_grossing %}
<tr>
<td><h5><a href="/search/?q={{ isbn }}">{{ isbn|isbn_name|title }}</a></h5></td>
<td><h5>${{ total_sum }}</h5></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
</div>
{% endblock %}
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