Moves product stat view logic to serializer

- Split frontend into multiple files
- Bump frontend deps
- Remove unused stuff
This commit is contained in:
nikolark committed 2015-11-28 21:21:58 +01:00
1 parent 3b7980ffe9
commit 195c49b0fd
24 files changed
+555 -599

No files matched your search

+35 -1
View File
@@ -1,7 +1,41 @@
from main.models import Product
from django.db.models import Sum
from rest_framework import serializers
from rest_framework.serializers import ModelSerializer
from main.models import Product, OrderLine
class ProductSerializer(ModelSerializer):
class Meta:
model = Product
class ProductStatSerializer(ModelSerializer):
EXTERNAL_USER = 'Ekstern'
counts = serializers.SerializerMethodField()
def _format_counts(self, per_user):
per_user = list(per_user)
for row in per_user:
first = row.pop('order__customer__first_name')
last = row.pop('order__customer__last_name')
if first is None:
who = self.EXTERNAL_USER
else:
who = '{} {}'.format(first, last)
row['full_name'] = who
return per_user
def get_counts(self, obj):
per_user = OrderLine.objects\
.filter(product=obj)\
.values('order__customer__first_name', 'order__customer__last_name')\
.annotate(amount_total=Sum('amount'))\
.order_by('-amount_total')
return self._format_counts(per_user)
class Meta:
model = Product
fields = ['id', 'name', 'description', 'image', 'inventory_amount', 'active', 'created', 'counts']
+12 -3
View File
@@ -1,9 +1,18 @@
from main.api.serializers import ProductSerializer
from main.models import Product
from rest_framework.decorators import list_route
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from main.api.serializers import ProductSerializer, ProductStatSerializer
from main.models import Product
class ProductViewSet(ModelViewSet):
# TODO: auth
serializer_class = ProductSerializer
queryset = Product.objects.all()
@list_route(methods=['get'])
def group_by_user(self, request):
products = self.get_queryset().filter(active=True)
serializer = ProductStatSerializer(products, many=True)
return Response(serializer.data)
+1 -7
View File
@@ -1,7 +1,6 @@
from django.conf.urls import url
from main.api.views import ProductViewSet
from main.views.stats import stats_list, stats_orders, stats_orders_hourly, stats_products_realtime, stats_products, \
stats_products_per_user, products_json
from main.views.stats import stats_list, stats_orders, stats_orders_hourly, stats_products_realtime, stats_products_per_user
from main.views.general import register, deposit, log
from main.views.reports import inventory, inventory_add, report
from main.views.users import create_user, profile
@@ -18,12 +17,7 @@ urlpatterns = [
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'^stats/products/by_user/(?P<user_id>[0-9]+)/$', stats_products_per_user, name='stats-products-per-user'),
# Products
url(r'^products/(?P<product_id>[0-9]+)/$', products_json, name='json-product'),
url(r'^products/$', products_json, name='json-products'),
# Users
url(r'^user/create/$', create_user, name='create-user'),
url(r'^profile/$', profile, name='profile'),
-76
View File
@@ -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\