def redeem(self, user=None):
if (user is None and self.redeemed) or self.accesspermissions.exists():
raise self.RedeemError('Already redeemed.')
if timezone.now() > self.valid_until + timedelta(minutes=5 if self.redeemed else 0):
raise self.RedeemError('No longer valid.')
if user:
with transaction.atomic():
if self.author_id and self.unique_key:
AccessPermission.objects.filter(author_id=self.author_id, unique_key=self.unique_key).delete()
for restriction in self.restrictions:
AccessPermission.objects.create(
user=user,
access_restriction_id=restriction.pk,
author_id=self.author_id,
expire_date=restriction.expire_date,
can_grant=self.can_grant,
unique_key=self.unique_key,
token=self if self.pk else None,
)
if self.pk and not self.unlimited:
self.redeemed = True
self.save()
评论列表
文章目录