From 602c6585f8dc552217033e094e142970140a9747 Mon Sep 17 00:00:00 2001 From: MasterofJOKers Date: Thu, 24 Nov 2011 02:23:32 +0100 Subject: [PATCH] Rebuild main view Mostly for testing purposes, but also to unify the usage of buyable in templates. This also fixes the problem of having to make the retrieved lists for usersLast* unique manually. --- .../templates/buyables/product_list.html | 2 +- k4ever/main/templates/main/product_list.html | 16 ++-- k4ever/main/views.py | 73 +++++++++---------- 3 files changed, 43 insertions(+), 48 deletions(-) diff --git a/k4ever/buyable/templates/buyables/product_list.html b/k4ever/buyable/templates/buyables/product_list.html index 0174db7..e4a6405 100644 --- a/k4ever/buyable/templates/buyables/product_list.html +++ b/k4ever/buyable/templates/buyables/product_list.html @@ -6,7 +6,7 @@ {% for buyable in buyables %} - + {{ buyable.name }} diff --git a/k4ever/main/templates/main/product_list.html b/k4ever/main/templates/main/product_list.html index d63e744..ce6e2b6 100644 --- a/k4ever/main/templates/main/product_list.html +++ b/k4ever/main/templates/main/product_list.html @@ -6,18 +6,18 @@ {% for buyable in buyables %} - + - {{ buyable.buyable__name }} {% if buyable.num_buys %}({{ buyable.num_buys }} mal gekauft){% endif %} - - {% if buyable.buyable__deposit > 0 %} - {{ buyable.buyable__price|floatformat:2 }}€ - {{ buyable.buyable__price|floatformat:2 }}€ / {{ buyable.buyable__deposit|floatformat:2 }}€ + {{ buyable.name }} {% if buyable.num_buys %}({{ buyable.num_buys }} mal gekauft){% endif %} + + {% if buyable.deposit > 0 %} + {{ buyable.price|floatformat:2 }}€ + {{ buyable.price|floatformat:2 }}€ / {{ buyable.deposit|floatformat:2 }}€ {% if includeDeposit %} - {{ buyable.buyable__deposit|floatformat:2 }}€ + {{ buyable.deposit|floatformat:2 }}€ {% endif %} {% else %} - {{ buyable.buyable__price|floatformat:2 }}€ + {{ buyable.price|floatformat:2 }}€ {% endif %} diff --git a/k4ever/main/views.py b/k4ever/main/views.py index 73da4df..55ebec5 100644 --- a/k4ever/main/views.py +++ b/k4ever/main/views.py @@ -1,13 +1,15 @@ # -*- coding: utf8 -*- -from django.shortcuts import render_to_response -from django.template import RequestContext +from django.contrib.auth.forms import PasswordChangeForm from django.contrib.auth.decorators import login_required -from django.db.models import Count +from django.db.models import Count, Max from django.http import HttpResponseRedirect -from main.models import Plugin, PluginPermission -from django.contrib.auth.forms import PasswordChangeForm +from django.shortcuts import render_to_response +from django.template import RequestContext + from buyable.models import Purchase, Buyable, BuyableType from main.helper import getUserFromAuthblob +from main.models import Plugin, PluginPermission + @login_required def startpage(request): @@ -16,40 +18,33 @@ def startpage(request): ''' drinks = BuyableType.objects.get(name="Getränk").buyable_set.all() snacks = BuyableType.objects.get(name="Snack").buyable_set.all() - - allMost = Purchase.objects.filter(isDeposit=False).values('buyable__name', 'buyable__id','buyable__image','buyable__price','buyable__deposit') - - allMostDrinks = allMost.filter(buyable__in=drinks).annotate(num_buys=Count('buyable')).order_by('-num_buys') - allMostSnacks = allMost.filter(buyable__in=snacks).annotate(num_buys=Count('buyable')).order_by('-num_buys') - - usersMostDrinks = allMost.filter(buyable__in=drinks).filter(order__user=request.user.id).annotate(num_buys=Count('buyable')).order_by('-num_buys') - usersMostSnacks = allMost.filter(buyable__in=snacks).filter(order__user=request.user.id).annotate(num_buys=Count('buyable')).order_by('-num_buys') - - usersLastDrinks = allMost.filter(buyable__in=drinks).filter(order__user=request.user.id).annotate(num_buys=Count('buyable')).order_by('-order__dateTime') - usersLastSnacks = allMost.filter(buyable__in=snacks).filter(order__user=request.user.id).annotate(num_buys=Count('buyable')).order_by('-order__dateTime') - - usersLastDrinks = allMost.distinct().filter(buyable__in=drinks).filter(order__user=request.user.id).order_by('-order__dateTime') - usersLastSnacks = allMost.distinct().filter(buyable__in=snacks).filter(order__user=request.user.id).order_by('-order__dateTime') - - #if someone knows a better way to do this, just replace this code - #purpose: filter usersLast so that it only contains unique items - #hint: distinct() does not work because dateTime is included due to order_by(), so all items appear distinct - - usersLastDrinks_unique = [] - for x in usersLastDrinks: - if not x in usersLastDrinks_unique: - usersLastDrinks_unique.append(x) - #usersLastDrinks = map(lambda x:{ "buyable__name" : x }, usersLastDrinks_unique) - usersLastDrinks = usersLastDrinks_unique - - usersLastSnacks_unique = [] - for x in usersLastSnacks: - if not x in usersLastSnacks_unique: - usersLastSnacks_unique.append(x) - #usersLastSnacks_unique = usersLastSnacks.distinct() - usersLastSnacks = usersLastSnacks_unique - - return render_to_response("main/startpage.html", {'allMostDrinks' : allMostDrinks[:5], 'allMostSnacks' : allMostSnacks[:5], 'usersMostDrinks': usersMostDrinks[:5], 'usersMostSnacks': usersMostSnacks[:5], 'usersLastDrinks' : usersLastDrinks[:5], 'usersLastSnacks' : usersLastSnacks[:5]}, RequestContext(request)) + + context = {} + + drink_data = (drinks, ('allMostDrinks', 'usersMostDrinks', 'usersLastDrinks')) + snack_data = (snacks, ('allMostSnacks', 'usersMostSnacks', 'usersLastSnacks')) + + for buyables, context_vars in (drink_data, snack_data): + buyables = buyables.values('name', 'id','image','price','deposit') + + # allMost + context[context_vars[0]] = buyables.filter(purchase__isDeposit=False).annotate( + num_buys=Count('purchase')).order_by('-num_buys')[:5] + + # filtert fuer die anderen Variablen vor + buyables = buyables.filter( purchase__order__user=request.user.id, + purchase__isDeposit=False) + buyables = buyables.annotate(num_buys=Count('purchase')) + + # usersMost + context[context_vars[1]] = buyables.order_by('-num_buys')[:5] + + # usersLast + buyables = buyables.annotate(max_dateTime=Max('purchase__order__dateTime')) + context[context_vars[2]] = buyables.order_by('-max_dateTime')[:5] + + return render_to_response("main/startpage.html", context, RequestContext(request)) + def register(request): """ The "no registration available" page... """