2016-04-22 20:38:44 +00:00
|
|
|
from django.db import models
|
2016-05-10 15:55:54 +00:00
|
|
|
from django.contrib.postgres.fields import DateTimeRangeField, JSONField
|
2016-04-22 20:38:44 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2016-05-06 20:33:59 +00:00
|
|
|
from django.utils import timezone
|
2016-04-22 20:38:44 +00:00
|
|
|
|
|
|
|
from bornhack.utils import CreatedUpdatedModel, UUIDModel
|
|
|
|
|
2016-05-10 20:20:01 +00:00
|
|
|
from .managers import ProductQuerySet
|
2016-05-06 20:33:59 +00:00
|
|
|
|
2016-04-22 20:38:44 +00:00
|
|
|
|
2016-05-10 20:20:01 +00:00
|
|
|
class Order(CreatedUpdatedModel):
|
|
|
|
|
|
|
|
products = models.ManyToManyField(
|
|
|
|
'shop.Product',
|
|
|
|
through='shop.OrderProductRelation'
|
|
|
|
)
|
2016-04-22 20:38:44 +00:00
|
|
|
|
|
|
|
user = models.ForeignKey(
|
|
|
|
'auth.User',
|
|
|
|
verbose_name=_('User'),
|
2016-05-10 20:20:01 +00:00
|
|
|
help_text=_('The user this order belongs to.'),
|
|
|
|
related_name='orders',
|
2016-04-22 20:38:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
paid = models.BooleanField(
|
|
|
|
verbose_name=_('Paid?'),
|
2016-05-10 20:20:01 +00:00
|
|
|
help_text=_('Whether this order has been paid.'),
|
2016-04-22 20:38:44 +00:00
|
|
|
default=False,
|
|
|
|
)
|
|
|
|
|
2016-05-10 20:20:01 +00:00
|
|
|
camp = models.ForeignKey(
|
|
|
|
'camps.Camp',
|
|
|
|
verbose_name=_('Camp'),
|
|
|
|
help_text=_('The camp this order is for.'),
|
2016-04-22 20:38:44 +00:00
|
|
|
)
|
|
|
|
|
2016-05-10 15:55:54 +00:00
|
|
|
CREDIT_CARD = 'credit_card'
|
2016-05-11 06:37:39 +00:00
|
|
|
BLOCKCHAIN = 'blockchain'
|
2016-05-10 15:55:54 +00:00
|
|
|
BANK_TRANSFER = 'bank_transfer'
|
|
|
|
|
|
|
|
PAYMENT_METHODS = [
|
|
|
|
(CREDIT_CARD, 'Credit card'),
|
2016-05-11 06:37:39 +00:00
|
|
|
(BLOCKCHAIN, 'Blockchain'),
|
2016-05-10 15:55:54 +00:00
|
|
|
(BANK_TRANSFER, 'Bank transfer'),
|
|
|
|
]
|
|
|
|
|
|
|
|
payment_method = models.CharField(
|
|
|
|
max_length=50,
|
|
|
|
choices=PAYMENT_METHODS,
|
|
|
|
default=CREDIT_CARD
|
|
|
|
)
|
|
|
|
|
2016-05-06 20:33:59 +00:00
|
|
|
|
2016-05-10 20:20:01 +00:00
|
|
|
class ProductCategory(CreatedUpdatedModel, UUIDModel):
|
|
|
|
class Meta:
|
|
|
|
verbose_name = 'Product category'
|
|
|
|
verbose_name_plural = 'Product categories'
|
|
|
|
|
|
|
|
name = models.CharField(max_length=150)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
2016-05-10 15:55:54 +00:00
|
|
|
|
2016-04-22 20:38:44 +00:00
|
|
|
|
2016-05-10 20:20:01 +00:00
|
|
|
class Product(CreatedUpdatedModel, UUIDModel):
|
2016-04-22 20:38:44 +00:00
|
|
|
class Meta:
|
2016-05-10 20:20:01 +00:00
|
|
|
verbose_name = 'Product'
|
|
|
|
verbose_name_plural = 'Products'
|
2016-05-06 20:33:59 +00:00
|
|
|
ordering = ['available_in']
|
2016-04-22 20:38:44 +00:00
|
|
|
|
2016-05-10 20:20:01 +00:00
|
|
|
category = models.ForeignKey('shop.ProductCategory')
|
2016-04-22 20:38:44 +00:00
|
|
|
|
2016-05-10 20:20:01 +00:00
|
|
|
name = models.CharField(max_length=150)
|
2016-04-22 20:38:44 +00:00
|
|
|
|
|
|
|
price = models.IntegerField(
|
2016-05-10 20:20:01 +00:00
|
|
|
help_text=_('Price of the product (in DKK).')
|
2016-04-22 20:38:44 +00:00
|
|
|
)
|
|
|
|
|
2016-05-10 20:20:01 +00:00
|
|
|
description = models.TextField()
|
|
|
|
|
2016-04-22 20:38:44 +00:00
|
|
|
available_in = DateTimeRangeField(
|
2016-05-06 20:33:59 +00:00
|
|
|
help_text=_(
|
2016-05-10 20:20:01 +00:00
|
|
|
'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'
|
|
|
|
)
|
2016-04-22 20:38:44 +00:00
|
|
|
)
|
|
|
|
|
2016-05-10 20:20:01 +00:00
|
|
|
objects = ProductQuerySet.as_manager()
|
2016-05-06 20:33:59 +00:00
|
|
|
|
2016-04-22 20:38:44 +00:00
|
|
|
def __str__(self):
|
2016-05-06 20:33:59 +00:00
|
|
|
return '{} ({} DKK)'.format(
|
|
|
|
self.name,
|
|
|
|
self.price,
|
|
|
|
)
|
|
|
|
|
|
|
|
def is_available(self):
|
|
|
|
now = timezone.now()
|
|
|
|
return now in self.available_in
|
2016-05-10 15:55:54 +00:00
|
|
|
|
|
|
|
|
2016-05-10 20:20:01 +00:00
|
|
|
class OrderProductRelation(models.Model):
|
|
|
|
order = models.ForeignKey('shop.Order')
|
|
|
|
product = models.ForeignKey('shop.Product')
|
|
|
|
quantity = models.PositiveIntegerField()
|
|
|
|
handed_out = models.BooleanField(default=False)
|
|
|
|
|
|
|
|
|
2016-05-10 15:55:54 +00:00
|
|
|
class EpayCallback(CreatedUpdatedModel, UUIDModel):
|
|
|
|
class Meta:
|
|
|
|
verbose_name = 'Epay Callback'
|
|
|
|
verbose_name_plural = 'Epay Callbacks'
|
2016-05-11 06:37:39 +00:00
|
|
|
|
2016-05-10 15:55:54 +00:00
|
|
|
payload = JSONField()
|
|
|
|
|
|
|
|
|
|
|
|
class EpayPayment(CreatedUpdatedModel, UUIDModel):
|
|
|
|
class Meta:
|
|
|
|
verbose_name = 'Epay Payment'
|
|
|
|
verbose_name_plural = 'Epay Payments'
|
|
|
|
|
2016-05-10 20:20:01 +00:00
|
|
|
order = models.OneToOneField('shop.Order')
|
|
|
|
callback = models.ForeignKey('shop.EpayCallback')
|
2016-05-10 15:55:54 +00:00
|
|
|
txnid = models.IntegerField()
|