Skip to content

Commit

Permalink
feat: add validation on subsidy uuid
Browse files Browse the repository at this point in the history
  • Loading branch information
muhammad-ammar committed Dec 9, 2024
1 parent 24873c3 commit f717838
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
SubsidyAccessPolicySetLateRedemptionView
)

from .forms import ForcedPolicyRedemptionForm
from .forms import ForcedPolicyRedemptionForm, SubsidyAccessPolicyForm


logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -199,6 +200,8 @@ class PerLearnerEnrollmentCreditAccessPolicy(DjangoQLSearchMixin, BaseSubsidyAcc
"""
Admin configuration for PerLearnerEnrollmentCreditAccessPolicy.
"""
form = SubsidyAccessPolicyForm

list_display = BaseSubsidyAccessPolicyMixin.list_display + (
'per_learner_enrollment_limit',
)
Expand Down Expand Up @@ -250,6 +253,8 @@ class PerLearnerSpendCreditAccessPolicy(DjangoQLSearchMixin, BaseSubsidyAccessPo
"""
Admin configuration for PerLearnerSpendCreditAccessPolicy.
"""
form = SubsidyAccessPolicyForm

list_display = BaseSubsidyAccessPolicyMixin.list_display + (
'per_learner_spend_limit_dollars',
)
Expand Down Expand Up @@ -313,6 +318,8 @@ class LearnerContentAssignmentAccessPolicy(DjangoQLSearchMixin, BaseSubsidyAcces
"""
Admin configuration for AssignedLearnerCreditAccessPolicy.
"""
form = SubsidyAccessPolicyForm

search_fields = (
'uuid',
'display_name',
Expand Down
30 changes: 29 additions & 1 deletion enterprise_access/apps/subsidy_access_policy/admin/forms.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down Expand Up @@ -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__'

0 comments on commit f717838

Please sign in to comment.