Browse Source

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.
MasterofJOKers 10 years ago
parent
commit
602c6585f8

+ 1
- 1
k4ever/buyable/templates/buyables/product_list.html View File

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

+ 8
- 8
k4ever/main/templates/main/product_list.html View File

@@ -6,18 +6,18 @@
6 6
 		{% for buyable in buyables %}
7 7
 			<tr>
8 8
 				<td class="productImage">
9
-					<a href="/store/show/{{ buyable.buyable__id }}"><img src="{% thumbnail buyable.buyable__image 64x64 %}" /></a>
9
+					<a href="/store/show/{{ buyable.id }}"><img src="{% thumbnail buyable.image 64x64 %}" /></a>
10 10
 				</td>
11
-				<td class="name"><span>{{ buyable.buyable__name }}</span> {% if buyable.num_buys %}({{ buyable.num_buys }} mal gekauft){% endif %}</td>
12
-				<td class="actions" data-image="{% thumbnail buyable.buyable__image 48x48 %}" data-name="{{ buyable.buyable__name }}" data-id="{{ buyable.buyable__id }}">
13
-					{% if buyable.buyable__deposit > 0 %}
14
-						<a class="button buy" href="/store/buy/{{ buyable.buyable__id }}" title="Kaufen (Ohne Pfand)"><span><span>{{ buyable.buyable__price|floatformat:2 }}€</span></span></a>
15
-						<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>
11
+				<td class="name"><span>{{ buyable.name }}</span> {% if buyable.num_buys %}({{ buyable.num_buys }} mal gekauft){% endif %}</td>
12
+				<td class="actions" data-image="{% thumbnail buyable.image 48x48 %}" data-name="{{ buyable.name }}" data-id="{{ buyable.id }}">
13
+					{% if buyable.deposit > 0 %}
14
+						<a class="button buy" href="/store/buy/{{ buyable.id }}" title="Kaufen (Ohne Pfand)"><span><span>{{ buyable.price|floatformat:2 }}€</span></span></a>
15
+						<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>
16 16
 						{% if includeDeposit %}
17
-							<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>
17
+							<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>
18 18
 						{% endif %}
19 19
 					{% else %}
20
-						<a class="button buy" href="/store/buy/{{ buyable.buyable__id }}" title="Kaufen"><span><span>{{ buyable.buyable__price|floatformat:2 }}€</span></span></a>
20
+						<a class="button buy" href="/store/buy/{{ buyable.id }}" title="Kaufen"><span><span>{{ buyable.price|floatformat:2 }}€</span></span></a>
21 21
 					{% endif %}
22 22
 				</td>
23 23
 			</tr>

+ 34
- 39
k4ever/main/views.py View File

@@ -1,13 +1,15 @@
1 1
 # -*- coding: utf8 -*-
2
-from django.shortcuts import render_to_response
3
-from django.template import RequestContext
2
+from django.contrib.auth.forms import PasswordChangeForm
4 3
 from django.contrib.auth.decorators import login_required
5
-from django.db.models import Count
4
+from django.db.models import Count, Max
6 5
 from django.http import HttpResponseRedirect
7
-from main.models import Plugin, PluginPermission
8
-from django.contrib.auth.forms import PasswordChangeForm
6
+from django.shortcuts import render_to_response
7
+from django.template import RequestContext
8
+
9 9
 from buyable.models import Purchase, Buyable, BuyableType
10 10
 from main.helper import getUserFromAuthblob
11
+from main.models import Plugin, PluginPermission
12
+
11 13
 
12 14
 @login_required
13 15
 def startpage(request):
