def edit_profile(request): user = request.user myuser = MyUser.objects.get(user=user) form_user = EditUserForm(request.POST or None, initial={'first_name': myuser.user.first_name, 'last_name': myuser.user.last_name, 'email': myuser.user.email}) form_myuser = EditMyUserForm(request.POST or None, initial={'phone_number': myuser.phone_number, 'address': myuser.address}) if request.method == 'POST': # form = EditProfileForm(request.POST) if form_user.is_valid() * form_myuser.is_valid(): user.first_name = form_user.cleaned_data['first_name'] user.last_name = form_user.cleaned_data['last_name'] user.email = form_user.cleaned_data['email'] myuser.phone_number = form_myuser.cleaned_data['phone_number'] myuser.address = form_myuser.cleaned_data['address'] user.save() myuser.user = user myuser.save() return HttpResponseRedirect('/user/profile') # return render(request, "thanks.html", {'message': 'تغییرات با موفقیت ذخیره شد.', 'redir': '/simorgh/profile/'}) # else: # print("valid nist") # return HttpResponseRedirect() # else: # form = EditProfileForm(initial=init) return render(request, 'user/profile_edit.html', {"form_user": form_user, "form_myuser": form_myuser, "myuser": myuser})
def test_change_email(self): form = EditUserForm(data={ 'email': '*****@*****.**', 'roles': [2] }, instance=self.user) if form.is_valid(): form.save(self.user) self.assertEqual(self.user.email, '*****@*****.**') self.assertEqual(self.user.username, '*****@*****.**') self.assertEqual(self.user.roles.count(), 1) self.assertEqual(self.user.roles.first().id, 2)
def test_add_role(self): form = EditUserForm(data={ 'email': '*****@*****.**', 'roles': [2, 7] }, instance=self.user) if form.is_valid(): form.save(self.user) self.assertEqual(self.user.email, '*****@*****.**') self.assertEqual(self.user.roles.count(), 2) self.assertIn(Role.objects.get(id=2), self.user.roles.all()) self.assertIn(Role.objects.get(id=7), self.user.roles.all())
def edit_biodata(request, pk, username): """Modify user details. Including Kin, Sponsor and Programme details.""" user_detail = get_object_or_404(UserDetail, pk=pk, user__username=username) if not is_owner(request, user_detail): return redirect('index') if request.method == 'POST': kin_form = EditKinForm(request.POST, instance=user_detail.kin) programme_form = EditProgrammeForm(request.POST, instance=user_detail.programme) sponsor_form = EditSponsorForm(request.POST, instance=user_detail.sponsor) user_detail_form = EditUserDetailForm(request.POST, request.FILES, instance=user_detail) user_form = EditUserForm(request.POST, instance=user_detail.user) if (kin_form.is_valid() and programme_form.is_valid() and sponsor_form.is_valid() and user_detail_form.is_valid() and user_form.is_valid()): # Check that all forms are valid. kin_form.save() programme_form.save() sponsor_form.save() user_detail_form.save() user_form.save() return redirect('profile', user_detail.pk, user_detail.user.username) return render(request, 'user/edit_profile.html', context={ 'kin_form': kin_form, 'programme_form': programme_form, 'sponsor_form': sponsor_form, 'user_form': user_form, 'user_detail_form': user_detail_form, 'user_detail': user_detail }) kin_form = EditKinForm(instance=user_detail.kin) programme_form = EditProgrammeForm(instance=user_detail.programme) sponsor_form = EditSponsorForm(instance=user_detail.sponsor) user_detail_form = EditUserDetailForm(instance=user_detail) user_form = EditUserForm(instance=user_detail.user) return render(request, 'user/edit_profile.html', context={ 'kin_form': kin_form, 'programme_form': programme_form, 'sponsor_form': sponsor_form, 'user_detail_form': user_detail_form, 'user_detail': user_detail, 'user_form': user_form })
def edit_user(request, id): user = Profile.objects.get(id=id) if request.method == 'POST': form = EditUserForm(data=request.POST, instance=user) if form.is_valid(): form.save(user=user) return redirect('/auth/admin') else: form = EditUserForm(instance=user) return render(request, 'admin/edit-user.html', { 'user': user, 'form': form })
def edit_profile(request): if request.method == 'POST': usereditform = EditUserForm(request.POST, instance=request.user) profileeditform = EditProfileForm(request.POST, instance=request.user) if usereditform.is_valid(): usereditform.save() return redirect('user:view_profile') else: usereditform = EditUserForm(instance=request.user) profileeditform = EditProfileForm(instance=request.user) args = { 'usereditform': usereditform, 'profileeditform': profileeditform } return render(request, 'edit_profile.html', args)
def editUser(request, id): user = User.objects.get(pk=id) if request.method == 'POST': form = EditUserForm(request.POST, instance=user) form.save() messages.success(request, 'Profile edited!') return HttpResponseRedirect('/') else: form = EditUserForm(instance=user) response = HttpResponse(form) return response
def test_form_renders_roles_input(self): form = EditUserForm(instance=self.user) self.assertIn('id="id_roles_0" checked', form.as_p())
def test_form_renders_email_input(self): form = EditUserForm(instance=self.user) self.assertIn('value="*****@*****.**"', form.as_p()) self.assertIn('type="email"', form.as_p())