Skip to content

Commit

Permalink
feat: add an assignments SAP model
Browse files Browse the repository at this point in the history
  • Loading branch information
iloveagent57 committed Aug 28, 2023
1 parent 2c4e315 commit e97af2f
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 0 deletions.
41 changes: 41 additions & 0 deletions enterprise_access/apps/subsidy_access_policy/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,44 @@ def per_learner_spend_limit_dollars(self, obj):
if obj.per_learner_spend_limit is None:
return None
return cents_to_usd_string(obj.per_learner_spend_limit)


@admin.register(models.AssignedLearnerCreditAccessPolicy)
class LearnerContentAssignmentAccessPolicy(DjangoQLSearchMixin, BaseSubsidyAccessPolicyMixin):
"""
Admin configuration for AssignedLearnerCreditAccessPolicy.
"""
search_fields = (
'uuid',
'display_name',
'enterprise_customer_uuid',
'catalog_uuid',
'subsidy_uuid',
)

fieldsets = [
(
'Base configuration',
{
'fields': [
'enterprise_customer_uuid',
'display_name',
'description',
'active',
'catalog_uuid',
'subsidy_uuid',
'created',
'modified',
]
}
),
(
'Spend limits',
{
'fields': [
'spend_limit',
'policy_spend_limit_dollars',
]
}
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Generated by Django 3.2.20 on 2023-08-28 13:25

from django.db import migrations
import enterprise_access.apps.subsidy_access_policy.models


class Migration(migrations.Migration):

dependencies = [
('subsidy_access_policy', '0014_add_policy_display_name'),
]

operations = [
migrations.CreateModel(
name='AssignedLearnerCreditAccessPolicy',
fields=[
],
options={
'proxy': True,
'indexes': [],
'constraints': [],
},
bases=(enterprise_access.apps.subsidy_access_policy.models.CreditPolicyMixin, 'subsidy_access_policy.subsidyaccesspolicy'),
),
migrations.CreateModel(
name='HistoricalAssignedLearnerCreditAccessPolicy',
fields=[
],
options={
'verbose_name': 'historical assigned learner credit access policy',
'ordering': ('-history_date', '-history_id'),
'get_latest_by': 'history_date',
'proxy': True,
'indexes': [],
'constraints': [],
},
bases=('subsidy_access_policy.historicalsubsidyaccesspolicy',),
),
]
39 changes: 39 additions & 0 deletions enterprise_access/apps/subsidy_access_policy/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import requests
from django.conf import settings
from django.core.cache import cache as django_cache
from django.core.exceptions import ValidationError
from django.db import models
from django_extensions.db.models import TimeStampedModel
from edx_django_utils.cache.utils import get_cache_key
Expand Down Expand Up @@ -770,3 +771,41 @@ def remaining_balance_per_user(self, lms_user_id=None):
"""
spent_amount = self.transactions_for_learner(lms_user_id)['aggregates'].get('total_quantity') or 0
return self.per_learner_spend_limit - spent_amount


class AssignedLearnerCreditAccessPolicy(CreditPolicyMixin, SubsidyAccessPolicy):
"""
Policy based on LearnerContentAssignments, backed by a learner credit type of subsidy.
.. no_pii: This model has no PII
"""
objects = PolicyManager()

class Meta:
""" Meta class for this policy type. """
proxy = True

def clean(self):
"""
Policies of this type must have a defined spend_limit,
and they can *not* define either of the per-learner limits.
"""
if self.spend_limit is None:
raise ValidationError(f'{self} must define a spend_limit.')
if self.per_learner_spend_limit is not None:
raise ValidationError(f'{self} must not define a per-learner spend limit.')
if self.per_learner_enrollment_limit is not None:
raise ValidationError(f'{self} must not define a per-learner enrollment limit.')

def save(self, *args, **kwargs):
"""
This type of policy must always have an access_method of "assigned".
"""
self.access_method = AccessMethods.ASSIGNED
super().save(*args, **kwargs)

def can_redeem(self, lms_user_id, content_key, skip_customer_user_check=False):
raise NotImplementedError

def redeem(self, lms_user_id, content_key, all_transactions, metadata=None):
raise NotImplementedError

0 comments on commit e97af2f

Please sign in to comment.