def get_context_data(self, **kwargs): """ Return a dictionary primarily of forms to for template rendering. Also includes a value for the "I have a car" checkbox. Outputs three types of forms: - Bound forms, if POSTed - Empty forms if GET, and no stored Participant data - Filled forms if GET, and Participant data exists Forms are bound to model instances for UPDATE if such instances exist. """ post = self.request.POST if self.request.method == "POST" else None participant = self.participant # Access other models within participant car = participant and participant.car e_info = participant and participant.emergency_info e_contact = e_info and e_info.emergency_contact # If no Participant object, fill at least with User email par_kwargs = self.prefix("participant", instance=participant) par_kwargs["user"] = self.user if not participant: par_kwargs["initial"] = {'email': self.user.email} elif participant.affiliation_dated or not participant.info_current: # Nulling this out forces the user to consciously choose an accurate value # (Only null out the field if it's the user editing their own profile, though) if self.request.participant == participant: par_kwargs["initial"] = {'affiliation': None} context = { 'currently_has_car': bool(car), 'participant_form': forms.ParticipantForm(post, **par_kwargs), 'car_form': forms.CarForm(post, instance=car, **self.prefix('car')), 'emergency_info_form': forms.EmergencyInfoForm(post, instance=e_info, **self.prefix('einfo')), 'emergency_contact_form': forms.EmergencyContactForm(post, instance=e_contact, **self.prefix('econtact')), } # Boolean: Already responded to question. # None: has not responded yet if post: context['has_car_checked'] = self.has_car elif participant: context['has_car_checked'] = bool(participant.car) else: context['has_car_checked'] = None return context
def test_non_mit_affiliation(self): """It's valid to have a non-MIT email address with non-MIT affiliations.""" user = factories.UserFactory.create() EmailAddress(user_id=user.pk, email='*****@*****.**', primary=True, verified=True).save() form = forms.ParticipantForm( data={ 'name': "Some User", 'email': "*****@*****.**", 'cell_phone': '', 'affiliation': affiliations.NON_AFFILIATE.CODE, }, user=user, ) self.assertTrue(form.is_valid())
def test_mit_affiliation_without_mit_email(self): """You must have an MIT email address to be an MIT student.""" user = factories.UserFactory.create() EmailAddress( user_id=user.pk, email='*****@*****.**', primary=True, verified=True, ).save() form = forms.ParticipantForm( user=user, data={ 'name': "Some User", 'email': "*****@*****.**", 'cell_phone': '', 'affiliation': affiliations.MIT_UNDERGRAD.CODE, }, ) self.assertFalse(form.is_valid())
def get_context_data(self, **kwargs): """Return a dictionary primarily of forms to for template rendering. Also includes a value for the "I have a car" checkbox. Outputs three types of forms: - Bound forms, if POSTed - Empty forms if GET, and no stored Participant data - Filled forms if GET, and Participant data exists Forms are bound to model instances for UPDATE if such instances exist. """ post = self.request.POST if self.request.method == "POST" else None participant = self.participant # Access other models within participant car = participant and participant.car e_info = participant and participant.emergency_info e_contact = e_info and e_info.emergency_contact par_kwargs = { 'prefix': 'participant', 'instance': participant, 'user': self.user, } if not participant: par_kwargs["initial"] = {'email': self.user.email} elif participant.affiliation_dated or not participant.info_current: # Nulling this out forces the user to consciously choose an accurate value # (Only null out the field if it's the user editing their own profile, though) if self.request.participant == participant: par_kwargs["initial"] = {'affiliation': None} verified_mit_emails = self.user.emailaddress_set.filter( verified=True, email__iendswith='mit.edu' ) context = { # For everybody but an admin, `participant` is just `viewing_participant` 'participant': participant, 'medical_info_scrubbed': bool( participant and e_info and not e_info.allergies ), 'has_mit_email': verified_mit_emails.exists(), 'currently_has_car': bool(car), 'participant_form': forms.ParticipantForm(post, **par_kwargs), 'car_form': forms.CarForm(post, instance=car, prefix='car'), 'emergency_info_form': forms.EmergencyInfoForm( post, instance=e_info, prefix='einfo' ), 'emergency_contact_form': forms.EmergencyContactForm( post, instance=e_contact, prefix='econtact' ), } # Boolean: Already responded to question. # None: has not responded yet if post: context['has_car_checked'] = self.has_car elif participant: context['has_car_checked'] = bool(participant.car) else: context['has_car_checked'] = None return context