Time for bed

This commit is contained in:
nikolark committed 2012-04-15 03:03:21 +02:00
1 parent d112e1b195
commit 598e2a8e57
4 files changed
+101 -70

No files matched your search

+3 -1
View File
@@ -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<product>[0-9]+)$', 'stats_product', name='stats-product'),
url(r'^products/(?P<product_id>[0-9]+)', 'products', name='json-product'),
url(r'^products/$', 'products', name='json-products'),
)
+35 -15
View File
@@ -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())
+62 -53
View File
@@ -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 '<b>'+ this.point.name +'</b>: '+ 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 '<b>'+ this.point.name +'</b>: '+ 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 '<b>'+ this.point.name +'</b>: '+ 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);
});
});
+1 -1
View File
@@ -17,7 +17,7 @@
<section id="product_{{ product.id }}" class="row-fluid">
<div class="span12">
<h2>{{ product }}</h2>
<div id="product_{{ product.id }}_chart"></div>
<div id="product_chart{{ product.id }}"></div>
<!--<div id="container" style="width: 800px; height: 400px; margin: 0 auto;"></div> -->
</div>
</section>