Added product sales updating in realtime

This commit is contained in:
Jonathan Lillesæter committed 2012-05-20 20:08:06 +02:00
1 parent 9854a17f4e
commit e0936bb64e
6 files changed
+159 -7

No files matched your search

+3 -3
View File
@@ -36,10 +36,10 @@ class OrderLine(models.Model):
unit_price = models.FloatField()
def __unicode__(self):
return "{1} {0} ({2} kr per)".format(self.product, self.amount, self.unit_price)
return u"{1} {0} ({2} kr per)".format(self.product, self.amount, self.unit_price)
class Meta:
unique_together = ('order', 'product')
unique_together = ('order', 'product')
class Transaction(models.Model):
user = models.ForeignKey(User)
@@ -47,7 +47,7 @@ class Transaction(models.Model):
created = models.DateTimeField(auto_now_add=True)
def __unicode__(self):
return "{0}: {1}: {2}".format(self.id, self.user, self.amount)
return "{0}: {1}: {2}".format(self.id, self.user, self.amount)
class UserProfile(models.Model):
user = models.OneToOneField(User)
+2
View File
@@ -8,8 +8,10 @@ urlpatterns = patterns('main.views',
url(r'^stats/$', 'stats', name='stats'),
url(r'^stats/orders/$', 'stats_orders', name='stats-orders'),
url(r'^stats/orders/hourly$', 'stats_orders_hourly', name='stats-orders-hourly'),
url(r'^stats/orders/products_realtime$', 'stats_products_realtime', name='stats-products-realtime'),
url(r'^stats/products/$', 'stats_products', name='stats-products'),
url(r'^stats/products/(?P<product>[0-9]+)/$', 'stats_products', name='stats-product'),
url(r'^products/(?P<product_id>[0-9]+)/$', 'products', name='json-product'),
url(r'^products/$', 'products', name='json-products'),
url("", include('django_socketio.urls'))
)
+28 -2
View File
@@ -3,6 +3,7 @@ from datetime import datetime
from datetime import timedelta
from itertools import groupby
import simplejson as json
import time
from django.contrib.auth import login, logout as auth_logout
from django.contrib.auth.decorators import login_required
@@ -171,7 +172,7 @@ def stats_product(product):
per_user = sales.values('order__customer__first_name','order__customer__last_name').annotate(num=Sum('amount')).order_by('num').reverse()
f_per_user = []
for row in per_user:
who = row['order__customer__first_name'] + " " + row['order__customer__last_name'][0]
who = row['order__customer__first_name'] + " " + row['order__customer__last_name']
num = row['num']
f_per_user.append([who, num])
response = {
@@ -206,7 +207,7 @@ def stats_orders(request):
per_user = Order.objects.values('customer__first_name','customer__last_name').annotate(num=Count('customer')).order_by('num').reverse()
f_per_user = []
for row in per_user:
who = row['customer__first_name'] + " " + row['customer__last_name'][0]
who = row['customer__first_name'] + " " + row['customer__last_name']
num = row['num']
f_per_user.append([who, num])
return HttpResponse(json.dumps(f_per_user), content_type='application/javascript; charset=utf8')
@@ -225,6 +226,31 @@ def stats_orders_hourly(request):
'total' : sum(f_hourly.values()),
}
return HttpResponse(json.dumps(response), content_type='application/javascript; charset=utf8')
def stats_products_realtime(request):
end_time = datetime.now()
start_time = datetime.now() - timedelta(hours=24)
order_lines = OrderLine.objects.filter(order__created__range=(start_time, end_time)).order_by("order__created")
products = {}
for order_line in order_lines:
order_time_in_milliseconds = int(time.mktime(order_line.order.created.timetuple()) * 1000)
if order_line.product not in products:
products[order_line.product] = [[order_time_in_milliseconds, order_line.amount]]
else:
prev_entry = products[order_line.product][-1]
products[order_line.product].append([order_time_in_milliseconds, prev_entry[1] + order_line.amount])
serialized_products = {}
for product, value in products.iteritems():
serialized_products[product.name] = value
response = {
'products': serialized_products
}
return HttpResponse(json.dumps(response), content_type='application/javascript; charset=utf8')
def logout(request):
auth_logout(request)