From f4ceecf2057a23cff42d05467dac48e94ed750d9 Mon Sep 17 00:00:00 2001 From: seba Date: Tue, 19 Oct 2010 01:03:38 +0200 Subject: [PATCH] Kaufen funktioniert --- k4ever/buyable/models.py | 19 ++++++- .../templates/buyables/itemBought.html | 15 +++++ .../buyable/templates/buyables/showItem.html | 2 +- k4ever/buyable/urls.py | 7 ++- k4ever/buyable/views.py | 53 +++++++++++++++--- k4ever/k4ever.db | Bin 63488 -> 63488 bytes k4ever/main/templates/base.html | 2 +- 7 files changed, 83 insertions(+), 15 deletions(-) create mode 100644 k4ever/buyable/templates/buyables/itemBought.html diff --git a/k4ever/buyable/models.py b/k4ever/buyable/models.py index 13da811..e520e73 100644 --- a/k4ever/buyable/models.py +++ b/k4ever/buyable/models.py @@ -1,5 +1,6 @@ from django.db import models from django.contrib.auth.models import User +import datetime # Create your models here. class BuyableType(models.Model): @@ -30,6 +31,20 @@ class Purchase(models.Model): def __unicode__(self): return "%s%s, %s by %s" % (self.buyable.name, self.isDeposit and " (deposit)" or "", self.price, self.user) - def create(self, buyable): + def create(self, buyable, isDeposit=None): + """constructor""" + if isDeposit is not None: + self.isDeposit = isDeposit self.dateTime = datetime.datetime.now() - self.buyable = buyable \ No newline at end of file + self.buyable = buyable + if self.isDeposit: + self.price = buyable.deposit + else: + self.price = buyable.price + def save(self, *args, **kwargs): + profile = self.user.get_profile() + if self.id == None: + # new item, get it! + profile.balance -= self.price + profile.save() + super(Purchase, self).save(*args, **kwargs) diff --git a/k4ever/buyable/templates/buyables/itemBought.html b/k4ever/buyable/templates/buyables/itemBought.html new file mode 100644 index 0000000..233403d --- /dev/null +++ b/k4ever/buyable/templates/buyables/itemBought.html @@ -0,0 +1,15 @@ +{% extends "base.html" %} + +{% block "content" %} + {% if not error %} + {% if item1 %} + You got the item {{ item1 }} + {% endif %} + {% if item2 %} + You got the second item {{ item2 }} + {% endif %} + {% else %} + {{ error }} + Junge junge junge + {% endif %} +{% endblock %} diff --git a/k4ever/buyable/templates/buyables/showItem.html b/k4ever/buyable/templates/buyables/showItem.html index 02a16c0..1c8036c 100644 --- a/k4ever/buyable/templates/buyables/showItem.html +++ b/k4ever/buyable/templates/buyables/showItem.html @@ -2,7 +2,7 @@ {% block "content" %} {% if item %} - You got the item {{ item }} + You got the item {{ item }}.Buy it! it+deposit! only deposit! {% else %} No item found :( {% endif %} diff --git a/k4ever/buyable/urls.py b/k4ever/buyable/urls.py index beb4562..020bc49 100644 --- a/k4ever/buyable/urls.py +++ b/k4ever/buyable/urls.py @@ -1,8 +1,9 @@ from django.conf.urls.defaults import * - +#/store/ urlpatterns = patterns('', (r'^$', 'buyable.views.showItems'), (r'^show/(\d+)/$', 'buyable.views.showItem'), - (r'^buy/(\d+)/$', 'buyable.views.buyItem'), -) \ No newline at end of file + (r'^buy/(\d+)/(|with/deposit|only/deposit)/?$', 'buyable.views.buyItem'), + (r'^bought/(\d+)/(\d+)?/?$', 'buyable.views.boughtItem'), +) diff --git a/k4ever/buyable/views.py b/k4ever/buyable/views.py index a0206a1..e82e3f9 100644 --- a/k4ever/buyable/views.py +++ b/k4ever/buyable/views.py @@ -1,9 +1,10 @@ -# Create your views here. +# -*- coding: utf-8 -*- from django.shortcuts import render_to_response from django.template import RequestContext from django.contrib.auth.decorators import login_required from django.http import HttpResponseRedirect -from models import Buyable +import datetime +from models import Buyable, Purchase @login_required def showItems(request): @@ -21,13 +22,49 @@ def showItem(request, itemid): return render_to_response("buyables/showItem.html", {'item': item}, RequestContext(request)) @login_required -def buyItem(request, itemid): +def buyItem(request, itemid, buymode=""): try: item = Buyable.objects.get(id=itemid) except Buyable.DoesNotExist: # oh no! kein item mit der id :( mutax trollt rum) - return HttpResponseRedirect("/buy/") - purchase = Purchase(user=request.user, price=item.price, isDeposit=false ) - purchase.create(item) - purchase.save() - return render_to_response("buyables/itemBought.html", {'item': item}, RequestContext(request)) \ No newline at end of file + return HttpResponseRedirect("/store/buy/") + """ tristate variable buymode: + "" only item + "with/deposit" item and deposit + "only/deposit" only deposit + """ + purchase = None + deposit = None + if buymode == "" or buymode == "with/deposit": + purchase = Purchase(user=request.user, isDeposit=False) + purchase.create(item) + purchase.save() + # TANNEK! if buymode == "with/dopsit" or buymode == "only/deposit": + if buymode == "with/deposit" or buymode == "only/deposit": + deposit = Purchase(user=request.user, isDeposit=True) + deposit.create(item) + deposit.save() + if purchase == None: + purchase = deposit + deposit = None + return HttpResponseRedirect("/store/bought/%s/%s" % (purchase.id, deposit and str(deposit.id) or "")) + +@login_required +def boughtItem(request, item1id, item2id=None): + if item1id == item2id: + item2id = None + error = None + try: + item1 = Purchase.objects.get(id=item1id) + if item2id: + item2 = Purchase.objects.get(id=item2id) + else: + item2 = None + except Purchase.DoesNotExist: + item1 = None + item2 = None + error = "Konnte gekaufte Objekte nicht wiederfinden" + if error == None and (item1.user != request.user or (item2 != None and (item2.user != request.user))): + item1 = item2 = None + error = u"Diese Items gehören dir nicht" + return render_to_response("buyables/itemBought.html", {'item1': item1, 'item2': item2, 'error': error}, RequestContext(request)) diff --git a/k4ever/k4ever.db b/k4ever/k4ever.db index c6beae29802934e0fafdc767f5b8818f93f1583f..523b228a3b7df659127bfca6e1b7b7c8f60a07cf 100644 GIT binary patch delta 1750 zcmZ{lO=w(I6oBu!XPTKw=VdZuMp|2vj5J_%eBPh;bKkR)MnQ{q(}Ft}zBJQD4N)?w zx)7!yE=wzuMeIs2*o~XcEDQ)0)Qzr+pa>}y8ZnBYBv`e?jrYDuGf$m&84i#0zVma> zIq%GcExfRWmk+_M_YXe$znvTHF@Hs5Qt8fe|WTREeeAcX$vjnEH`VPsUGGrnYuFV22 zg`B+h+k2y{r#693lnkxmKD1*i?nveWp|#-@r#zJ0mRxv}cbztk*IF?Z7ryOzTq%{v zVJIaBZZdX#E&`@=n6T0@W;4ZwR6v@vR!9lXLdUf^3mEq%*JXSN)b{tIMd*6EbG8$( zz)zNIuwM>cTG%2`vMEiyCi3YVvpuDpz%ymdr3ROttwg}N=+&u~O{H~Jl~9fplg+)> z)&~0HP7q4n1TzTk$`_)dV}l`-08qiTR6N=Z;AW+V^2F^v;gf@y>805q~TU!xe9mnw>pchrL~6vTA!{> zIh1xmvwpf9-o-ok3*N??xQgH74g3bb!t2<^Pw->>5U=1;?Bh9ON6WD+#WGHW7USok zSXxFxA%5N;%fVRYW0{L(Yi;f5N0}QrtYX;8{t)Iv9qJ@Li^p}CEqcH}P>s>?SdPVV zv~_x|J)~`jMoq3K@p!9n)1w&n?5x-h6LvV-IsIdMWF!YyiQY}m(`~v;U!w|k;HtK} z1QVyKc`^l1d zT^AL2BI!b}RVUJJ!o#tcMMeNbb1*UeW?(L2`n_3@NPBg&3G0G2fXibjJ)Vx;g0nCJ`PM ZrWp*(OPIZwJ~GV!3U@GVp7AY%5ddDjI>rD1 diff --git a/k4ever/main/templates/base.html b/k4ever/main/templates/base.html index a1b5df5..b01226d 100644 --- a/k4ever/main/templates/base.html +++ b/k4ever/main/templates/base.html @@ -8,7 +8,7 @@
Its Alive - and here could be a logo. Wouldn't that be nice? {% if user.is_authenticated %} - Logged in as {{ user }} + Logged in as {{ user }}, balance {{ user.get_profile.balance }} EUR {% endif %}