Moves product stat view logic to serializer
- Split frontend into multiple files - Bump frontend deps - Remove unused stuff
This commit is contained in:
1 parent
3b7980ffe9
commit
195c49b0fd
24 files changed
+555
-599
No files matched your search
@@ -16,82 +16,6 @@ def stats_list(request):
|
||||
return render(request, 'stats.html', {'products': products})
|
||||
|
||||
|
||||
def serialize_product(product):
|
||||
f_product = {}
|
||||
for attr in Product._meta.get_all_field_names():
|
||||
if attr in ["transactions", "orderlines", "image"]:
|
||||
continue
|
||||
|
||||
if hasattr(product, attr) and product.__getattribute__(attr):
|
||||
if type(product.__getattribute__(attr)) is datetime:
|
||||
f_product[attr] = str(product.__getattribute__(attr))
|
||||
else:
|
||||
f_product[attr] = product.__getattribute__(attr)
|
||||
return f_product
|
||||
|
||||
|
||||
def products_json(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 JsonResponse(f_products)
|
||||
|
||||
|
||||
def stats_product(product):
|
||||
# reverse lookup via order, customer
|
||||
per_user = OrderLine.objects\
|
||||
.filter(product=product)\
|
||||
.values('order__customer__first_name', 'order__customer__last_name')\
|
||||
.annotate(num=Sum('amount'))\
|
||||
.order_by('-num')
|
||||
f_per_user = []
|
||||
for row in per_user:
|
||||
if row['order__customer__first_name'] is not None:
|
||||
who = row['order__customer__first_name'] + " " + row['order__customer__last_name']
|
||||
else:
|
||||
who = "Ekstern"
|
||||
num = row['num']
|
||||
f_per_user.append([who, num])
|
||||
|
||||
response = {
|
||||
'product': serialize_product(Product.objects.get(pk=product)) if type(product) in six.string_types else serialize_product(product),
|
||||
'counts': f_per_user,
|
||||
'total_counts': sum([row['num'] for row in per_user]),
|
||||
}
|
||||
|
||||
return response
|
||||
|
||||
|
||||
def stats_products(request, product=None):
|
||||
if product:
|
||||
# single
|
||||
f_products = [stats_product(product)]
|
||||
sales = OrderLine.objects.filter(product=product)
|
||||
else:
|
||||
# all
|
||||
sales = OrderLine.objects.filter(product__active=True)
|
||||
f_products = [stats_product(p) for p in Product.objects.filter(active=True)]
|
||||
|
||||
totals = float(sum([sale.amount for sale in sales]))
|
||||
response = {
|
||||
'request': 'all' if not product else product,
|
||||
'response': {
|
||||
'products': f_products,
|
||||
'total_count': totals,
|
||||
}
|
||||
}
|
||||
return JsonResponse(response)
|
||||
|
||||
|
||||
def stats_orders(request):
|
||||
num_external_orders = Order.objects.filter(customer=None).count()
|
||||
per_user = Order.objects\
|
||||
|
||||
Reference in new issue
Block a user