Coverage for benefits/core/mixins.py: 100%
31 statements
« prev ^ index » next coverage.py v7.9.1, created at 2025-07-02 21:44 +0000
« prev ^ index » next coverage.py v7.9.1, created at 2025-07-02 21:44 +0000
1import logging
3from django.conf import settings
5from . import recaptcha, session
6from .middleware import user_error
8logger = logging.getLogger(__name__)
11class AgencySessionRequiredMixin:
12 """Mixin intended for use with a class-based view as the first in the MRO.
14 Gets the active `TransitAgency` out of session and sets an attribute on `self`.
16 If the session is not configured with an agency, return a user error.
17 """
19 def dispatch(self, request, *args, **kwargs):
20 if session.active_agency(request):
21 self.agency = session.agency(request)
22 return super().dispatch(request, *args, **kwargs)
23 else:
24 logger.warning("Session not configured with an active agency")
25 return user_error(request)
28class EligibleSessionRequiredMixin:
29 """Mixin intended for use with a class-based view as the first in the MRO.
31 If the session is not marked as eligible (e.g. the user has verified their eligibility), return a user error.
32 """
34 def dispatch(self, request, *args, **kwargs):
35 if session.eligible(request):
36 return super().dispatch(request, *args, **kwargs)
37 else:
38 logger.warning("Session does not have verified eligibility")
39 return user_error(request)
42class FlowSessionRequiredMixin:
43 """Mixin intended for use with a class-based view as the first in the MRO.
45 Gets the current `EnrollmentFlow` out of session and sets an attribute on `self`.
47 If the session is not configured with a flow, return a user error.
48 """
50 def dispatch(self, request, *args, **kwargs):
51 flow = session.flow(request)
52 if flow:
53 self.flow = flow
54 return super().dispatch(request, *args, **kwargs)
55 else:
56 logger.warning("Session not configured with enrollment flow")
57 return user_error(request)
60class RecaptchaEnabledMixin:
61 """Mixin intended for use with a class-based view as the first in the MRO.
63 Configures the request with required reCAPTCHA settings."""
65 def dispatch(self, request, *args, **kwargs):
66 if settings.RECAPTCHA_ENABLED:
67 request.recaptcha = {
68 "data_field": recaptcha.DATA_FIELD,
69 "script_api": settings.RECAPTCHA_API_KEY_URL,
70 "site_key": settings.RECAPTCHA_SITE_KEY,
71 }
72 return super().dispatch(request, *args, **kwargs)