@@ -16,40 +18,33 @@ def startpage(request):
16 18
 	'''
17 19
 	drinks = BuyableType.objects.get(name="Getränk").buyable_set.all()
18 20
 	snacks = BuyableType.objects.get(name="Snack").buyable_set.all()
19
-	
20
-	allMost = Purchase.objects.filter(isDeposit=False).values('buyable__name', 'buyable__id','buyable__image','buyable__price','buyable__deposit')
21
-	
22
-	allMostDrinks = allMost.filter(buyable__in=drinks).annotate(num_buys=Count('buyable')).order_by('-num_buys')
23
-	allMostSnacks = allMost.filter(buyable__in=snacks).annotate(num_buys=Count('buyable')).order_by('-num_buys')
24
-	
25
-	usersMostDrinks = allMost.filter(buyable__in=drinks).filter(order__user=request.user.id).annotate(num_buys=Count('buyable')).order_by('-num_buys')
26
-	usersMostSnacks = allMost.filter(buyable__in=snacks).filter(order__user=request.user.id).annotate(num_buys=Count('buyable')).order_by('-num_buys')
27
-	
28
-	usersLastDrinks = allMost.filter(buyable__in=drinks).filter(order__user=request.user.id).annotate(num_buys=Count('buyable')).order_by('-order__dateTime')
29
-	usersLastSnacks = allMost.filter(buyable__in=snacks).filter(order__user=request.user.id).annotate(num_buys=Count('buyable')).order_by('-order__dateTime')
30
-	
31
-	usersLastDrinks = allMost.distinct().filter(buyable__in=drinks).filter(order__user=request.user.id).order_by('-order__dateTime')
32
-	usersLastSnacks = allMost.distinct().filter(buyable__in=snacks).filter(order__user=request.user.id).order_by('-order__dateTime')
33
-	
34
-	#if someone knows a better way to do this, just replace this code
35
-	#purpose: filter usersLast so that it only contains unique items
36
-	#hint: distinct() does not work because dateTime is included due to order_by(), so all items appear distinct
37
-
38
-	usersLastDrinks_unique = []
39
-	for x in usersLastDrinks:
40
-		if not x in usersLastDrinks_unique:
41
-			usersLastDrinks_unique.append(x)
42
-	#usersLastDrinks = map(lambda x:{ "buyable__name" : x }, usersLastDrinks_unique) 
43
-	usersLastDrinks = usersLastDrinks_unique
44
-	
45
-	usersLastSnacks_unique = []
46
-	for x in usersLastSnacks:
47
-		if not x in usersLastSnacks_unique:
48
-			usersLastSnacks_unique.append(x)
49
-	#usersLastSnacks_unique = usersLastSnacks.distinct()
50
-	usersLastSnacks = usersLastSnacks_unique
51
-	
52
-	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))
21
+
22
+	context = {}
23
+
24
+	drink_data = (drinks, ('allMostDrinks', 'usersMostDrinks', 'usersLastDrinks'))
25
+	snack_data = (snacks, ('allMostSnacks', 'usersMostSnacks', 'usersLastSnacks'))
26
+
27
+	for buyables, context_vars in (drink_data, snack_data):
28
+		buyables = buyables.values('name', 'id','image','price','deposit')
29
+
30
+		# allMost
31
+		context[context_vars[0]] = buyables.filter(purchase__isDeposit=False).annotate(
32
+				num_buys=Count('purchase')).order_by('-num_buys')[:5]
33
+
34
+		# filtert fuer die anderen Variablen vor
35
+		buyables = buyables.filter( purchase__order__user=request.user.id,
36
+									purchase__isDeposit=False)
37
+		buyables = buyables.annotate(num_buys=Count('purchase'))
38
+
39
+		# usersMost
40
+		context[context_vars[1]] = buyables.order_by('-num_buys')[:5]
41
+
42
+		# usersLast
43
+		buyables = buyables.annotate(max_dateTime=Max('purchase__order__dateTime'))
44
+		context[context_vars[2]] = buyables.order_by('-max_dateTime')[:5]
45
+
46
+	return render_to_response("main/startpage.html", context, RequestContext(request))
47
+
53 48
 
54 49
 def register(request):
55 50
 	""" The "no registration available" page... """

Loading…
Cancel
Save