From 598e2a8e573746d76015593f08e437b1387db8a2 Mon Sep 17 00:00:00 2001 From: Nikolai Kristiansen Date: Sun, 15 Apr 2012 03:03:21 +0200 Subject: [PATCH] Time for bed --- main/urls.py | 4 +- main/views.py | 50 +++++++++++++------ static/js/stats.js | 115 +++++++++++++++++++++++-------------------- templates/stats.html | 2 +- 4 files changed, 101 insertions(+), 70 deletions(-) diff --git a/main/urls.py b/main/urls.py index 04d6acd..61a7626 100644 --- a/main/urls.py +++ b/main/urls.py @@ -7,5 +7,7 @@ urlpatterns = patterns('main.views', url(r'^log/all$', 'log', kwargs={'limit': None}, name='log-all'), url(r'^stats/$', 'stats', name='stats'), url(r'^stats/orders$', 'stats_orders', name='stats-orders'), - url(r'^stats/products$', 'products', name='stats-products'), + url(r'^stats/product/(?P[0-9]+)$', 'stats_product', name='stats-product'), + url(r'^products/(?P[0-9]+)', 'products', name='json-product'), + url(r'^products/$', 'products', name='json-products'), ) diff --git a/main/views.py b/main/views.py index a8c78c4..919b905 100755 --- a/main/views.py +++ b/main/views.py @@ -139,25 +139,45 @@ def stats(request): return render_to_response('stats.html', locals(), context_instance=RequestContext(request)) -def products(request): - products = Product.objects.filter(active=True) +def serialize_product(product): + f_product = {} + for attr in Product._meta.get_all_field_names(): + if hasattr(product, attr) and product.__getattribute__(attr): + #print type(product.__getattribute__(attr)) + if type(product.__getattribute__(attr)) is datetime: + # TODO parse datetime + date = product.__getattribute__(attr) + f_product[attr] = str(product.__getattribute__(attr)) + else: + f_product[attr] = product.__getattribute__(attr) + return f_product - f_products = [] - for product in products: - f_product = [] - for attr in Product._meta.get_all_field_names(): - if hasattr(product, attr) and product.__getattribute__(attr): - #print type(product.__getattribute__(attr)) - if type(product.__getattribute__(attr)) is datetime: - # TODO parse datetime - date = product.__getattribute__(attr) - f_product.append({attr : str(product.__getattribute__(attr))}) - else: - f_product.append({attr : product.__getattribute__(attr)}) - f_products.append(f_product) +def products(request, product_id=None): + if product_id: + #single + products = get_object_or_404(Product, pk=product_id) + else: + #many + products = Product.objects.filter(active=True) + + if product_id: + f_products = serialize_product(products) + else: + f_products = [serialize_product(product) for product in products] return HttpResponse(json.dumps(f_products), content_type='application/javascript; charset=utf8') +def stats_product(request, product): + sales = OrderLine.objects.filter(product=product) + total = float(sales.count()) + per_user = sales.values('order__customer__first_name','order__customer__last_name').annotate(num=Count('order__customer')).order_by('num') + f_per_user = [] + for row in per_user: + who = row['order__customer__first_name'] + " " + row['order__customer__last_name'][0] + num = row['num'] + f_per_user.append([who, num]) + return HttpResponse(json.dumps(f_per_user), content_type='application/javascript; charset=utf8') + def stats_orders(request): total = float(Order.objects.count()) diff --git a/static/js/stats.js b/static/js/stats.js index ec7f27f..faa8c28 100644 --- a/static/js/stats.js +++ b/static/js/stats.js @@ -1,62 +1,71 @@ -var charts; $(document).ready(function() { - // define the options - var options = { - credits: { - enabled: false - }, - chart: { - renderTo: 'order_chart', - plotBackgroundColor: null, - plotBorderWidth: null, - plotShadow: false - }, - title: { - text: 'Antall ordre per bruker' - }, - tooltip: { - formatter: function() { - return ''+ this.point.name +': '+ Highcharts.numberFormat(this.percentage, 1) +' %'; - } - }, - plotOptions: { - pie: { - allowPointSelect: true, - cursor: 'pointer', - dataLabels: { - enabled: true, - color: '#000000', - connectorColor: '#000000', + var charts = []; + var products_url = '/products'; + + /* drink beer and... */ + $.getJSON(products_url, function(data) { + var graph_mappings = [ + { + 'url' : '/stats/orders', + 'render_to' : 'order_chart' + }]; + var products = data; + for(i in products) { + var mapping = { + 'url' : '/stats/product/' + products[i].id, + 'info_url' : '/products/' + products[i].id, + 'render_to' : 'product_chart' + products[i].id + }; + graph_mappings.push(mapping); + } + for(i in graph_mappings) { + // define the options + var options = { + credits: { + enabled: false + }, + chart: { + /* TODO this is not updated right. + * ie. how to get ajax-callback to update the right options-object? + * */ + renderTo: graph_mappings[i].render_to, + plotBackgroundColor: null, + plotBorderWidth: null, + plotShadow: false + }, + title: { + text: 'Antall ordre per bruker' + }, + tooltip: { formatter: function() { return ''+ this.point.name +': '+ Highcharts.numberFormat(this.percentage, 1) +' %'; } - } - } - }, - series: [{ - type: 'pie', - name: 'Orders', - data: [], - }] -} - var products_url = '/stats/products'; - /* drink beer and */ - $.getJSON(products_url, function(data) { - var graph_mappings = [ - ['/stats/orders', 'order_chart'] - ]; - var products = data; - for(i in products) { - var mapping = ['/stats/product/'+products[i].id, 'product_chart'+products[i].id]; - graph_mappings.push(mapping); - } - /* TODO merere her */ - for(i in graph_mappings) { - $.getJSON(graph_mappings, function(data) { - options.series[0].data = data; - options.chart.renderTo = graph_mappings[i][1]; + }, + plotOptions: { + pie: { + allowPointSelect: true, + cursor: 'pointer', + dataLabels: { + enabled: true, + color: '#000000', + connectorColor: '#000000', + formatter: function() { + return ''+ this.point.name +': '+ Highcharts.numberFormat(this.percentage, 1) +' %'; + } + } + } + }, + series: [{ + type: 'pie', + name: 'Orders', + data: [] + }] + }; + $.getJSON(graph_mappings[i].url, function(data) { + options.series[0].data = data, charts.push(new Highcharts.Chart(options)); }); } + console.log(charts); }); }); diff --git a/templates/stats.html b/templates/stats.html index 3aae54b..96194f8 100644 --- a/templates/stats.html +++ b/templates/stats.html @@ -17,7 +17,7 @@

{{ product }}

-
+