91 lines
2.1 KiB
Python
91 lines
2.1 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, isDeposit=False):
|
|
p = Purchase()
|
|
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)
|
|
price = models.FloatField()
|
|
dateTime = models.DateTimeField()
|
|
|
|
def create(self, user=None):
|
|
models.Model.__init__(self)
|
|
self.price = 0.0
|
|
self.dateTime = datetime.datetime.now()
|
|
if user:
|
|
self.user = user
|
|
|
|
def addItems(self, items):
|
|
for item in items:
|
|
self.purchase.add(item)
|
|
self.price += item.price
|
|
|
|
def updatePrice(self):
|
|
self.price = 0.0
|
|
for item in self.purchase_set.all():
|
|
self.price += item.price
|
|
|
|
def __unicode__(self):
|
|
return "Price %s, User %s" % (self.price, self.user)
|
|
|
|
class Purchase(models.Model):
|
|
order = models.ForeignKey(Order)
|
|
price = models.FloatField()
|
|
isDeposit = models.BooleanField()
|
|
buyable = models.ForeignKey(Buyable)
|
|
|
|
@staticmethod
|
|
def create(order, buyable, isDeposit=False):
|
|
p = Purchase()
|
|
p.order = order
|
|
if isDeposit:
|
|
p.price = buyable.deposit
|
|
else:
|
|
p.price = buyable.price
|
|
p.dateTime = datetime.datetime.now()
|
|
p.buyable = buyable
|
|
|
|
return p
|
|
|
|
def __unicode__(self):
|
|
return "%s%s, %s" % (self.buyable.name, self.isDeposit and " (deposit)" or "", self.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)
|
|
|
|
|