def UserProfileUpdateView(request, pk): userprofile = UserProfile.objects.get(pk=pk) user = userprofile.user user_form = UserUpdateForm() user_profile_form = UserProfileUpdateForm() if request.POST: user_form = UserUpdateForm(request.POST, instance=user) user_profile_form = UserProfileUpdateForm(request.POST, instance=userprofile) if user_form.is_valid() and user_profile_form.is_valid(): user_form.save() user_profile_form.save() return HttpResponseRedirect(reverse('home')) user_form = UserUpdateForm(instance=user) user_profile_form = UserProfileUpdateForm(instance = userprofile) return render(request, 'userprofile/update.html', { 'user_form': user_form, 'user_profile_form': user_profile_form, })
def user_profile(request): orders = SingleOrderLineItem.objects.all() if request.method == 'POST': user_update_form = UserUpdateForm(request.POST, instance=request.user) if user_update_form.is_valid(): user_update_form.save() messages.success(request, "You have updated your profile!") return redirect('profile') else: user_update_form = UserUpdateForm(instance=request.user) uploads = SingleOrderUpload.objects.all() user = User.objects.get(email=request.user.email) context = { 'user_update_form': user_update_form, 'orders': orders, 'profile': user, 'uploads': uploads } return render(request, 'profile.html', context)
def profile(request): """Show user profile, allow them to update it.""" if not request.user.is_authenticated(): return HttpResponseRedirect(reverse('index')) if request.method == 'POST': form = UserUpdateForm(request.POST or None, instance=request.user) if form.is_valid(): form.save() if 'profile_image' in request.FILES: try: lbwuser = request.user.lbwuser lbwuser.profile_image = request.FILES['profile_image'] lbwuser.save() except LbwUser.DoesNotExist: lbwuser = LbwUser(user=request.user, profile_image=request.FILES['profile_image']) lbwuser.save() return redirect('index') else: try: lbwuser = request.user.lbwuser except LbwUser.DoesNotExist: lbwuser = LbwUser(user=request.user) lbwuser.save() form = UserUpdateForm(instance=request.user) return render(request, 'accounts/profile.html', {'form': form})
def update_view(request): contact_form = ContactForm() if request.user.is_authenticated: user = request.user if request.method == 'POST': form = UserUpdateForm(request.POST) if form.is_valid(): data = form.cleaned_data user.city = data['city'] user.language = data['language'] user.send_email = data['send_email'] user.save() messages.success(request, 'Данные сохраненны.') return redirect('accounts:update') form = UserUpdateForm( initial={ 'city': user.city, 'language': user.language, 'send_email': user.send_email }) return render(request, 'accounts/update.html', { 'form': form, 'contact_form': contact_form }) else: return redirect('accounts:login')
def test_invalid_update_form_username_symbols(self): """ Test an invalid form due to @ in username """ form_data = {"username": "******", "email": ""} form = UserUpdateForm(data=form_data) self.assertFalse(form.is_valid()) self.assertTrue("username" in form.errors)
def test_invalid_update_form_existing_username(self): """ Test an invalid form due to existing username """ form_data = {"username": "******", "email": ""} form = UserUpdateForm(data=form_data) self.assertFalse(form.is_valid()) self.assertTrue("username" in form.errors)
def test_invalid_update_form_existing_email(self): """ Test an invalid form due to existing email """ form_data = {"username": "", "email": "*****@*****.**"} form = UserUpdateForm(data=form_data) self.assertFalse(form.is_valid()) self.assertTrue("email" in form.errors)
def profile(request): u = request.user p = u.get_profile() if request.method == 'POST': previous_email = u.email user_form = UserUpdateForm(request.POST, instance=u) profile_form = ProfileForm(request.POST, request.FILES, instance=p) if user_form.is_valid() and profile_form.is_valid(): user_form.save() profile_form.save() # Check if email change was requested # Note: request.POST['email'] should be safe below since it's a valid email if u.email != request.POST['email']: p.new_email = request.POST['email'] p.email_change_key = random_string(16) p.save() send_email( "Action required to change your email at TempSurge", "accounts/emails/email_change", settings.FROM_EMAIL, p.new_email, { 'change_email_url': "http://tempsurge.com/accounts/change-email/" + str(u.id) + "/" + p.email_change_key }) return render( request, "accounts/message.html", { 'app': "accounts", 'connotation': "warning", 'message': "Please check your email (%s) and follow the reactivation instructions." % p.new_email }) messages.success(request, "Your profile has been successfully updated.", extra_tags="profile_updated") return redirect("/accounts/profile/") else: user_form = UserUpdateForm(instance=u) profile_form = ProfileForm(instance=p) return render( request, 'accounts/profile.html', { 'user': u, 'profile': p, 'user_form': user_form, 'profile_form': profile_form, 'random': random_string() })
def post(self, request, *args, **kwargs): form = UserUpdateForm(request.POST, instance=request.user) if form.is_valid(): form.save() messages.success(request, 'Successfully updated account details') return redirect('dashboard') else: messages.warning(request, 'Check you details') return redirect('account')
def post(self, request, *args, **kwargs): user_form = UserUpdateForm(request.POST, instance=request.user) profile_form = ProfileUpdateForm(request.POST, request.FILES, instance=request.user.profile) if user_form.is_valid() and profile_form.is_valid(): user_form.save() and profile_form.save() messages.success(request, f"Your account has been updated!") return redirect("bookstore")
def post(self, request, *args, **kwargs): author_form = AuthorForm(request.POST, request.FILES, instance=request.user.author) user_form = UserUpdateForm(request.POST, instance=request.user) if author_form.is_valid() and user_form.is_valid(): user_form.save() author_form.save() return redirect("accounts_update")
def user_update(request, user_slug): if request.method == "POST" and user_slug == request.user.slug: form = UserUpdateForm(request.POST) user = get_object_or_404(User, slug=user_slug) if form.is_valid(): email = request.POST.get('email') new_password = request.POST.get('new_password') repeat_password = request.POST.get('repeat_password') current_password = request.POST.get('current_password') # check if email already exists if User.objects.filter( email=email).exists() and email != user.email: messages.error(request, 'This email is already taken, try another.') return redirect('accounts:user_update', user_slug=user_slug) #check password if user.check_password(current_password): #check if password is to be updated if new_password != '': if new_password == repeat_password: user.set_password(new_password) else: print('new password : {}, repeat: {}'.format( new_password, repeat_password)) messages.error( request, 'New password repeated incorrectly, try again.') return redirect('accounts:user_update', user_slug=user_slug) #save email user.email = email user.save() messages.success(request, 'Credentials were updated successfuly.') # log in user with new credentials user = authenticate(request, username=email, password=new_password) login(request, user) return redirect('accounts:user_home', user_slug=user_slug) else: # wrong password messages.error(request, 'Wrong Password') return redirect('accounts:user_update', user_slug=user_slug) else: # invalid form messages.error(request, 'Ups. Something went wrong.') return redirect('accounts:user_home', user_slug=user_slug) else: form = UserUpdateForm(initial={ 'email': request.user.email, }) return render(request, 'accounts/user_update.html', { 'user_update_form': form, })
def post(self, request, username): u_form = UserUpdateForm(request.POST, instance=request.user) p_form = ProfileUpdateForm(request.POST, request.FILES, instance=request.user.profile) if u_form.is_valid() and p_form.is_valid(): u_form.save() p_form.save() messages.success(request, _('Account successfully updated!')) return redirect('accounts:profile:bio', self.get_username()) return self.get(request, username)
def post(request): update_form = UserUpdateForm(request.POST, instance=request.user) if update_form.is_valid(): update_form.save() messages.success(request, u'个人信息已更新') return HttpResponseRedirect(reverse('accounts:detail')) else: messages.error(request, u'信息更新失败') return render(request, "accounts/form_tpl.html", {"form": update_form})
def test_valid_update_form(self): """ Test a valid form """ form_data = { "username": "", "email": "*****@*****.**" } form = UserUpdateForm(data=form_data) self.assertTrue(form.is_valid())
def employee_update_view(request, id): data = dict() employee_instance = Employee.objects.get(id=id) if request.method == 'POST': user_form = UserUpdateForm(request.POST, initial={ 'username': employee_instance.user.username, 'email': employee_instance.user.email }) employee_form = EmployeeForm(request.POST, request.FILES, instance=employee_instance) if employee_form.is_valid() and user_form.is_valid(): username = user_form.cleaned_data.get("username") email = user_form.cleaned_data.get("email") user_qs = User.objects.filter(username=username).exclude( id=employee_instance.user.id).exists() if user_qs: data['username_error'] = { "error": "Username is already taken!" } return JsonResponse(data) else: instance_user = employee_instance.user instance_user.username = username if email: instance_user.email = email instance_user.save() employee_form.save() else: data['errors'] = { 'status': 'form-invalid', 'employee_user_errors': user_form.errors, 'employee_errors': employee_form.errors } else: user_form = UserUpdateForm( initial={ 'username': employee_instance.user.username, 'email': employee_instance.user.email }) employee_form = EmployeeForm(instance=employee_instance) context = { 'user_form': user_form, 'employee_form': employee_form, 'object': employee_instance } if request.is_ajax(): data['html_form'] = render_to_string('employees/employee-form.html', context, request=request) return JsonResponse(data) return render(request, 'employees/employee-update.html', context)
def user_account_update_form(request): if request.method == 'POST': form = UserUpdateForm(request.POST, instance=request.user) if form.is_valid(): form.save() messages.success(request, 'Your account has been updated') return redirect('accounts:dashboard') # return redirect('accounts:user_profile') else: form = UserUpdateForm(instance=request.user) context = {'form': form} return render(request, 'accounts/update/edit_account_info.html', context)
def update_user(request): """Register new users""" active_user = request.user # check if a form was submitted if request.method == 'POST': next = request.POST.get('next', '/') user_form = UserUpdateForm(request.POST, instance=active_user) if user_form.is_valid(): user = user_form.save() else: return redirect('accounts:profile') return redirect(next)
def put(self, request, id=None): context = {} student = get_object_or_404(Student, id=id) user = get_object_or_404(User, id=student.user.id) user_form = UserUpdateForm(request.POST, instance=user) student_form = StudentForm(request.POST, instance=student) if user_form.is_valid(): get_user = user_form.save(commit=False) if student_form.is_valid(): student_form.save(user=get_user.id, commit=False) context = {'user_form': user_form, 'student_form': student_form} return render(request, 'accounts/student_new_update.html', context)
def user_update(request, pk): if request.user.id == pk: users = get_object_or_404(User, id=pk) if request.method == "POST": form = UserUpdateForm(request.POST, instance=users) if form.is_valid(): form.save() return redirect("accounts:user_info", request.user.pk) else: form = UserUpdateForm(instance=users) return render(request, "accounts/user_update.html", {"form":form}) else: return HttpResponseForbidden()
def test_form_is_valid(self): post_dict = { 'username':'******', 'email':'*****@*****.**', } files_dict = { 'icon':SimpleUploadedFile( name='sample.jpeg', content=open(os.path.join(os.environ['HOME'],'Pictures/sample.jpeg'), 'rb').read(), content_type='image/jpeg' ) } form = UserUpdateForm(post_dict, files_dict) self.assertTrue(form.is_valid())
def TeacherProfile(request): if request.method == 'POST': u_form = UserUpdateForm(request.POST,request.FILES, instance=request.user) if u_form.is_valid(): u_form.save() messages.success(request, f'Your account has been updated') return redirect('TeacherProfile') else: u_form = UserUpdateForm(instance=request.user) context = { 'u_form': u_form } return render(request, 'Teacher/Profile/TeacherProfile.html', context)
def user_profile(request): ''' Renders profile page for user with a form to update their information. ''' if request.method == 'POST': form = UserUpdateForm(request.POST, instance=request.user) if form.is_valid(): form.save() messages.success(request, u'Your account info has been updated.') return redirect('profile') else: form = UserUpdateForm(instance=request.user) return render(request, 'profile.html', {'form':form})
def edit_profile(request): if request.method == "POST": form = UserUpdateForm(request.POST, instance=request.user) form2 = ProfileUpdateForm(request.POST, instance=request.user.profile) if form.is_valid() and form2.is_valid(): form.save() form2.save() return redirect('view_profile') else: form = UserUpdateForm(instance=request.user) form2 = ProfileUpdateForm(instance=request.user.profile) context = {"form": form, "form2": form2} return render(request, "edit_profile.html", context)
def edit_profile(request): if request.method == "POST": form = UserUpdateForm(request.POST, instance=request.user) form_user = ProfileUpdateForm(request.POST,\ request.FILES,\ instance = request.user.userprofile) if form.is_valid() and form_user.is_valid: form.save() form_user.save() return redirect('/account/') else: form = UserUpdateForm(instance=request.user) form_user = ProfileUpdateForm(instance=request.user.userprofile) args = {'form': form, 'form_user': form_user} return render(request, 'accounts/edit_profile.html', args)
def MasterUserUpdateView(request,slug): masteruser_user = get_object_or_404(UserModel,slug = slug) user_update_form = UserUpdateForm(request.POST or None, instance = masteruser_user) masteruser = get_object_or_404(MasterUserModel, user = masteruser_user) masteruser_update_form = MasterUserUpdateForm(request.POST or None, instance = masteruser) if masteruser_update_form.is_valid() and user_update_form.is_valid(): user_update_form.save() masteruser_update_form.save() if 'picture' in request.FILES: masteruser.picture = request.FILES['picture'] masteruser.save() return HttpResponseRedirect(reverse('masteruser:profile', kwargs={'slug':masteruser_user.slug})) return render(request,'masteruser/masteruserupdate.html', {'masteruserform':masteruser_update_form, 'userform':user_update_form,})
def government_employee__profile(request): if request.method == 'POST': u_form = UserUpdateForm(request.POST or None, request.FILES or None, instance=request.user) if u_form.is_valid(): u_form.save() messages.success(request, f'Your account has been updated') return redirect('government_change_profile') else: u_form = UserUpdateForm(instance=request.user) context = {'u_form': u_form} return render( request, 'government_employee/profile/GovernmentEmployeeProfile.html', context)
def profile(request): if request.method == 'POST': u_form = UserUpdateForm(request.POST, instance=request.user) p_form = ProfileUpdateForm(request.POST, request.FILES, instance=request.user.profile) if u_form.is_valid() and p_form.is_valid(): u_form.save() p_form.save() return redirect('profile') else: u_form = UserUpdateForm(instance=request.user) p_form = ProfileUpdateForm(instance=request.user.profile) context = {'u_form': u_form, 'p_form': p_form} return render(request, 'accounts/profile.html', context)
def profile_page(request, *args, **kwargs): if request.method == 'POST': user_form = UserUpdateForm(request.POST, instance=request.user) profile_form = ProfileUpdateForm(request.POST, request.FILES, instance=request.user.profile) if user_form.is_valid() and profile_form.is_valid(): user_form.save() profile_form.save() return redirect('profile') else: user_form = UserUpdateForm(instance=request.user) profile_form = ProfileUpdateForm(instance=request.user.profile) context = {'user_form': user_form, 'profile_form': profile_form} return render(request, 'profile/profile-update.html', context)
def profile(request): profile = get_object_or_404(Profile, user_id=request.user.id) if request.method == 'POST': u_form = UserUpdateForm(request.POST, instance=request.user) p_form = ProfileUpdateForm(request.POST, request.FILES, instance=request.user.profile) if u_form.is_valid() and p_form.is_valid: u_form.save() p_form.save() messages.success(request, f'Your account has been updated!') return redirect('profile') else: u_form = UserUpdateForm(instance=request.user) p_form = ProfileUpdateForm(instance=request.user.profile) context = {'profile': profile, 'u_form': u_form, 'p_form': p_form} return render(request, 'accounts/profile.html', context)
def profile(request): if request.method == 'POST': u_form = UserUpdateForm(request.POST, instance=request.user) p_form = ProfileUpdateForm(request.POST, request.FILES, instance=request.user.profile) if u_form.is_valid() and p_form.is_valid(): u_form.save() p_form.save() messages.success(request, f'Your account has been updated!') return redirect('profile') else: messages.error(request, _('Please correct the error below.')) else: u_form = UserUpdateForm(instance=request.user) p_form = ProfileUpdateForm(instance=request.user.profile) context = {'u_form': u_form, 'p_form': p_form} return render(request, 'accounts/profile.html', context)
def CommonUserUpdateView(request, slug): commonuser_user = get_object_or_404(UserModel, slug=slug) if request.user == commonuser_user: user_update_form = UserUpdateForm(request.POST or None, instance=commonuser_user) commonuser = get_object_or_404(CommonUserModel, user=commonuser_user) if user_update_form.is_valid(): user_update_form.save() if 'picture' in request.FILES: commonuser.picture = request.FILES['picture'] commonuser.save() return HttpResponseRedirect( reverse('commonuser:profile', kwargs={'slug': commonuser_user.slug})) return render(request, 'commonuser/commonuserupdate.html', { 'userform': user_update_form, }) else: return HttpResponseRedirect(reverse('login'))
def user_profile(request): """ Renders the user's info on the profile page with a form to update their username or emails address. """ if request.method == 'POST': user_form = UserUpdateForm(request.POST, instance=request.user) # If the form is valid, save new details if user_form.is_valid(): user_form.save() messages.success(request, "Your account has been successfully updated") return redirect('index') else: user_form = UserUpdateForm(instance=request.user) context = {'user_form': user_form} return render(request, 'profile.html', context)