Coverage for benefits / core / admin / enrollment.py: 97%
51 statements
« prev ^ index » next coverage.py v7.13.0, created at 2025-12-22 19:08 +0000
« prev ^ index » next coverage.py v7.13.0, created at 2025-12-22 19:08 +0000
1from django import forms
2from django.core.exceptions import ValidationError
3from django.contrib import admin
5from adminsortable2.admin import SortableAdminMixin
7from benefits.core import models
8from .mixins import ProdReadOnlyPermissionMixin, StaffPermissionMixin, SuperuserPermissionMixin
11@admin.register(models.EnrollmentEvent)
12class EnrollmentEventAdmin(ProdReadOnlyPermissionMixin, admin.ModelAdmin):
13 list_display = ("enrollment_datetime", "transit_agency", "enrollment_flow", "enrollment_method", "verified_by")
14 ordering = ("-enrollment_datetime",)
17@admin.register(models.EligibilityApiVerificationRequest)
18class EligibilityApiVerificationRequestAdmin(SuperuserPermissionMixin, admin.ModelAdmin):
19 list_display = ("label", "api_url")
22class EnrollmentFlowForm(forms.ModelForm):
23 def has_field(self, field_name):
24 return self.fields.get(field_name) is not None
26 def get(self, cleaned_data, field_name):
27 """
28 If the field is present on the form, simply get the value from the cleaned_data.
30 If the field is not present on the form, that means the user doesn't have access to the field,
31 so get the value from the form's instance of the object.
32 """
33 return cleaned_data.get(field_name) if self.has_field(field_name) else getattr(self.instance, field_name)
35 def clean(self):
36 cleaned_data = super().clean()
38 field_errors = {}
39 non_field_errors = []
41 supports_expiration = cleaned_data.get("supports_expiration")
43 if supports_expiration:
44 expiration_days = cleaned_data.get("expiration_days")
45 expiration_reenrollment_days = cleaned_data.get("expiration_reenrollment_days")
47 message = "When support_expiration is True, this value must be greater than 0."
48 if expiration_days is None or expiration_days <= 0: 48 ↛ 50line 48 didn't jump to line 50 because the condition on line 48 was always true
49 field_errors.update(expiration_days=ValidationError(message))
50 if expiration_reenrollment_days is None or expiration_reenrollment_days <= 0: 50 ↛ 53line 50 didn't jump to line 53 because the condition on line 50 was always true
51 field_errors.update(expiration_reenrollment_days=ValidationError(message))
53 transit_agency = cleaned_data.get("transit_agency")
55 if transit_agency:
56 # these fields might not be on the form, so use helper method to correctly get the value
57 eligibility_api_request = self.get(cleaned_data, "api_request")
58 claims_request = self.get(cleaned_data, "claims_request")
60 if not (claims_request or eligibility_api_request):
61 message = (
62 "Must configure either claims verification or Eligibility API verification before"
63 + " adding to a transit agency."
64 )
65 non_field_errors.append(ValidationError(message))
67 for field_name, validation_error in field_errors.items():
68 self.add_error(field_name, validation_error)
69 for validation_error in non_field_errors:
70 self.add_error(None, validation_error)
73@admin.register(models.EnrollmentFlow)
74class SortableEnrollmentFlowAdmin(StaffPermissionMixin, SortableAdminMixin, admin.ModelAdmin):
75 list_display = ("label", "transit_agency", "supported_enrollment_methods")
76 form = EnrollmentFlowForm
78 def get_readonly_fields(self, request, obj=None):
79 fields = []
81 if not request.user.is_superuser:
82 fields.extend(
83 [
84 "selection_label_template_override",
85 ]
86 )
88 return fields or super().get_readonly_fields(request, obj)