k4ever/k4ever/buyable/models.py

70 lines
1.7 KiB
Python

from django.db import models
from django.contrib.auth.models import User
import datetime
# Create your models here.
class BuyableType(models.Model):
name = models.CharField(max_length=100)
def __unicode__(self):
return self.name
class Buyable(models.Model):
name = models.CharField(max_length=100)
price = models.FloatField()
image = models.ImageField(upload_to='img/buyable/')
deposit = models.FloatField()
description = models.TextField()
buyableType = models.ManyToManyField(BuyableType)
def createPurchase(self, user, isDeposit=False):
p = Purchase()
p.user = user
if isDeposit:
p.price = self.deposit
else:
p.price = self.price
p.dateTime = datetime.datetime.now()
p.buyable = self
return p
def __unicode__(self):
return "%s (%s EUR/%s Pfand)" % (self.name, self.price, self.deposit)
class Order(models.Model):
user = models.ForeignKey(User)
purchase = models.ManyToManyField('Purchase')
price = models.FloatField()
dateTime = models.DateTimeField()
def __init__(self, user=None, items=None):
if user:
self.user = user
if items:
for item in items:
purchase.add(item)
def __unicode__(self):
return "Price %s, User %s" % (self.price, self.user)
class Purchase(models.Model):
price = models.FloatField()
isDeposit = models.BooleanField()
buyable = models.ForeignKey(Buyable)
def __unicode__(self):
return "%s%s, %s by %s" % (self.buyable.name, self.isDeposit and " (deposit)" or "", self.price, self.user)
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)