Kaufen funktioniert

This commit is contained in:
seba 2010-10-19 01:03:38 +02:00
parent 9450f3326b
commit f4ceecf205
7 changed files with 83 additions and 15 deletions

View File

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

View File

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

View File

@ -2,7 +2,7 @@
{% block "content" %}
{% if item %}
You got the item {{ item }}
You got the item {{ item }}.Buy <a href="/store/buy/{{ item.id }}/">it!</a> <a href="/store/buy/{{ item.id }}/with/deposit/">it+deposit!</a> <a href="/store/buy/{{ item.id }}/only/deposit/"> only deposit!</a>
{% else %}
No item found :(
{% endif %}

View File

@ -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'),
)
(r'^buy/(\d+)/(|with/deposit|only/deposit)/?$', 'buyable.views.buyItem'),
(r'^bought/(\d+)/(\d+)?/?$', 'buyable.views.boughtItem'),
)

View File

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

Binary file not shown.

View File

@ -8,7 +8,7 @@
<div class="title">
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 %}
</div>
<div class="navigation">