Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Work-in-Progress] limit coupon use as one per profile (#280) #285

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions saas/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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``
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand Down