bornhack-website/shop/models.py

179 lines
4.6 KiB
Python
Raw Normal View History

from django.db import models
from django.db.models.aggregates import Sum
from django.contrib.postgres.fields import DateTimeRangeField, JSONField
2016-05-15 22:09:00 +00:00
from django.utils.text import slugify
from django.utils.translation import ugettext_lazy as _
2016-05-06 20:33:59 +00:00
from django.utils import timezone
2016-05-16 15:11:07 +00:00
from django.core.urlresolvers import reverse_lazy
from bornhack.utils import CreatedUpdatedModel, UUIDModel
from .managers import ProductQuerySet
2016-05-06 20:33:59 +00:00
class Order(CreatedUpdatedModel):
2016-05-15 22:09:00 +00:00
class Meta:
unique_together = ('user', 'open')
products = models.ManyToManyField(
'shop.Product',
through='shop.OrderProductRelation'
)
user = models.ForeignKey(
'auth.User',
verbose_name=_('User'),
help_text=_('The user this order belongs to.'),
related_name='orders',
)
paid = models.BooleanField(
verbose_name=_('Paid?'),
help_text=_('Whether this order has been paid.'),
default=False,
)
2016-05-15 22:09:00 +00:00
open = models.NullBooleanField(
verbose_name=_('Open?'),
help_text=_('Whether this order is open or not. "None" means closed.'),
default=True,
2016-05-13 06:37:47 +00:00
)
camp = models.ForeignKey(
'camps.Camp',
verbose_name=_('Camp'),
help_text=_('The camp this order is for.'),
)
CREDIT_CARD = 'credit_card'
BLOCKCHAIN = 'blockchain'
BANK_TRANSFER = 'bank_transfer'
PAYMENT_METHODS = [
2016-05-15 22:09:00 +00:00
CREDIT_CARD,
BLOCKCHAIN,
BANK_TRANSFER,
]
PAYMENT_METHOD_CHOICES = [
(CREDIT_CARD, 'Credit card'),
(BLOCKCHAIN, 'Blockchain'),
(BANK_TRANSFER, 'Bank transfer'),
]
payment_method = models.CharField(
max_length=50,
2016-05-15 22:09:00 +00:00
choices=PAYMENT_METHOD_CHOICES,
2016-05-12 17:08:54 +00:00
default=BLOCKCHAIN
)
def get_number_of_items(self):
return self.products.aggregate(
sum=Sum('orderproductrelation__quantity')
)['sum']
@property
def vat(self):
return self.total*0.2
@property
def total(self):
return self.products.aggregate(
sum=Sum(
models.F('orderproductrelation__product__price') *
models.F('orderproductrelation__quantity'),
output_field=models.IntegerField()
)
)['sum']
def get_absolute_url(self):
return reverse_lazy('shop:order_detail', kwargs={'pk': self.pk})
2016-05-06 20:33:59 +00:00
class ProductCategory(CreatedUpdatedModel, UUIDModel):
class Meta:
verbose_name = 'Product category'
verbose_name_plural = 'Product categories'
name = models.CharField(max_length=150)
2016-05-15 22:09:00 +00:00
slug = models.SlugField()
def __str__(self):
return self.name
2016-05-15 22:09:00 +00:00
def save(self, **kwargs):
self.slug = slugify(self.name)
super(ProductCategory, self).save(**kwargs)
class Product(CreatedUpdatedModel, UUIDModel):
class Meta:
verbose_name = 'Product'
verbose_name_plural = 'Products'
2016-05-06 20:33:59 +00:00
ordering = ['available_in']
2016-05-15 22:09:00 +00:00
category = models.ForeignKey(
'shop.ProductCategory',
related_name='products'
)
name = models.CharField(max_length=150)
2016-05-15 22:09:00 +00:00
slug = models.SlugField()
price = models.IntegerField(
help_text=_('Price of the product (in DKK).')
)
description = models.TextField()
available_in = DateTimeRangeField(
2016-05-06 20:33:59 +00:00
help_text=_(
'Which period is this product available for purchase? | '
2016-05-06 20:33:59 +00:00
'(Format: YYYY-MM-DD HH:MM) | Only one of start/end is required'
)
)
objects = ProductQuerySet.as_manager()
2016-05-06 20:33:59 +00:00
def __str__(self):
2016-05-06 20:33:59 +00:00
return '{} ({} DKK)'.format(
self.name,
self.price,
)
2016-05-15 22:09:00 +00:00
def save(self, **kwargs):
self.slug = slugify(self.name)
super(Product, self).save(**kwargs)
2016-05-06 20:33:59 +00:00
def is_available(self):
now = timezone.now()
return now in self.available_in
class OrderProductRelation(CreatedUpdatedModel):
order = models.ForeignKey('shop.Order')
product = models.ForeignKey('shop.Product')
quantity = models.PositiveIntegerField()
handed_out = models.BooleanField(default=False)
2016-05-15 22:09:00 +00:00
@property
def total(self):
return self.product.price * self.quantity
class EpayCallback(CreatedUpdatedModel, UUIDModel):
class Meta:
verbose_name = 'Epay Callback'
verbose_name_plural = 'Epay Callbacks'
payload = JSONField()
class EpayPayment(CreatedUpdatedModel, UUIDModel):
class Meta:
verbose_name = 'Epay Payment'
verbose_name_plural = 'Epay Payments'
order = models.OneToOneField('shop.Order')
callback = models.ForeignKey('shop.EpayCallback')
txnid = models.IntegerField()