def user_profile(request): if request.user.username == "guest": raise Http404 user = request.user profile = user.userprofile token = Token.objects.get(user=request.user) initial = {"email": user.email, "first_name": user.first_name, "last_name": user.last_name, "affiliation": profile.affiliation} active_request = AccessRequest.objects.for_user(request.user) has_access_request = False if active_request and active_request.approved: has_access_request = True if request.method == "POST": form = forms.UserEditForm(request.POST, initial=initial) if form.is_valid() and form.has_changed(): user.first_name = form.cleaned_data["first_name"] user.last_name = form.cleaned_data["last_name"] user.email = form.cleaned_data["email"] user.save() profile.affiliation = form.cleaned_data["affiliation"] profile.save() else: form = forms.UserEditForm(initial=initial) ret = { 'user_profile': user_profile, 'token': token.key, 'form': form, 'has_access_request': has_access_request } return render(request, 'account/user_profile.html', ret)
def test_user_edit_post_method_invalid_data(self): self.form_edit_invalid_data = copy.copy(self.form_edit_data) self.form_edit_invalid_data['date_of_birth'] = '05/28/2020' self.user_data.update(self.form_edit_invalid_data) edit_form = forms.UserEditForm(data=self.user_data) edit_form.is_valid() expected_error = 'Seriously are you coming from the future?' response = self.client.post(reverse('account:edit'), data=self.user_data) assert response.status_code == 200 assert expected_error in edit_form.errors.as_text() with self.assertRaises(django_form.ValidationError): edit_form.clean()
def get(self, request, user_id): user = get_object_or_404(account_model.UserProfile, pk=user_id) user_edit_form = account_form.UserEditForm( instance=account_model.UserProfile.objects.get(id=user_id)) variables = { 'user': user, 'user_edit_form': user_edit_form, } return render(request, self.template_name, variables)
def test_edit_form_invalid_date_of_birth(self): edit_data_invalid_date_of_birth = { **self.edit_data, 'date_of_birth': TestUserRegistrationForm.future_date(), } edit_form = forms.UserEditForm(data=edit_data_invalid_date_of_birth) expected_error = 'Seriously are you coming from the future?' assert edit_form.is_valid() is False, \ 'Should return False - date_of_birth from future' assert expected_error in edit_form.non_field_errors() with self.assertRaises(django_form.ValidationError): edit_form.clean()
def post(self, request, user_id): user = get_object_or_404(account_model.UserProfile, pk=user_id) user_edit_form = account_form.UserEditForm( request.POST or None, request.FILES, instance=account_model.UserProfile.objects.get(id=user_id)) if user_edit_form.is_valid(): user_edit_form.save() return redirect('administration:user-detail', user_id=user.id) variables = { 'user': user, 'user_edit_form': user_edit_form, } return render(request, self.template_name, variables)
def test_edit_form_valid_data(self): edit_form = forms.UserEditForm(data=self.edit_data) assert edit_form.is_valid() is True, \ 'Should return True - edit form valid'