From 2e2597226b3d26e94cdc995261dbd092ac192913 Mon Sep 17 00:00:00 2001 From: Brian Beggs Date: Thu, 29 Feb 2024 16:15:49 -0500 Subject: [PATCH] chore: Suggest a fix for ordering access policies so assignments are prioritized. --- .../apps/subsidy_access_policy/constants.py | 5 +-- .../apps/subsidy_access_policy/models.py | 31 +++++++++++++++++-- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/enterprise_access/apps/subsidy_access_policy/constants.py b/enterprise_access/apps/subsidy_access_policy/constants.py index 8ed3348a..8600258a 100644 --- a/enterprise_access/apps/subsidy_access_policy/constants.py +++ b/enterprise_access/apps/subsidy_access_policy/constants.py @@ -30,8 +30,9 @@ class SegmentEvents: # Configure the priority of each policy type here. When given multiple redeemable policies to select for redemption, # the policy resolution engine will select policies with the lowest priority number. -CREDIT_POLICY_TYPE_PRIORITY = 1 -SUBSCRIPTION_POLICY_TYPE_PRIORITY = 2 +# BB - Some padding between the larger policy type grouping... +CREDIT_POLICY_TYPE_PRIORITY = 10 +SUBSCRIPTION_POLICY_TYPE_PRIORITY = 20 class PolicyTypes: diff --git a/enterprise_access/apps/subsidy_access_policy/models.py b/enterprise_access/apps/subsidy_access_policy/models.py index 2da80f21..7a22337f 100644 --- a/enterprise_access/apps/subsidy_access_policy/models.py +++ b/enterprise_access/apps/subsidy_access_policy/models.py @@ -931,6 +931,7 @@ def __str__(self): class CreditPolicyMixin: """ Mixin class for credit type policies. + BB - Something in the docs about how to use this? Potentially remove? """ @property @@ -938,7 +939,7 @@ def priority(self): return CREDIT_POLICY_TYPE_PRIORITY -class PerLearnerEnrollmentCreditAccessPolicy(CreditPolicyMixin, SubsidyAccessPolicy): +class PerLearnerEnrollmentCreditAccessPolicy(SubsidyAccessPolicy): """ Policy that limits the number of enrollments transactions for a learner in a subsidy. @@ -1007,8 +1008,16 @@ def remaining_balance_per_user(self, lms_user_id): existing_transaction_count = len(self.transactions_for_learner(lms_user_id)['transactions']) return self.per_learner_enrollment_limit - existing_transaction_count + @property + def priority(self): + """ + Ensure that Per learner enrollment policies are evaluated before spend cap but after assignment + based policy types. + """ + return CREDIT_POLICY_TYPE_PRIORITY + 1 -class PerLearnerSpendCreditAccessPolicy(CreditPolicyMixin, SubsidyAccessPolicy): + +class PerLearnerSpendCreditAccessPolicy(SubsidyAccessPolicy): """ Policy that limits the amount of learner spend for enrollment transactions. @@ -1089,8 +1098,15 @@ def remaining_balance_per_user(self, lms_user_id=None): positive_spent_amount = spent_amount * -1 return self.per_learner_spend_limit - positive_spent_amount + @property + def priority(self): + """ + Ensure that Per leaner spend policies are evaluated last, after other similar policy types. + """ + return CREDIT_POLICY_TYPE_PRIORITY + 2 + -class AssignedLearnerCreditAccessPolicy(CreditPolicyMixin, SubsidyAccessPolicy): +class AssignedLearnerCreditAccessPolicy(SubsidyAccessPolicy): """ Policy based on LearnerContentAssignments, backed by a learner credit type of subsidy. @@ -1427,6 +1443,7 @@ def allocate(self, learner_emails, content_key, content_price_cents): content_price_cents, ) +<<<<<<< Updated upstream class PolicyGroupAssociation(TimeStampedModel): """ @@ -1455,3 +1472,11 @@ class Meta: null=False, help_text='The uuid that uniquely identifies the associated group.', ) +======= + @property + def priority(self): + """ + Ensure that assignment policies are evaluated before other similar policy types + """ + return CREDIT_POLICY_TYPE_PRIORITY + 0 +>>>>>>> Stashed changes