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.
This commit is contained in:
MasterofJOKers 2011-11-24 02:23:32 +01:00
parent 9bad46cf2d
commit 602c6585f8
3 changed files with 42 additions and 47 deletions

View File

@ -6,7 +6,7 @@
{% for buyable in buyables %} {% for buyable in buyables %}
<tr> <tr>
<td class="productImage"> <td class="productImage">
<a href="/store/show/{{ buyable.id }}"><img src="{% thumbnail buyable.image 64x64 %}"/></a> <a href="{% url buyable_show buyable.id %}"><img src="{% thumbnail buyable.image 64x64 %}"/></a>
</td> </td>
<td class="name"><a href="/store/show/{{ buyable.id }}">{{ buyable.name }}</a></td> <td class="name"><a href="/store/show/{{ buyable.id }}">{{ buyable.name }}</a></td>
<td class="actions" data-image="{% thumbnail buyable.image 48x48 %}" data-name="{{ buyable.name }}" data-id="{{ buyable.id }}"> <td class="actions" data-image="{% thumbnail buyable.image 48x48 %}" data-name="{{ buyable.name }}" data-id="{{ buyable.id }}">

View File

@ -6,18 +6,18 @@
{% for buyable in buyables %} {% for buyable in buyables %}
<tr> <tr>
<td class="productImage"> <td class="productImage">
<a href="/store/show/{{ buyable.buyable__id }}"><img src="{% thumbnail buyable.buyable__image 64x64 %}" /></a> <a href="/store/show/{{ buyable.id }}"><img src="{% thumbnail buyable.image 64x64 %}" /></a>
</td> </td>
<td class="name"><span>{{ buyable.buyable__name }}</span> {% if buyable.num_buys %}({{ buyable.num_buys }} mal gekauft){% endif %}</td> <td class="name"><span>{{ buyable.name }}</span> {% if buyable.num_buys %}({{ buyable.num_buys }} mal gekauft){% endif %}</td>
<td class="actions" data-image="{% thumbnail buyable.buyable__image 48x48 %}" data-name="{{ buyable.buyable__name }}" data-id="{{ buyable.buyable__id }}"> <td class="actions" data-image="{% thumbnail buyable.image 48x48 %}" data-name="{{ buyable.name }}" data-id="{{ buyable.id }}">
{% if buyable.buyable__deposit > 0 %} {% if buyable.deposit > 0 %}
<a class="button buy" href="/store/buy/{{ buyable.buyable__id }}" title="Kaufen (Ohne Pfand)"><span><span>{{ buyable.buyable__price|floatformat:2 }}€</span></span></a> <a class="button buy" href="/store/buy/{{ buyable.id }}" title="Kaufen (Ohne Pfand)"><span><span>{{ buyable.price|floatformat:2 }}€</span></span></a>
<a class="button buy inclDeposit" href="/store/buy/{{ buyable.buyable__id }}/with/deposit" title="Kaufen (Mit Pfand)"><span><span>{{ buyable.buyable__price|floatformat:2 }}€ / {{ buyable.buyable__deposit|floatformat:2 }}€</span></span></a> <a class="button buy inclDeposit" href="/store/buy/{{ buyable.id }}/with/deposit" title="Kaufen (Mit Pfand)"><span><span>{{ buyable.price|floatformat:2 }}€ / {{ buyable.deposit|floatformat:2 }}€</span></span></a>
{% if includeDeposit %} {% if includeDeposit %}
<a class="button buy onlyDeposit" href="/store/buy/{{ buyable.buyable__id }}/only/deposit" title="Kaufen (Nur Pfand)"><span><span>{{ buyable.buyable__deposit|floatformat:2 }}€</span></span></a> <a class="button buy onlyDeposit" href="/store/buy/{{ buyable.id }}/only/deposit" title="Kaufen (Nur Pfand)"><span><span>{{ buyable.deposit|floatformat:2 }}€</span></span></a>
{% endif %} {% endif %}
{% else %} {% else %}
<a class="button buy" href="/store/buy/{{ buyable.buyable__id }}" title="Kaufen"><span><span>{{ buyable.buyable__price|floatformat:2 }}€</span></span></a> <a class="button buy" href="/store/buy/{{ buyable.id }}" title="Kaufen"><span><span>{{ buyable.price|floatformat:2 }}€</span></span></a>
{% endif %} {% endif %}
</td> </td>
</tr> </tr>

View File

@ -1,13 +1,15 @@
# -*- coding: utf8 -*- # -*- coding: utf8 -*-
from django.contrib.auth.forms import PasswordChangeForm
from django.contrib.auth.decorators import login_required
from django.db.models import Count, Max
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response from django.shortcuts import render_to_response
from django.template import RequestContext from django.template import RequestContext
from django.contrib.auth.decorators import login_required
from django.db.models import Count
from django.http import HttpResponseRedirect
from main.models import Plugin, PluginPermission
from django.contrib.auth.forms import PasswordChangeForm
from buyable.models import Purchase, Buyable, BuyableType from buyable.models import Purchase, Buyable, BuyableType
from main.helper import getUserFromAuthblob from main.helper import getUserFromAuthblob
from main.models import Plugin, PluginPermission
@login_required @login_required
def startpage(request): def startpage(request):
@ -16,40 +18,33 @@ def startpage(request):
''' '''
drinks = BuyableType.objects.get(name="Getränk").buyable_set.all() drinks = BuyableType.objects.get(name="Getränk").buyable_set.all()
snacks = BuyableType.objects.get(name="Snack").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 = [] context = {}
for x in usersLastDrinks:
if not x in usersLastDrinks_unique: drink_data = (drinks, ('allMostDrinks', 'usersMostDrinks', 'usersLastDrinks'))
usersLastDrinks_unique.append(x) snack_data = (snacks, ('allMostSnacks', 'usersMostSnacks', 'usersLastSnacks'))
#usersLastDrinks = map(lambda x:{ "buyable__name" : x }, usersLastDrinks_unique)
usersLastDrinks = usersLastDrinks_unique for buyables, context_vars in (drink_data, snack_data):
buyables = buyables.values('name', 'id','image','price','deposit')
usersLastSnacks_unique = []
for x in usersLastSnacks: # allMost
if not x in usersLastSnacks_unique: context[context_vars[0]] = buyables.filter(purchase__isDeposit=False).annotate(
usersLastSnacks_unique.append(x) num_buys=Count('purchase')).order_by('-num_buys')[:5]
#usersLastSnacks_unique = usersLastSnacks.distinct()
usersLastSnacks = usersLastSnacks_unique # filtert fuer die anderen Variablen vor
buyables = buyables.filter( purchase__order__user=request.user.id,
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)) 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): def register(request):
""" The "no registration available" page... """ """ The "no registration available" page... """