diff --git a/saas/models.py b/saas/models.py index 67f73083..4554449e 100644 --- a/saas/models.py +++ b/saas/models.py @@ -812,6 +812,9 @@ def execute_order(self, invoicables, user): subscription.save() if cart_item: cart_item.recorded = True + if (cart_item.coupon and cart_item.coupon.once_per_user and + user not in cart_item.coupon.uses.all()): + cart_item.coupon.uses.add(user) cart_item.save() # At this point we have gathered all the ``Organization`` @@ -3015,6 +3018,11 @@ class Coupon(models.Model): help_text=_("Number of times the coupon can be used")) extra = get_extra_field_class()(null=True, help_text=_("Extra meta data (can be stringify JSON)")) + once_per_user = models.BooleanField(default=False, + help_text=_("Flag indicating if the coupon can be used only once per user")) + uses = models.ManyToManyField(settings.AUTH_USER_MODEL, + related_name='uses', blank=True, + help_text=_("Users who have already redeemed this coupon")) class Meta: unique_together = ('organization', 'code') @@ -3134,6 +3142,8 @@ def redeem(self, user, coupon_code, created_at=None): for item in self.get_cart(user): redeemed = Coupon.objects.active( item.plan.organization, coupon_code, at_time=at_time).first() + if redeemed and redeemed.once_per_user and user in redeemed.uses.all(): + continue if redeemed and redeemed.is_valid(item.plan, at_time=at_time): coupon_applied = True item.coupon = redeemed