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
+62 -31

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'^log/all$', 'log', kwargs={'limit': None}, name='log-all'),
url(r'^stats/$', 'stats', name='stats'), url(r'^stats/$', 'stats', name='stats'),
url(r'^stats/orders$', 'stats_orders', name='stats-orders'), 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'),
) )
+29 -9
View File
@@ -139,25 +139,45 @@ def stats(request):
return render_to_response('stats.html', locals(), context_instance=RequestContext(request)) return render_to_response('stats.html', locals(), context_instance=RequestContext(request))
def products(request): def serialize_product(product):
products = Product.objects.filter(active=True) f_product = {}
f_products = []
for product in products:
f_product = []
for attr in Product._meta.get_all_field_names(): for attr in Product._meta.get_all_field_names():
if hasattr(product, attr) and product.__getattribute__(attr): if hasattr(product, attr) and product.__getattribute__(attr):
#print type(product.__getattribute__(attr)) #print type(product.__getattribute__(attr))
if type(product.__getattribute__(attr)) is datetime: if type(product.__getattribute__(attr)) is datetime:
# TODO parse datetime # TODO parse datetime
date = product.__getattribute__(attr) date = product.__getattribute__(attr)
f_product.append({attr : str(product.__getattribute__(attr))}) f_product[attr] = str(product.__getattribute__(attr))
else: else:
f_product.append({attr : product.__getattribute__(attr)}) f_product[attr] = product.__getattribute__(attr)
f_products.append(f_product) return 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') 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): def stats_orders(request):
total = float(Order.objects.count()) total = float(Order.objects.count())
+29 -20
View File
@@ -1,12 +1,34 @@
var charts;
$(document).ready(function() { $(document).ready(function() {
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 // define the options
var options = { var options = {
credits: { credits: {
enabled: false enabled: false
}, },
chart: { chart: {
renderTo: 'order_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, plotBackgroundColor: null,
plotBorderWidth: null, plotBorderWidth: null,
plotShadow: false plotShadow: false
@@ -36,27 +58,14 @@ $(document).ready(function() {
series: [{ series: [{
type: 'pie', type: 'pie',
name: 'Orders', name: 'Orders',
data: [], data: []
}] }]
} };
var products_url = '/stats/products'; $.getJSON(graph_mappings[i].url, function(data) {
/* drink beer and */ options.series[0].data = data,
$.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];
charts.push(new Highcharts.Chart(options)); 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"> <section id="product_{{ product.id }}" class="row-fluid">
<div class="span12"> <div class="span12">
<h2>{{ product }}</h2> <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 id="container" style="width: 800px; height: 400px; margin: 0 auto;"></div> -->
</div> </div>
</section> </section>