2016-05-25 17:13:45 +00:00
|
|
|
from django.conf import settings
|
2016-04-22 20:38:44 +00:00
|
|
|
from django.db import models
|
2016-05-14 17:39:34 +00:00
|
|
|
from django.db.models.aggregates import Sum
|
2016-06-19 06:43:56 +00:00
|
|
|
from django.contrib import messages
|
2016-05-10 15:55:54 +00:00
|
|
|
from django.contrib.postgres.fields import DateTimeRangeField, JSONField
|
2016-05-31 21:23:05 +00:00
|
|
|
from django.http import HttpResponse
|
2016-05-15 22:09:00 +00:00
|
|
|
from django.utils.text import slugify
|
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-05-16 15:11:07 +00:00
|
|
|
from django.core.urlresolvers import reverse_lazy
|
2016-05-30 19:51:17 +00:00
|
|
|
from utils.models import UUIDModel, CreatedUpdatedModel
|
2016-06-01 09:10:06 +00:00
|
|
|
from .managers import ProductQuerySet, OrderQuerySet
|
2016-05-29 12:28:47 +00:00
|
|
|
import hashlib, io, base64, qrcode
|
2016-05-31 05:43:51 +00:00
|
|
|
from decimal import Decimal
|
2016-05-31 06:19:42 +00:00
|
|
|
from datetime import timedelta
|
2016-05-25 17:13:45 +00:00
|
|
|
|
2016-04-22 20:38:44 +00:00
|
|
|
|
2016-05-10 20:20:01 +00:00
|
|
|
class Order(CreatedUpdatedModel):
|
|
|
|
|
2016-05-15 22:09:00 +00:00
|
|
|
class Meta:
|
|
|
|
unique_together = ('user', 'open')
|
2016-05-29 16:02:12 +00:00
|
|
|
ordering = ['-created']
|
2016-05-15 22:09:00 +00:00
|
|
|
|
2016-05-10 20:20:01 +00:00
|
|
|
products = models.ManyToManyField(
|
|
|
|
'shop.Product',
|
|
|
|
through='shop.OrderProductRelation'
|
|
|
|
)
|
2016-04-22 20:38:44 +00:00
|
|
|
|
2016-06-18 18:59:07 +00:00
|
|
|
user = models.ForeignKey(
|
|
|
|
'auth.User',
|
|
|
|
verbose_name=_('User'),
|
|
|
|
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-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
|
|
|
)
|
|
|
|
|
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 = [
|
2016-05-15 22:09:00 +00:00
|
|
|
CREDIT_CARD,
|
|
|
|
BLOCKCHAIN,
|
|
|
|
BANK_TRANSFER,
|
|
|
|
]
|
|
|
|
|
|
|
|
PAYMENT_METHOD_CHOICES = [
|
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_method = models.CharField(
|
|
|
|
max_length=50,
|
2016-05-15 22:09:00 +00:00
|
|
|
choices=PAYMENT_METHOD_CHOICES,
|
2016-06-04 08:01:31 +00:00
|
|
|
default='',
|
2016-05-10 15:55:54 +00:00
|
|
|
)
|
|
|
|
|
2016-06-01 09:10:06 +00:00
|
|
|
cancelled = models.BooleanField(default=False)
|
|
|
|
|
2016-06-18 21:42:58 +00:00
|
|
|
refunded = models.BooleanField(
|
|
|
|
verbose_name=_('Refunded?'),
|
|
|
|
help_text=_('Whether this order has been refunded.'),
|
|
|
|
default=False,
|
|
|
|
)
|
|
|
|
|
2016-06-01 09:10:06 +00:00
|
|
|
objects = OrderQuerySet.as_manager()
|
|
|
|
|
2016-05-30 15:32:53 +00:00
|
|
|
def __str__(self):
|
|
|
|
return 'order id #%s' % self.pk
|
|
|
|
|
2016-05-14 17:39:34 +00:00
|
|
|
def get_number_of_items(self):
|
|
|
|
return self.products.aggregate(
|
|
|
|
sum=Sum('orderproductrelation__quantity')
|
|
|
|
)['sum']
|
|
|
|
|
2016-05-16 14:14:16 +00:00
|
|
|
@property
|
|
|
|
def vat(self):
|
2016-05-31 05:47:18 +00:00
|
|
|
return Decimal(self.total*Decimal(0.2))
|
2016-05-16 14:14:16 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def total(self):
|
2016-06-04 07:49:22 +00:00
|
|
|
if self.products.all():
|
|
|
|
return Decimal(self.products.aggregate(
|
|
|
|
sum=Sum(
|
|
|
|
models.F('orderproductrelation__product__price') *
|
|
|
|
models.F('orderproductrelation__quantity'),
|
|
|
|
output_field=models.IntegerField()
|
|
|
|
)
|
|
|
|
)['sum'])
|
|
|
|
else:
|
|
|
|
return False
|
2016-05-16 14:09:25 +00:00
|
|
|
|
2016-05-29 10:29:38 +00:00
|
|
|
def get_coinify_callback_url(self, request):
|
|
|
|
return 'https://' + request.get_host() + str(reverse_lazy('shop:coinify_callback', kwargs={'pk': self.pk}))
|
|
|
|
|
|
|
|
def get_coinify_thanks_url(self, request):
|
|
|
|
return 'https://' + request.get_host() + str(reverse_lazy('shop:coinify_thanks', kwargs={'pk': self.pk}))
|
|
|
|
|
2016-05-17 05:06:25 +00:00
|
|
|
def get_epay_accept_url(self, request):
|
2016-05-17 05:15:10 +00:00
|
|
|
return 'https://' + request.get_host() + str(reverse_lazy('shop:epay_thanks', kwargs={'pk': self.pk}))
|
2016-05-16 14:54:11 +00:00
|
|
|
|
2016-05-29 11:00:00 +00:00
|
|
|
def get_cancel_url(self, request):
|
2016-05-17 05:15:10 +00:00
|
|
|
return 'https://' + request.get_host() + str(reverse_lazy('shop:order_detail', kwargs={'pk': self.pk}))
|
2016-05-17 05:06:25 +00:00
|
|
|
|
2016-05-25 20:48:02 +00:00
|
|
|
def get_epay_callback_url(self, request):
|
|
|
|
return 'https://' + request.get_host() + str(reverse_lazy('shop:epay_callback', kwargs={'pk': self.pk}))
|
|
|
|
|
2016-05-17 05:06:25 +00:00
|
|
|
@property
|
|
|
|
def description(self):
|
2016-05-29 12:28:47 +00:00
|
|
|
return "BornHack %s order #%s" % (self.camp.start.year, self.pk)
|
2016-05-06 20:33:59 +00:00
|
|
|
|
2016-05-17 05:21:22 +00:00
|
|
|
def get_absolute_url(self):
|
|
|
|
return str(reverse_lazy('shop:order_detail', kwargs={'pk': self.pk}))
|
|
|
|
|
2016-05-25 18:05:31 +00:00
|
|
|
def mark_as_paid(self):
|
|
|
|
self.paid = True
|
2016-06-03 19:31:12 +00:00
|
|
|
self.open = None
|
2016-05-25 18:05:31 +00:00
|
|
|
for order_product in self.orderproductrelation_set.all():
|
|
|
|
category_pk = str(order_product.product.category.pk)
|
|
|
|
if category_pk == settings.TICKET_CATEGORY_ID:
|
|
|
|
for _ in range(0, order_product.quantity):
|
|
|
|
ticket = Ticket(
|
|
|
|
order=self,
|
|
|
|
product=order_product.product,
|
|
|
|
)
|
|
|
|
ticket.save()
|
|
|
|
self.save()
|
|
|
|
|
2016-06-19 06:49:30 +00:00
|
|
|
def mark_as_refunded(self, request):
|
2016-06-19 06:38:43 +00:00
|
|
|
if not self.paid:
|
2016-06-19 06:49:30 +00:00
|
|
|
messages.error(request, "Order %s is not paid, so cannot mark it as refunded!" % self.pk)
|
2016-06-19 06:38:43 +00:00
|
|
|
else:
|
|
|
|
self.refunded=True
|
|
|
|
### delete any tickets related to this order
|
2016-06-19 06:58:26 +00:00
|
|
|
if self.tickets.all():
|
|
|
|
messages.success(request, "Order %s marked as refunded, deleting %s tickets..." % (self.pk, self.tickets.count()))
|
|
|
|
self.tickets.all().delete()
|
|
|
|
else:
|
|
|
|
messages.success(request, "Order %s marked as refunded, no tickets to delete" % self.pk)
|
2016-06-19 06:38:43 +00:00
|
|
|
self.save()
|
2016-06-18 21:42:58 +00:00
|
|
|
|
2016-05-29 12:28:47 +00:00
|
|
|
def is_not_handed_out(self):
|
|
|
|
if self.orderproductrelation_set.filter(handed_out=True).count() == 0:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
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:
|
|
|
|
# some products are handed out, others are not
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def is_fully_handed_out(self):
|
|
|
|
if self.orderproductrelation_set.filter(handed_out=False).count() == 0:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
@property
|
|
|
|
def handed_out_status(self):
|
|
|
|
if self.is_not_handed_out():
|
|
|
|
return "no"
|
|
|
|
elif self.is_partially_handed_out():
|
|
|
|
return "partially"
|
|
|
|
elif self.is_fully_handed_out():
|
|
|
|
return "fully"
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
2016-06-01 09:10:06 +00:00
|
|
|
def mark_as_cancelled(self):
|
|
|
|
self.cancelled = True
|
|
|
|
self.open = None
|
|
|
|
self.save()
|
|
|
|
|
2016-05-25 18:05:31 +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)
|
2016-05-15 22:09:00 +00:00
|
|
|
slug = models.SlugField()
|
2016-05-17 18:56:11 +00:00
|
|
|
public = models.BooleanField(default=True)
|
2016-05-10 20:20:01 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
2016-05-10 15:55:54 +00:00
|
|
|
|
2016-05-15 22:09:00 +00:00
|
|
|
def save(self, **kwargs):
|
|
|
|
self.slug = slugify(self.name)
|
|
|
|
super(ProductCategory, self).save(**kwargs)
|
|
|
|
|
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-06-01 16:42:17 +00:00
|
|
|
ordering = ['available_in', 'price']
|
2016-04-22 20:38:44 +00:00
|
|
|
|
2016-05-15 22:09:00 +00:00
|
|
|
category = models.ForeignKey(
|
|
|
|
'shop.ProductCategory',
|
|
|
|
related_name='products'
|
|
|
|
)
|
2016-04-22 20:38:44 +00:00
|
|
|
|
2016-05-10 20:20:01 +00:00
|
|
|
name = models.CharField(max_length=150)
|
2016-05-15 22:09:00 +00:00
|
|
|
slug = models.SlugField()
|
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-17 13:09:31 +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-16 13:25:12 +00:00
|
|
|
class OrderProductRelation(CreatedUpdatedModel):
|
2016-05-10 20:20:01 +00:00
|
|
|
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):
|
2016-05-31 06:01:55 +00:00
|
|
|
return Decimal(self.product.price * self.quantity)
|
2016-05-15 22:09:00 +00:00
|
|
|
|
2016-05-10 20:20:01 +00:00
|
|
|
|
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-17 06:04:53 +00:00
|
|
|
ordering = ['-created']
|
2016-05-11 06:37:39 +00:00
|
|
|
|
2016-05-10 15:55:54 +00:00
|
|
|
payload = JSONField()
|
2016-05-17 05:59:42 +00:00
|
|
|
md5valid = models.BooleanField(default=False)
|
2016-05-10 15:55:54 +00:00
|
|
|
|
2016-05-17 06:04:53 +00:00
|
|
|
def __str__(self):
|
2016-05-17 06:08:30 +00:00
|
|
|
return 'callback at %s (md5 valid: %s)' % (self.created, self.md5valid)
|
2016-05-17 06:04:53 +00:00
|
|
|
|
2016-05-10 15:55:54 +00:00
|
|
|
|
|
|
|
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()
|
2016-05-16 19:45:34 +00:00
|
|
|
|
2016-05-17 13:09:40 +00:00
|
|
|
|
2016-06-18 18:51:53 +00:00
|
|
|
class CreditNote(CreatedUpdatedModel):
|
2016-06-18 18:59:07 +00:00
|
|
|
amount = models.DecimalField(max_digits=10, decimal_places=2)
|
2016-06-18 18:51:53 +00:00
|
|
|
text = models.TextField()
|
|
|
|
pdf = models.FileField(
|
|
|
|
null=True,
|
|
|
|
blank=True,
|
|
|
|
upload_to='creditnotes/'
|
|
|
|
)
|
|
|
|
user = models.ForeignKey(
|
|
|
|
'auth.User',
|
|
|
|
verbose_name=_('User'),
|
|
|
|
help_text=_('The user this credit note belongs to.'),
|
|
|
|
related_name='creditnotes',
|
|
|
|
)
|
|
|
|
paid = models.BooleanField(
|
|
|
|
verbose_name=_('Paid?'),
|
|
|
|
help_text=_('Whether this creditnote has been paid.'),
|
|
|
|
default=False,
|
|
|
|
)
|
|
|
|
sent_to_customer = models.BooleanField(default=False)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
return 'creditnote#%s - %s DKK (sent to %s: %s)' % (
|
|
|
|
self.id,
|
|
|
|
self.amount,
|
|
|
|
self.user.email,
|
|
|
|
self.sent_to_customer,
|
|
|
|
)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def vat(self):
|
2016-06-18 19:13:27 +00:00
|
|
|
return Decimal(self.amount*Decimal(0.2))
|
2016-06-18 18:51:53 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def filename(self):
|
|
|
|
return 'bornhack_creditnote_%s.pdf' % self.pk
|
|
|
|
|
2016-06-18 18:59:07 +00:00
|
|
|
|
2016-05-17 13:09:40 +00:00
|
|
|
class Invoice(CreatedUpdatedModel):
|
|
|
|
order = models.OneToOneField('shop.Order')
|
2016-05-30 18:15:35 +00:00
|
|
|
pdf = models.FileField(null=True, blank=True, upload_to='invoices/')
|
2016-05-17 13:09:40 +00:00
|
|
|
sent_to_customer = models.BooleanField(default=False)
|
|
|
|
|
|
|
|
def __str__(self):
|
2016-06-18 18:51:53 +00:00
|
|
|
return 'invoice#%s - order %s - %s - total %s DKK (sent to %s: %s)' % (
|
2016-05-17 13:09:40 +00:00
|
|
|
self.id,
|
|
|
|
self.order.id,
|
|
|
|
self.order.created,
|
|
|
|
self.order.total,
|
2016-06-18 18:51:53 +00:00
|
|
|
self.order.user.email,
|
2016-05-17 13:09:40 +00:00
|
|
|
self.sent_to_customer,
|
|
|
|
)
|
|
|
|
|
2016-05-30 14:58:55 +00:00
|
|
|
@property
|
|
|
|
def filename(self):
|
|
|
|
return 'bornhack_invoice_%s.pdf' % self.pk
|
|
|
|
|
2016-05-31 05:42:01 +00:00
|
|
|
def regretdate(self):
|
2016-05-31 06:17:14 +00:00
|
|
|
return self.created+timedelta(days=15)
|
2016-05-25 20:48:02 +00:00
|
|
|
|
2016-05-31 06:01:55 +00:00
|
|
|
|
2016-05-25 20:48:02 +00:00
|
|
|
class CoinifyAPIInvoice(CreatedUpdatedModel):
|
|
|
|
invoicejson = JSONField()
|
|
|
|
order = models.OneToOneField('shop.Order')
|
|
|
|
|
2016-05-31 20:22:17 +00:00
|
|
|
def __str__(self):
|
|
|
|
return "coinifyinvoice for order #%s" % self.order.id
|
|
|
|
|
|
|
|
|
2016-05-29 17:33:26 +00:00
|
|
|
class CoinifyAPICallback(CreatedUpdatedModel):
|
2016-05-29 11:20:51 +00:00
|
|
|
headers = JSONField()
|
2016-05-25 20:48:02 +00:00
|
|
|
payload = JSONField()
|
2016-05-29 08:43:38 +00:00
|
|
|
order = models.ForeignKey('shop.Order')
|
2016-05-29 11:20:51 +00:00
|
|
|
valid = models.BooleanField(default=False)
|
2016-05-25 20:48:02 +00:00
|
|
|
|
|
|
|
def __str__(self):
|
2016-05-29 17:55:07 +00:00
|
|
|
return 'order #%s callback at %s' % (self.order.id, self.created)
|
2016-05-25 20:48:02 +00:00
|
|
|
|
2016-05-25 20:53:02 +00:00
|
|
|
|
2016-05-25 17:13:45 +00:00
|
|
|
class Ticket(CreatedUpdatedModel, UUIDModel):
|
2016-05-30 22:58:11 +00:00
|
|
|
order = models.ForeignKey('shop.Order', related_name='tickets')
|
|
|
|
product = models.ForeignKey('shop.Product', related_name='tickets')
|
2016-05-25 17:13:45 +00:00
|
|
|
qrcode_base64 = models.TextField(null=True, blank=True)
|
|
|
|
|
|
|
|
name = models.CharField(
|
|
|
|
max_length=100,
|
|
|
|
help_text=(
|
|
|
|
'Name of the person this ticket belongs to. '
|
|
|
|
'This can be different from the buying user.'
|
|
|
|
),
|
2016-05-30 23:02:47 +00:00
|
|
|
null=True,
|
|
|
|
blank=True,
|
2016-05-25 17:13:45 +00:00
|
|
|
)
|
|
|
|
|
2016-05-30 22:58:11 +00:00
|
|
|
email = models.EmailField(
|
|
|
|
null=True,
|
|
|
|
blank=True,
|
|
|
|
)
|
|
|
|
|
2016-05-25 17:13:45 +00:00
|
|
|
def __str__(self):
|
|
|
|
return 'Ticket {user} {product}'.format(
|
|
|
|
user=self.order.user,
|
|
|
|
product=self.product
|
|
|
|
)
|
|
|
|
|
|
|
|
def save(self, **kwargs):
|
|
|
|
super(Ticket, self).save(**kwargs)
|
|
|
|
self.qrcode_base64 = self.get_qr_code()
|
|
|
|
super(Ticket, self).save(**kwargs)
|
|
|
|
|
|
|
|
def get_token(self):
|
|
|
|
return hashlib.sha256(
|
|
|
|
'{ticket_id}{user_id}{secret_key}'.format(
|
|
|
|
ticket_id=self.pk,
|
|
|
|
user_id=self.order.user.pk,
|
|
|
|
secret_key=settings.SECRET_KEY,
|
|
|
|
)
|
|
|
|
).hexdigest()
|
|
|
|
|
|
|
|
def get_qr_code(self):
|
2016-05-30 22:58:11 +00:00
|
|
|
qr = qrcode.make(
|
|
|
|
self.get_token(),
|
|
|
|
version=1,
|
|
|
|
error_correction=qrcode.constants.ERROR_CORRECT_H
|
|
|
|
).resize((250,250))
|
2016-05-25 17:13:45 +00:00
|
|
|
file_like = io.BytesIO()
|
2016-05-30 22:58:11 +00:00
|
|
|
qr.save(file_like, format='png')
|
2016-05-25 17:13:45 +00:00
|
|
|
qrcode_base64 = base64.b64encode(file_like.getvalue())
|
|
|
|
return qrcode_base64
|
|
|
|
|
|
|
|
def get_qr_code_url(self):
|
|
|
|
return 'data:image/png;base64,{}'.format(self.qrcode_base64)
|
|
|
|
|
2016-05-30 22:58:11 +00:00
|
|
|
def get_absolute_url(self):
|
|
|
|
return str(reverse_lazy('shop:ticket_detail', kwargs={'pk': self.pk}))
|
2016-06-01 16:42:17 +00:00
|
|
|
|