Blackness.

This commit is contained in:
Víðir Valberg Guðmundsson 2019-03-29 22:19:49 +01:00
parent 4aad051c72
commit e78013c87c
3 changed files with 202 additions and 207 deletions

View file

@ -11,39 +11,37 @@ from utils.factories import UserFactory
class ProductCategoryFactory(DjangoModelFactory): class ProductCategoryFactory(DjangoModelFactory):
class Meta: class Meta:
model = 'shop.ProductCategory' model = "shop.ProductCategory"
name = factory.Faker('word') name = factory.Faker("word")
class ProductFactory(DjangoModelFactory): class ProductFactory(DjangoModelFactory):
class Meta: class Meta:
model = 'shop.Product' model = "shop.Product"
name = factory.Faker('word') name = factory.Faker("word")
slug = factory.Faker('word') slug = factory.Faker("word")
category = factory.SubFactory(ProductCategoryFactory) category = factory.SubFactory(ProductCategoryFactory)
description = factory.Faker('paragraph') description = factory.Faker("paragraph")
price = factory.Faker('pyint') price = factory.Faker("pyint")
available_in = factory.LazyFunction( available_in = factory.LazyFunction(
lambda: lambda: DateTimeTZRange(
DateTimeTZRange( lower=timezone.now(), upper=timezone.now() + timezone.timedelta(31)
lower=timezone.now(), )
upper=timezone.now() + timezone.timedelta(31)
)
) )
class OrderFactory(DjangoModelFactory): class OrderFactory(DjangoModelFactory):
class Meta: class Meta:
model = 'shop.Order' model = "shop.Order"
user = factory.SubFactory(UserFactory) user = factory.SubFactory(UserFactory)
class OrderProductRelationFactory(DjangoModelFactory): class OrderProductRelationFactory(DjangoModelFactory):
class Meta: class Meta:
model = 'shop.OrderProductRelation' model = "shop.OrderProductRelation"
product = factory.SubFactory(ProductFactory) product = factory.SubFactory(ProductFactory)
order = factory.SubFactory(OrderFactory) order = factory.SubFactory(OrderFactory)

View file

@ -5,27 +5,22 @@ from shop.models import OrderProductRelation
class OrderProductRelationForm(forms.ModelForm): class OrderProductRelationForm(forms.ModelForm):
class Meta: class Meta:
model = OrderProductRelation model = OrderProductRelation
fields = ['quantity'] fields = ["quantity"]
def clean_quantity(self): def clean_quantity(self):
product = self.instance.product product = self.instance.product
new_quantity = self.cleaned_data['quantity'] new_quantity = self.cleaned_data["quantity"]
if product.stock_amount and product.left_in_stock < new_quantity: if product.stock_amount and product.left_in_stock < new_quantity:
raise forms.ValidationError( raise forms.ValidationError(
"Only {} left in stock.".format( "Only {} left in stock.".format(product.left_in_stock)
product.left_in_stock,
)
) )
return new_quantity return new_quantity
OrderProductRelationFormSet = modelformset_factory( OrderProductRelationFormSet = modelformset_factory(
OrderProductRelation, OrderProductRelation, form=OrderProductRelationForm, extra=0
form=OrderProductRelationForm,
extra=0
) )

View file

