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.
master
MasterofJOKers 13 years ago
parent 9bad46cf2d
commit 602c6585f8

@ -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 }}">

@ -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>

@ -1,13 +1,15 @@
# -*- coding: utf8 -*- # -*- coding: utf8 -*-
from django.shortcuts import render_to_response from django.contrib.auth.forms import PasswordChangeForm
from django.template import RequestContext
from django.contrib.auth.decorators import login_required 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 django.http import HttpResponseRedirect
from main.models import Plugin, PluginPermission from django.shortcuts import render_to_response
from django.contrib.auth.forms import PasswordChangeForm from django.template import RequestContext
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') context = {}
allMostDrinks = allMost.filter(buyable__in=drinks).annotate(num_buys=Count('buyable')).order_by('-num_buys') drink_data = (drinks, ('allMostDrinks', 'usersMostDrinks', 'usersLastDrinks'))
allMostSnacks = allMost.filter(buyable__in=snacks).annotate(num_buys=Count('buyable')).order_by('-num_buys') snack_data = (snacks, ('allMostSnacks', 'usersMostSnacks', 'usersLastSnacks'))
usersMostDrinks = allMost.filter(buyable__in=drinks).filter(order__user=request.user.id).annotate(num_buys=Count('buyable')).order_by('-num_buys') for buyables, context_vars in (drink_data, snack_data):
usersMostSnacks = allMost.filter(buyable__in=snacks).filter(order__user=request.user.id).annotate(num_buys=Count('buyable')).order_by('-num_buys') buyables = buyables.values('name', 'id','image','price','deposit')
usersLastDrinks = allMost.filter(buyable__in=drinks).filter(order__user=request.user.id).annotate(num_buys=Count('buyable')).order_by('-order__dateTime') # allMost
usersLastSnacks = allMost.filter(buyable__in=snacks).filter(order__user=request.user.id).annotate(num_buys=Count('buyable')).order_by('-order__dateTime') context[context_vars[0]] = buyables.filter(purchase__isDeposit=False).annotate(
num_buys=Count('purchase')).order_by('-num_buys')[:5]
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') # filtert fuer die anderen Variablen vor
buyables = buyables.filter( purchase__order__user=request.user.id,
#if someone knows a better way to do this, just replace this code purchase__isDeposit=False)
#purpose: filter usersLast so that it only contains unique items buyables = buyables.annotate(num_buys=Count('purchase'))
#hint: distinct() does not work because dateTime is included due to order_by(), so all items appear distinct
# usersMost
usersLastDrinks_unique = [] context[context_vars[1]] = buyables.order_by('-num_buys')[:5]
for x in usersLastDrinks:
if not x in usersLastDrinks_unique: # usersLast
usersLastDrinks_unique.append(x) buyables = buyables.annotate(max_dateTime=Max('purchase__order__dateTime'))
#usersLastDrinks = map(lambda x:{ "buyable__name" : x }, usersLastDrinks_unique) context[context_vars[2]] = buyables.order_by('-max_dateTime')[:5]
usersLastDrinks = usersLastDrinks_unique
return render_to_response("main/startpage.html", context, RequestContext(request))
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))
def register(request): def register(request):
""" The "no registration available" page... """ """ The "no registration available" page... """

Loading…
Cancel
Save