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

fix: unsupported operand type for remaining balance #262

Merged
merged 1 commit into from
Sep 18, 2023
Merged
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
18 changes: 14 additions & 4 deletions enterprise_access/apps/subsidy_access_policy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -721,14 +721,18 @@ def can_redeem(self, lms_user_id, content_key, skip_customer_user_check=False):
return (True, None, existing_redemptions)

def credit_available(self, lms_user_id=None):
if self.remaining_balance_per_user(lms_user_id) > 0:
return True
return False
remaining_balance_per_user = self.remaining_balance_per_user(lms_user_id)
return (remaining_balance_per_user is not None) and remaining_balance_per_user > 0

def remaining_balance_per_user(self, lms_user_id=None):
"""
Returns the remaining redeemable credit for the user.
Returns None if `per_learner_enrollment_limit` is not set.
"""
if self.per_learner_enrollment_limit is None:
return None
if self.per_learner_enrollment_limit <= 0:
return 0
existing_transaction_count = len(self.transactions_for_learner(lms_user_id)['transactions'])
return self.per_learner_enrollment_limit - existing_transaction_count

Expand Down Expand Up @@ -777,12 +781,18 @@ def can_redeem(self, lms_user_id, content_key, skip_customer_user_check=False):
return (True, None, existing_redemptions)

def credit_available(self, lms_user_id=None):
return self.remaining_balance_per_user(lms_user_id) > 0
remaining_balance_per_user = self.remaining_balance_per_user(lms_user_id)
return (remaining_balance_per_user is not None) and remaining_balance_per_user > 0

def remaining_balance_per_user(self, lms_user_id=None):
"""
Returns the remaining redeemable credit for the user.
Returns None if `per_learner_spend_limit` is not set.
"""
if self.per_learner_spend_limit is None:
return None
if self.per_learner_spend_limit <= 0:
return 0
spent_amount = self.transactions_for_learner(lms_user_id)['aggregates'].get('total_quantity') or 0
return self.per_learner_spend_limit - spent_amount

Expand Down
Loading