diff --git a/enterprise_access/apps/subsidy_access_policy/admin/__init__.py b/enterprise_access/apps/subsidy_access_policy/admin/__init__.py index 1d688c76..08d27766 100644 --- a/enterprise_access/apps/subsidy_access_policy/admin/__init__.py +++ b/enterprise_access/apps/subsidy_access_policy/admin/__init__.py @@ -26,7 +26,8 @@ SubsidyAccessPolicySetLateRedemptionView ) -from .forms import ForcedPolicyRedemptionForm +from .forms import ForcedPolicyRedemptionForm, SubsidyAccessPolicyForm + logger = logging.getLogger(__name__) @@ -199,6 +200,8 @@ class PerLearnerEnrollmentCreditAccessPolicy(DjangoQLSearchMixin, BaseSubsidyAcc """ Admin configuration for PerLearnerEnrollmentCreditAccessPolicy. """ + form = SubsidyAccessPolicyForm + list_display = BaseSubsidyAccessPolicyMixin.list_display + ( 'per_learner_enrollment_limit', ) @@ -250,6 +253,8 @@ class PerLearnerSpendCreditAccessPolicy(DjangoQLSearchMixin, BaseSubsidyAccessPo """ Admin configuration for PerLearnerSpendCreditAccessPolicy. """ + form = SubsidyAccessPolicyForm + list_display = BaseSubsidyAccessPolicyMixin.list_display + ( 'per_learner_spend_limit_dollars', ) @@ -313,6 +318,8 @@ class LearnerContentAssignmentAccessPolicy(DjangoQLSearchMixin, BaseSubsidyAcces """ Admin configuration for AssignedLearnerCreditAccessPolicy. """ + form = SubsidyAccessPolicyForm + search_fields = ( 'uuid', 'display_name', diff --git a/enterprise_access/apps/subsidy_access_policy/admin/forms.py b/enterprise_access/apps/subsidy_access_policy/admin/forms.py index b202adf5..6202483a 100644 --- a/enterprise_access/apps/subsidy_access_policy/admin/forms.py +++ b/enterprise_access/apps/subsidy_access_policy/admin/forms.py @@ -1,11 +1,15 @@ """ Forms to be used for subsidy_access_policy django admin. """ +import requests from django import forms from django.conf import settings +from django.core.exceptions import ValidationError from django.utils.translation import gettext as _ -from ..models import ForcedPolicyRedemption +from enterprise_access.apps.subsidy_access_policy.utils import get_versioned_subsidy_client + +from ..models import ForcedPolicyRedemption, SubsidyAccessPolicy class LateRedemptionDaysFromNowChoices: @@ -92,3 +96,27 @@ class ForcedPolicyRedemptionForm(forms.ModelForm): class Meta: model = ForcedPolicyRedemption fields = '__all__' + + +class SubsidyAccessPolicyForm(forms.ModelForm): + def clean_subsidy_uuid(self): + """ + Validate that the subsidy exists and is assigned to the same enterprise customer as the budget. + """ + # 1. check if the subsidy_uuid actually exists + # 2. subsidy is assigned to the same enterprise customer as the budget + # if any of these checks fail, raise a ValidationError + client = get_versioned_subsidy_client(version=1) + try: + subsidy = client.retrieve_subsidy(self.cleaned_data["subsidy_uuid"]) + except requests.exceptions.HTTPError as exc: + raise ValidationError("Subsidy does not exist") + + if str(subsidy["enterprise_customer_uuid"]) != str(self.cleaned_data["enterprise_customer_uuid"]): + raise ValidationError("Subsidy is not assigned to the same enterprise customer as the budget") + + return self.cleaned_data["subsidy_uuid"] + + class Meta: + model = SubsidyAccessPolicy + fields = '__all__'