Skip to content

Commit

Permalink
accounts for email in different case in group buy
Browse files Browse the repository at this point in the history
`bulk_items` in `execute_order` would return an empty set if the profile
email in the database had a different case than the one in the `Cart.sync_on`
field, leading to the else condition and a MultipleObject exception.

Note `translate_descr_suffix` was also updated such that the group buy
benefiary doesn't get dropped in translation.
  • Loading branch information
smirolo committed May 1, 2024
1 parent f552117 commit 34813d2
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 20 deletions.
29 changes: 18 additions & 11 deletions saas/cart.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ def cart_insert_item(request, **kwargs):
filter_args = None
if sync_on:
if filter_args:
filter_args |= Q(sync_on=sync_on)
filter_args |= Q(sync_on__iexact=sync_on)
else:
filter_args = Q(sync_on=sync_on)
filter_args = Q(sync_on__iexact=sync_on)
if option:
if filter_args:
filter_args |= Q(option=option)
Expand All @@ -95,7 +95,8 @@ def cart_insert_item(request, **kwargs):
use = template_item.use
# Can we use that template?
if sync_on:
if template_item.sync_on and template_item.sync_on != sync_on:
if (template_item.sync_on and
template_item.sync_on.lower() != sync_on.lower()):
# conflicting sync_on. we cannot use the template.
template_item = None
if template_item and option:
Expand Down Expand Up @@ -147,11 +148,14 @@ def cart_insert_item(request, **kwargs):
continue
if sync_on:
item_sync_on = item.get('sync_on')
if item_sync_on and item_sync_on == sync_on:
if (item_sync_on and
item_sync_on.lower() == sync_on.lower()):
if not template_item:
template_item = item
continue
if template_item.get('sync_on') != sync_on:
template_sync_on = template_item.get('sync_on')
if (template_sync_on and
template_sync_on.lower() != sync_on.lower()):
# The item matches on sync_on but the template
# does not.
template_item = item
Expand All @@ -176,10 +180,12 @@ def cart_insert_item(request, **kwargs):
if template_item.get('use') != use:
template_item = item
continue
if (template_item and
template_item.get('sync_on') == sync_on):
# We already have a template matching on `sync_on`.
continue
if template_item:
template_sync_on = template_item.get('sync_on')
if (template_sync_on and
template_sync_on.lower() == sync_on.lower()):
# We already have a template matching on `sync_on`.
continue
# Couldn't match on `sync_on`. next is `option`.
if option:
item_option = item.get('option')
Expand Down Expand Up @@ -227,7 +233,8 @@ def cart_insert_item(request, **kwargs):
# Can we use that template?
if sync_on:
template_sync_on = template_item.get('sync_on')
if template_sync_on and template_sync_on != sync_on:
if (template_sync_on and
template_sync_on.lower() != sync_on.lower()):
# conflicting sync_on. we cannot use the template.
template_item = None
if template_item and option:
Expand Down Expand Up @@ -311,7 +318,7 @@ def session_cart_to_database(request):
email != cart_item.email)):
continue
if (sync_on and (not cart_item.sync_on or
sync_on != cart_item.sync_on)):
sync_on.lower() != cart_item.sync_on.lower())):
continue
# We found a `CartItem` in the database that was can be
# further constrained by the cookie session item.
Expand Down
10 changes: 5 additions & 5 deletions saas/humanize.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,9 @@ def translate_descr_suffix(descr):
pat = r"(a %(percent)s discount)?( and )?"\
r"(%(nb_periods)s %(period_name)s free)?( and )?"\
r"(a %(amount)s off)?"\
r"( \(code: %(code)s\))?"\
r"(, complimentary of %(payer)s)?"\
r"(\s*for %(subscriber_full_name)s \(%(sync_on)s\))?"\
r"( \(code: %(code)s\))?" % REGEXES
r"(\s*for %(subscriber_full_name)s \(%(sync_on)s\))?" % REGEXES
look = re.match(pat, descr_suffix)
if look:
descr_suffix = ""
Expand All @@ -291,6 +291,9 @@ def translate_descr_suffix(descr):
descr_suffix += sep + _(DESCRIBE_SUFFIX_DISCOUNT_CURRENCY) % {
'amount': amount}
sep = _(" and ")
code = look.group('code')
if code:
descr_suffix += _(DESCRIBE_SUFFIX_COUPON_APPLIED) % {'code': code}
payer = look.group('payer')
if payer:
descr_suffix += _(", complimentary of %(payer)s") % {'payer': payer}
Expand All @@ -301,9 +304,6 @@ def translate_descr_suffix(descr):
'subscriber_full_name': subscriber_full_name,
'sync_on': sync_on
}
code = look.group('code')
if code:
descr_suffix += _(DESCRIBE_SUFFIX_COUPON_APPLIED) % {'code': code}

if descr_suffix:
descr += " - %s" % descr_suffix
Expand Down
12 changes: 8 additions & 4 deletions saas/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ def execute_order(self, invoicables, user):
if cart_items.exists():
# We are doing a groupBuy for a specified email.
bulk_items = cart_items.filter(
sync_on=subscription.organization.email)
sync_on__iexact=subscription.organization.email)
if bulk_items.exists():
cart_item = bulk_items.get()
else:
Expand Down Expand Up @@ -2357,9 +2357,13 @@ def to_sync(self, user):
Returns charges which have been paid and a 3rd party asked
to be notified about.
"""
results = self.filter(Q(sync_on=user.username) | Q(sync_on=user.email)
| Q(sync_on__in=get_organization_model().objects.accessible_by(user).values(
'slug').distinct()),
results = self.filter(Q(sync_on__iexact=user.email) |
# We don't do case-insensitive search here (i.e. `sync_on__in`)
# because we assume the case for sync_on field and username/slug
# are in sync here.
Q(sync_on=user.username) |
Q(sync_on__in=get_organization_model().objects.accessible_by(
user).values('slug').distinct()),
charge__state=Charge.DONE, invoice_key__isnull=False)
return results

Expand Down

0 comments on commit 34813d2

Please sign in to comment.