@ -22,177 +22,188 @@ logger = logging.getLogger("bornhack.%s" % __name__)
class CustomOrder(CreatedUpdatedModel): class CustomOrder(CreatedUpdatedModel):
text = models.TextField( text = models.TextField(help_text=_("The invoice text"))
help_text=_('The invoice text')
)
customer = models.TextField( customer = models.TextField(help_text=_("The customer info for this order"))
help_text=_('The customer info for this order')
)
amount = models.IntegerField( amount = models.IntegerField(
help_text=_('Amount of this custom order (in DKK, including VAT).') help_text=_("Amount of this custom order (in DKK, including VAT).")
) )
paid = models.BooleanField( paid = models.BooleanField(
verbose_name=_('Paid?'), verbose_name=_("Paid?"),
help_text=_('Check when this custom order has been paid (or if it gets cancelled out by a Credit Note)'), help_text=_(
"Check when this custom order has been paid (or if it gets cancelled out by a Credit Note)"
),
default=False, default=False,
) )
danish_vat = models.BooleanField( danish_vat = models.BooleanField(help_text="Danish VAT?", default=True)
help_text="Danish VAT?",
default=True
)
def __str__(self): def __str__(self):
return 'custom order id #%s' % self.pk return "custom order id #%s" % self.pk
@property @property
def vat(self): def vat(self):
if self.danish_vat: if self.danish_vat:
return Decimal(round(self.amount*Decimal(0.2), 2)) return Decimal(round(self.amount * Decimal(0.2), 2))
else: else:
return 0 return 0
class Order(CreatedUpdatedModel): class Order(CreatedUpdatedModel):
class Meta: class Meta:
unique_together = ('user', 'open') unique_together = ("user", "open")
ordering = ['-created'] ordering = ["-created"]
products = models.ManyToManyField( products = models.ManyToManyField(
'shop.Product', "shop.Product", through="shop.OrderProductRelation"
through='shop.OrderProductRelation'
) )
user = models.ForeignKey( user = models.ForeignKey(
'auth.User', "auth.User",
verbose_name=_('User'), verbose_name=_("User"),
help_text=_('The user this shop order belongs to.'), help_text=_("The user this shop order belongs to."),
related_name='orders', related_name="orders",
on_delete=models.PROTECT, on_delete=models.PROTECT,
) )
paid = models.BooleanField( paid = models.BooleanField(
verbose_name=_('Paid?'), verbose_name=_("Paid?"),
help_text=_('Whether this shop order has been paid.'), help_text=_("Whether this shop order has been paid."),
default=False, default=False,
) )
# We are using a NullBooleanField here to ensure that we only have one open order per user at a time. # We are using a NullBooleanField here to ensure that we only have one open order per user at a time.
# This "hack" is possible since postgres treats null values as different, and thus we have database level integrity. # This "hack" is possible since postgres treats null values as different, and thus we have database level integrity.
open = models.NullBooleanField( open = models.NullBooleanField(
verbose_name=_('Open?'), verbose_name=_("Open?"),
help_text=_('Whether this shop order is open or not. "None" means closed.'), help_text=_('Whether this shop order is open or not. "None" means closed.'),
default=True, default=True,
) )
CREDIT_CARD = 'credit_card' CREDIT_CARD = "credit_card"
BLOCKCHAIN = 'blockchain' BLOCKCHAIN = "blockchain"
BANK_TRANSFER = 'bank_transfer' BANK_TRANSFER = "bank_transfer"
CASH = 'cash' CASH = "cash"
PAYMENT_METHODS = [ PAYMENT_METHODS = [CREDIT_CARD, BLOCKCHAIN, BANK_TRANSFER, CASH]
CREDIT_CARD,
BLOCKCHAIN,
BANK_TRANSFER,
CASH,
]
PAYMENT_METHOD_CHOICES = [ PAYMENT_METHOD_CHOICES = [
(CREDIT_CARD, 'Credit card'), (CREDIT_CARD, "Credit card"),
(BLOCKCHAIN, 'Blockchain'), (BLOCKCHAIN, "Blockchain"),
(BANK_TRANSFER, 'Bank transfer'), (BANK_TRANSFER, "Bank transfer"),
(CASH, 'Cash'), (CASH, "Cash"),
] ]
payment_method = models.CharField( payment_method = models.CharField(
max_length=50, max_length=50, choices=PAYMENT_METHOD_CHOICES, default="", blank=True
choices=PAYMENT_METHOD_CHOICES,
default='',
blank=True
) )
cancelled = models.BooleanField(default=False) cancelled = models.BooleanField(default=False)
refunded = models.BooleanField( refunded = models.BooleanField(
verbose_name=_('Refunded?'), verbose_name=_("Refunded?"),
help_text=_('Whether this order has been refunded.'), help_text=_("Whether this order has been refunded."),
default=False, default=False,
) )
customer_comment = models.TextField( customer_comment = models.TextField(
verbose_name=_('Customer comment'), verbose_name=_("Customer comment"),
help_text=_('If you have any comments about the order please enter them here.'), help_text=_("If you have any comments about the order please enter them here."),
default='', default="",
blank=True, blank=True,
) )
invoice_address = models.TextField( invoice_address = models.TextField(
help_text=_('The invoice address for this order. Leave blank to use the email associated with the logged in user.'), help_text=_(
blank=True "The invoice address for this order. Leave blank to use the email associated with the logged in user."
),
blank=True,
) )
notes = models.TextField( notes = models.TextField(
help_text='Any internal notes about this order can be entered here. They will not be printed on the invoice or shown to the customer in any way.', help_text="Any internal notes about this order can be entered here. They will not be printed on the invoice or shown to the customer in any way.",
default='', default="",
blank=True, blank=True,
) )
objects = OrderQuerySet.as_manager() objects = OrderQuerySet.as_manager()
def __str__(self): def __str__(self):
return 'shop order id #%s' % self.pk return "shop order id #%s" % self.pk
def get_number_of_items(self): def get_number_of_items(self):
return self.products.aggregate( return self.products.aggregate(sum=Sum("orderproductrelation__quantity"))["sum"]
sum=Sum('orderproductrelation__quantity')
)['sum']
@property @property
def vat(self): def vat(self):
return Decimal(self.total*Decimal(0.2)) return Decimal(self.total * Decimal(0.2))
@property @property
def total(self): def total(self):
if self.products.all(): if self.products.all():
return Decimal(self.products.aggregate( return Decimal(
sum=Sum( self.products.aggregate(
models.F('orderproductrelation__product__price') * sum=Sum(
models.F('orderproductrelation__quantity'), models.F("orderproductrelation__product__price")
output_field=models.IntegerField() * models.F("orderproductrelation__quantity"),
) output_field=models.IntegerField(),
)['sum']) )
)["sum"]
)
else: else:
return False return False
def get_coinify_callback_url(self, request): def get_coinify_callback_url(self, request):
""" Check settings for an alternative COINIFY_CALLBACK_HOSTNAME otherwise use the one from the request """ """ Check settings for an alternative COINIFY_CALLBACK_HOSTNAME otherwise use the one from the request """
if hasattr(settings, 'COINIFY_CALLBACK_HOSTNAME') and settings.COINIFY_CALLBACK_HOSTNAME: if (
hasattr(settings, "COINIFY_CALLBACK_HOSTNAME")
and settings.COINIFY_CALLBACK_HOSTNAME
):
host = settings.COINIFY_CALLBACK_HOSTNAME host = settings.COINIFY_CALLBACK_HOSTNAME
else: else:
host = request.get_host() host = request.get_host()
return 'https://' + host + str(reverse_lazy('shop:coinify_callback', kwargs={'pk': self.pk})) return (
"https://"
+ host
+ str(reverse_lazy("shop:coinify_callback", kwargs={"pk": self.pk}))
)
def get_coinify_thanks_url(self, request): def get_coinify_thanks_url(self, request):
return 'https://' + request.get_host() + str(reverse_lazy('shop:coinify_thanks', kwargs={'pk': self.pk})) return (
"https://"
+ request.get_host()
+ str(reverse_lazy("shop:coinify_thanks", kwargs={"pk": self.pk}))
)
def get_epay_accept_url(self, request): def get_epay_accept_url(self, request):
return 'https://' + request.get_host() + str(reverse_lazy('shop:epay_thanks', kwargs={'pk': self.pk})) return (
"https://"
+ request.get_host()
+ str(reverse_lazy("shop:epay_thanks", kwargs={"pk": self.pk}))
)
def get_cancel_url(self, request): def get_cancel_url(self, request):
return 'https://' + request.get_host() + str(reverse_lazy('shop:order_detail', kwargs={'pk': self.pk})) return (
"https://"
+ request.get_host()
+ str(reverse_lazy("shop:order_detail", kwargs={"pk": self.pk}))
)
def get_epay_callback_url(self, request): def get_epay_callback_url(self, request):
return 'https://' + request.get_host() + str(reverse_lazy('shop:epay_callback', kwargs={'pk': self.pk})) return (
"https://"
+ request.get_host()
+ str(reverse_lazy("shop:epay_callback", kwargs={"pk": self.pk}))
)
@property @property
def description(self): def description(self):
return "Order #%s" % self.pk return "Order #%s" % self.pk
def get_absolute_url(self): def get_absolute_url(self):
return str(reverse_lazy('shop:order_detail', kwargs={'pk': self.pk})) return str(reverse_lazy("shop:order_detail", kwargs={"pk": self.pk}))
def create_tickets(self, request=None): def create_tickets(self, request=None):
for order_product in self.orderproductrelation_set.all(): for order_product in self.orderproductrelation_set.all():
@ -203,17 +214,24 @@ class Order(CreatedUpdatedModel):
ticket_type=order_product.product.ticket_type, ticket_type=order_product.product.ticket_type,
) )
already_created_tickets = self.shoptickets.filter(**query_kwargs).count() already_created_tickets = self.shoptickets.filter(
tickets_to_create = max(0, order_product.quantity - already_created_tickets) **query_kwargs
).count()
tickets_to_create = max(
0, order_product.quantity - already_created_tickets
)
# create the number of tickets required # create the number of tickets required
if tickets_to_create > 0: if tickets_to_create > 0:
for _ in range(0, (order_product.quantity - already_created_tickets)): for _ in range(
self.shoptickets.create( 0, (order_product.quantity - already_created_tickets)
**query_kwargs ):
) self.shoptickets.create(**query_kwargs)
msg = "Created %s tickets of type: %s" % (order_product.quantity, order_product.product.ticket_type.name) msg = "Created %s tickets of type: %s" % (
order_product.quantity,
order_product.product.ticket_type.name,
)
if request: if request:
messages.success(request, msg) messages.success(request, msg)
else: else:
@ -240,7 +258,10 @@ class Order(CreatedUpdatedModel):
self.refunded = True self.refunded = True
# delete any tickets related to this order # delete any tickets related to this order
if self.shoptickets.all(): if self.shoptickets.all():
msg = "Order %s marked as refunded, deleting %s tickets..." % (self.pk, self.shoptickets.count()) msg = "Order %s marked as refunded, deleting %s tickets..." % (
self.pk,
self.shoptickets.count(),
)
if request: if request:
messages.success(request, msg) messages.success(request, msg)
else: else:
@ -273,7 +294,10 @@ class Order(CreatedUpdatedModel):
return False return False
def is_partially_handed_out(self): def is_partially_handed_out(self):
if self.orderproductrelation_set.filter(handed_out=True).count() != 0 and self.orderproductrelation_set.filter(handed_out=False).count() != 0: if (
self.orderproductrelation_set.filter(handed_out=True).count() != 0
and self.orderproductrelation_set.filter(handed_out=False).count() != 0
):
# some products are handed out, others are not # some products are handed out, others are not
return True return True
else: else:
@ -313,8 +337,8 @@ class Order(CreatedUpdatedModel):
class ProductCategory(CreatedUpdatedModel, UUIDModel): class ProductCategory(CreatedUpdatedModel, UUIDModel):
class Meta: class Meta:
verbose_name = 'Product category' verbose_name = "Product category"
verbose_name_plural = 'Product categories' verbose_name_plural = "Product categories"
name = models.CharField(max_length=150) name = models.CharField(max_length=150)
slug = models.SlugField() slug = models.SlugField()
@ -330,61 +354,51 @@ class ProductCategory(CreatedUpdatedModel, UUIDModel):
class Product(CreatedUpdatedModel, UUIDModel): class Product(CreatedUpdatedModel, UUIDModel):
class Meta: class Meta:
verbose_name = 'Product' verbose_name = "Product"
verbose_name_plural = 'Products' verbose_name_plural = "Products"
ordering = ['available_in', 'price', 'name'] ordering = ["available_in", "price", "name"]
category = models.ForeignKey( category = models.ForeignKey(
'shop.ProductCategory', "shop.ProductCategory", related_name="products", on_delete=models.PROTECT
related_name='products',
on_delete=models.PROTECT,
) )
name = models.CharField(max_length=150) name = models.CharField(max_length=150)
slug = models.SlugField(unique=True, max_length=100) slug = models.SlugField(unique=True, max_length=100)
price = models.IntegerField( price = models.IntegerField(
help_text=_('Price of the product (in DKK, including VAT).') help_text=_("Price of the product (in DKK, including VAT).")
) )
description = models.TextField() description = models.TextField()
available_in = DateTimeRangeField( available_in = DateTimeRangeField(
help_text=_( help_text=_(
'Which period is this product available for purchase? | ' "Which period is this product available for purchase? | "
'(Format: YYYY-MM-DD HH:MM) | Only one of start/end is required' "(Format: YYYY-MM-DD HH:MM) | Only one of start/end is required"
) )
) )
ticket_type = models.ForeignKey( ticket_type = models.ForeignKey(
'tickets.TicketType', "tickets.TicketType", on_delete=models.PROTECT, null=True, blank=True
on_delete=models.PROTECT,
null=True,
blank=True
) )
stock_amount = models.IntegerField( stock_amount = models.IntegerField(
help_text=( help_text=(
'Initial amount available in stock if there is a limited ' "Initial amount available in stock if there is a limited "
'supply, e.g. fridge space' "supply, e.g. fridge space"
), ),
null=True, null=True,
blank=True blank=True,
) )
objects = ProductQuerySet.as_manager() objects = ProductQuerySet.as_manager()
def __str__(self): def __str__(self):
return '{} ({} DKK)'.format( return "{} ({} DKK)".format(self.name, self.price)
self.name,
self.price,
)
def clean(self): def clean(self):
if self.category.name == 'Tickets' and not self.ticket_type: if self.category.name == "Tickets" and not self.ticket_type:
raise ValidationError( raise ValidationError("Products with category Tickets need a ticket_type")
'Products with category Tickets need a ticket_type'
)
def is_available(self): def is_available(self):
""" Is the product available or not? """ Is the product available or not?
@ -407,7 +421,7 @@ class Product(CreatedUpdatedModel, UUIDModel):
def is_old(self): def is_old(self):
now = timezone.now() now = timezone.now()
if hasattr(self.available_in, 'upper') and self.available_in.upper: if hasattr(self.available_in, "upper") and self.available_in.upper:
return self.available_in.upper < now return self.available_in.upper < now
return False return False
@ -425,10 +439,8 @@ class Product(CreatedUpdatedModel, UUIDModel):
# or is marked to be paid with cash or bank transfer, meaning it is a # or is marked to be paid with cash or bank transfer, meaning it is a
# "reservation" of the product in question. # "reservation" of the product in question.
sold = OrderProductRelation.objects.filter( sold = OrderProductRelation.objects.filter(
product=self, product=self, order__open=None, order__cancelled=False
order__open=None, ).aggregate(Sum("quantity"))["quantity__sum"]
order__cancelled=False,
).aggregate(Sum('quantity'))['quantity__sum']
total_left = self.stock_amount - (sold or 0) total_left = self.stock_amount - (sold or 0)
@ -445,8 +457,8 @@ class Product(CreatedUpdatedModel, UUIDModel):
class OrderProductRelation(CreatedUpdatedModel): class OrderProductRelation(CreatedUpdatedModel):
order = models.ForeignKey('shop.Order', on_delete=models.PROTECT) order = models.ForeignKey("shop.Order", on_delete=models.PROTECT)
product = models.ForeignKey('shop.Product', on_delete=models.PROTECT) product = models.ForeignKey("shop.Product", on_delete=models.PROTECT)
quantity = models.PositiveIntegerField() quantity = models.PositiveIntegerField()
handed_out = models.BooleanField(default=False) handed_out = models.BooleanField(default=False)
@ -457,76 +469,64 @@ class OrderProductRelation(CreatedUpdatedModel):
def clean(self): def clean(self):
if self.handed_out and not self.order.paid: if self.handed_out and not self.order.paid:
raise ValidationError( raise ValidationError(
'Product can not be handed out when order is not paid.' "Product can not be handed out when order is not paid."
) )
class EpayCallback(CreatedUpdatedModel, UUIDModel): class EpayCallback(CreatedUpdatedModel, UUIDModel):
class Meta: class Meta:
verbose_name = 'Epay Callback' verbose_name = "Epay Callback"
verbose_name_plural = 'Epay Callbacks' verbose_name_plural = "Epay Callbacks"
ordering = ['-created'] ordering = ["-created"]
payload = JSONField() payload = JSONField()
md5valid = models.BooleanField(default=False) md5valid = models.BooleanField(default=False)
def __str__(self): def __str__(self):
return 'callback at %s (md5 valid: %s)' % (self.created, self.md5valid) return "callback at %s (md5 valid: %s)" % (self.created, self.md5valid)
class EpayPayment(CreatedUpdatedModel, UUIDModel): class EpayPayment(CreatedUpdatedModel, UUIDModel):
class Meta: class Meta:
verbose_name = 'Epay Payment' verbose_name = "Epay Payment"
verbose_name_plural = 'Epay Payments' verbose_name_plural = "Epay Payments"
order = models.OneToOneField('shop.Order', on_delete=models.PROTECT) order = models.OneToOneField("shop.Order", on_delete=models.PROTECT)
callback = models.ForeignKey('shop.EpayCallback', on_delete=models.PROTECT) callback = models.ForeignKey("shop.EpayCallback", on_delete=models.PROTECT)
txnid = models.IntegerField() txnid = models.IntegerField()
class CreditNote(CreatedUpdatedModel): class CreditNote(CreatedUpdatedModel):
class Meta: class Meta:
ordering = ['-created'] ordering = ["-created"]
amount = models.DecimalField( amount = models.DecimalField(max_digits=10, decimal_places=2)
max_digits=10,
decimal_places=2
)
text = models.TextField( text = models.TextField(help_text="Description of what this credit note covers")
help_text="Description of what this credit note covers"
)
pdf = models.FileField( pdf = models.FileField(null=True, blank=True, upload_to="creditnotes/")
null=True,
blank=True,
upload_to='creditnotes/'
)
user = models.ForeignKey( user = models.ForeignKey(
'auth.User', "auth.User",
verbose_name=_('User'), verbose_name=_("User"),
help_text=_('The user this credit note belongs to, if any.'), help_text=_("The user this credit note belongs to, if any."),
related_name='creditnotes', related_name="creditnotes",
on_delete=models.PROTECT, on_delete=models.PROTECT,
null=True, null=True,
blank=True blank=True,
) )
customer = models.TextField( customer = models.TextField(
help_text="Customer info if no user is selected", help_text="Customer info if no user is selected", blank=True, default=""
blank=True,
default='',
) )
danish_vat = models.BooleanField( danish_vat = models.BooleanField(help_text="Danish VAT?", default=True)
help_text="Danish VAT?",
default=True
)
paid = models.BooleanField( paid = models.BooleanField(
verbose_name=_('Paid?'), verbose_name=_("Paid?"),
help_text=_('Whether the amount in this creditnote has been paid back to the customer.'), help_text=_(
"Whether the amount in this creditnote has been paid back to the customer."
),
default=False, default=False,
) )
@ -536,24 +536,24 @@ class CreditNote(CreatedUpdatedModel):
errors = [] errors = []
if self.user and self.customer: if self.user and self.customer:
msg = "Customer info should be blank if a user is selected." msg = "Customer info should be blank if a user is selected."
errors.append(ValidationError({'user', msg})) errors.append(ValidationError({"user", msg}))
errors.append(ValidationError({'customer', msg})) errors.append(ValidationError({"customer", msg}))
if not self.user and not self.customer: if not self.user and not self.customer:
msg = "Either pick a user or fill in Customer info" msg = "Either pick a user or fill in Customer info"
errors.append(ValidationError({'user', msg})) errors.append(ValidationError({"user", msg}))
errors.append(ValidationError({'customer', msg})) errors.append(ValidationError({"customer", msg}))
if errors: if errors:
raise ValidationError(errors) raise ValidationError(errors)
def __str__(self): def __str__(self):
if self.user: if self.user:
return 'creditnoote#%s - %s DKK (customer: user %s)' % ( return "creditnoote#%s - %s DKK (customer: user %s)" % (
self.id, self.id,
self.amount, self.amount,
self.user.email, self.user.email,
) )
else: else:
return 'creditnoote#%s - %s DKK (customer: %s)' % ( return "creditnoote#%s - %s DKK (customer: %s)" % (
self.id, self.id,
self.amount, self.amount,
self.customer, self.customer,
@ -562,34 +562,28 @@ class CreditNote(CreatedUpdatedModel):
@property @property
def vat(self): def vat(self):
if self.danish_vat: if self.danish_vat:
return Decimal(round(self.amount*Decimal(0.2), 2)) return Decimal(round(self.amount * Decimal(0.2), 2))
else: else:
return 0 return 0
@property @property
def filename(self): def filename(self):
return 'bornhack_creditnote_%s.pdf' % self.pk return "bornhack_creditnote_%s.pdf" % self.pk
class Invoice(CreatedUpdatedModel): class Invoice(CreatedUpdatedModel):
order = models.OneToOneField( order = models.OneToOneField(
'shop.Order', "shop.Order", null=True, blank=True, on_delete=models.PROTECT
null=True,
blank=True,
on_delete=models.PROTECT
) )
customorder = models.OneToOneField( customorder = models.OneToOneField(
'shop.CustomOrder', "shop.CustomOrder", null=True, blank=True, on_delete=models.PROTECT
null=True,
blank=True,
on_delete=models.PROTECT
) )
pdf = models.FileField(null=True, blank=True, upload_to='invoices/') pdf = models.FileField(null=True, blank=True, upload_to="invoices/")
sent_to_customer = models.BooleanField(default=False) sent_to_customer = models.BooleanField(default=False)
def __str__(self): def __str__(self):
if self.order: if self.order:
return 'invoice#%s - shop order %s - %s - total %s DKK (sent to %s: %s)' % ( return "invoice#%s - shop order %s - %s - total %s DKK (sent to %s: %s)" % (
self.id, self.id,
self.order.id, self.order.id,
self.order.created, self.order.created,
@ -598,52 +592,60 @@ class Invoice(CreatedUpdatedModel):
self.sent_to_customer, self.sent_to_customer,
) )
elif self.customorder: elif self.customorder:
return 'invoice#%s - custom order %s - %s - amount %s DKK (customer: %s)' % ( return (
self.id, "invoice#%s - custom order %s - %s - amount %s DKK (customer: %s)"
self.customorder.id, % (
self.customorder.created, self.id,
self.customorder.amount, self.customorder.id,
unidecode(self.customorder.customer), self.customorder.created,
self.customorder.amount,
unidecode(self.customorder.customer),
)
) )
@property @property
def filename(self): def filename(self):
return 'bornhack_invoice_%s.pdf' % self.pk return "bornhack_invoice_%s.pdf" % self.pk
def regretdate(self): def regretdate(self):
return self.created+timedelta(days=15) return self.created + timedelta(days=15)
class CoinifyAPIInvoice(CreatedUpdatedModel): class CoinifyAPIInvoice(CreatedUpdatedModel):
coinify_id = models.IntegerField(null=True) coinify_id = models.IntegerField(null=True)
invoicejson = JSONField() invoicejson = JSONField()
order = models.ForeignKey('shop.Order', related_name="coinify_api_invoices", on_delete=models.PROTECT) order = models.ForeignKey(
"shop.Order", related_name="coinify_api_invoices", on_delete=models.PROTECT
)
def __str__(self): def __str__(self):
return "coinifyinvoice for order #%s" % self.order.id return "coinifyinvoice for order #%s" % self.order.id
@property @property
def expired(self): def expired(self):
return parse_datetime(self.invoicejson['expire_time']) < timezone.now() return parse_datetime(self.invoicejson["expire_time"]) < timezone.now()
class CoinifyAPICallback(CreatedUpdatedModel): class CoinifyAPICallback(CreatedUpdatedModel):
headers = JSONField() headers = JSONField()
payload = JSONField(blank=True) payload = JSONField(blank=True)
body = models.TextField(default='') body = models.TextField(default="")
order = models.ForeignKey('shop.Order', related_name="coinify_api_callbacks", on_delete=models.PROTECT) order = models.ForeignKey(
"shop.Order", related_name="coinify_api_callbacks", on_delete=models.PROTECT
)
authenticated = models.BooleanField(default=False) authenticated = models.BooleanField(default=False)
def __str__(self): def __str__(self):
return 'order #%s callback at %s' % (self.order.id, self.created) return "order #%s callback at %s" % (self.order.id, self.created)
class CoinifyAPIRequest(CreatedUpdatedModel): class CoinifyAPIRequest(CreatedUpdatedModel):
order = models.ForeignKey('shop.Order', related_name="coinify_api_requests", on_delete=models.PROTECT) order = models.ForeignKey(
"shop.Order", related_name="coinify_api_requests", on_delete=models.PROTECT
)
method = models.CharField(max_length=100) method = models.CharField(max_length=100)
payload = JSONField() payload = JSONField()
response = JSONField() response = JSONField()
def __str__(self): def __str__(self):
return 'order %s api request %s' % (self.order.id, self.method) return "order %s api request %s" % (self.order.id, self.method)