def personal_information(): form = PersonalForm() pwd_form = UpdatePasswordForm() if form.validate_on_submit(): user = User.query.get(session.get(USER_ID)) user.username = form.username.data user.name = form.name.data user.birthday = form.birthday.data user.sex = form.sex.data try: db.session.commit() except Exception as e: print(e.args) db.session.rollback() flash('请检查格式是否正确') return render_template('personal_information.html', form=form, pwd_form=pwd_form) flash('修改成功') return redirect(request.path) # 提交修改密码 if pwd_form.validate_on_submit(): return update_password(pwd_form) user = get_user() form.username.data = user.username form.name.data = user.name form.birthday.data = user.birthday form.sex.data = user.sex return render_template('personal_information.html', form=form, pwd_form=pwd_form)
def account(): username_form = UpdateUsernameForm() password_form = UpdatePasswordForm() profile_pic_form = UpdateProfilePicForm() if password_form.validate_on_submit(): hashed = bcrypt.generate_password_hash( password_form.new_password.data).decode("utf-8") msg = Message('Password Change', sender='*****@*****.**', recipients=[str(temp.email)]) msg.body = "Your password has been updated! Please reply to this e-mail if you did not request this change." mail.send(msg) current_user.modify(password=hashed) current_user.save() return redirect(url_for('users.account')) if username_form.validate_on_submit(): temp = User.objects(username=current_user.username).first() current_user.username = username_form.username.data msg = Message('Username Change', sender='*****@*****.**', recipients=[str(temp.email)]) msg.body = "Your username has been updated!\nYour new username is: " + str( username_form.username.data) mail.send(msg) current_user.modify(username=username_form.username.data) current_user.save() return redirect(url_for('users.account')) if profile_pic_form.validate_on_submit(): img = profile_pic_form.propic.data filename = secure_filename(img.filename) if current_user.profile_pic.get() is None: current_user.profile_pic.put(img.stream, content_type='images/png') else: current_user.profile_pic.replace(img.stream, content_type='images/png') current_user.save() return redirect(url_for('users.account')) image = images(current_user.username) return render_template("account.html", title="Account", username_form=username_form, password_form=password_form, profile_pic_form=profile_pic_form, image=image)
def cambio_clave(request): if request.method == 'POST': formulario = UpdatePasswordForm(request.POST) if formulario.is_valid(): formulario.save() return redirect('/index/') else: formulario = UpdatePasswordForm() return render_to_response('usuarios/cambiar_clave.html', {'formulario': formulario}, context_instance=RequestContext(request))
def passwordupdate(): """ @author: EM Implementation of the password update feature. """ passwordForm = UpdatePasswordForm() if passwordForm.validate_on_submit(): flash( f"{session['username'].title()}, you have successfully changed your password.", "success") return redirect(url_for("index")) return redirect(url_for("settings"))
def update_my_info(request, number): if request.session['user_level'] >= 9 or request.session['user_id'] == int( number): if request.method == "POST": user = User.objects.get(id=number) my_bound_form = UpdateInfo_Form(request.POST, instance=user) if my_bound_form.is_valid(): my_bound_form.save() context = {'users': User.objects.all()} return render(request, "user_app/dashboard_admin.html", context) else: context = { 'user': user, 'myupdateinfoform': my_bound_form, 'myupdatepasswordform': UpdatePasswordForm(), 'myupdatedescriptionform': UpdateDescription_Form(instance=user) } return render(request, "user_app/edit_admin.html", context) else: context = {'users': User.objects.all()} return render(request, "user_app/dashboard.html", context)
def edit_admin(request, number): user = User.objects.get(id=number) myupdateinfoform = UpdateInfoAdmin_Form(instance=user) myupdatepasswordform = UpdatePasswordForm() myupdatedescriptionform = UpdateDescription_Form(instance=user) context = { 'user': User.objects.get(id=number), 'myupdateinfoform': myupdateinfoform, 'myupdatepasswordform': myupdatepasswordform, 'myupdatedescriptionform': myupdatedescriptionform } return render(request, "user_app/edit_admin.html", context)
def edit(request): user = User.objects.get(id=request.session['user_id']) myupdateinfoform = UpdateInfo_Form(instance=user) myupdatepasswordform = UpdatePasswordForm() myupdatedescriptionform = UpdateDescription_Form(instance=user) context = { 'user': User.objects.get(id=request.session['user_id']), 'myupdateinfoform': myupdateinfoform, 'myupdatepasswordform': myupdatepasswordform, 'myupdatedescriptionform': myupdatedescriptionform } return render(request, "user_app/edit.html", context)
def update_password(id): colleague, who, authorized = update_authorization(current_user, id) if not authorized: return unathorized("Only your own password can you change.", "error") form = UpdatePasswordForm() if form.validate_on_submit(): if not current_user.check_password(form.password.data): flash("Invalid password. Please log in again.", "warning") logout_user() return redirect(url_for("login")) if form.password.data != form.new_password.data: if form.new_password.data == form.repeat_new_password.data: try: colleague.set_password(form.new_password.data) db.session.commit() flash(f"{who} password changed successfully.", "inform") except: db.session.rollback() flash(f"Any error occured. Please try again.", "error") else: flash( f"{who} repeat password does not match. Please try again.", "warning") return redirect(url_for("profile", id=id)) return render_template("update_password.html", type="Password", value="********", form=form, colleague=colleague, placeholder=get_placeholder(colleague, current_user, form), avatar=get_avatar(colleague), nav=get_nav(current_user))
def settings(): """ @author: EM Implementation of the settings feature. """ searchForm = SearchForm() passwordForm = UpdatePasswordForm() # calling the utility function for autocomplete quotes = search_autocomplete() settings = {} return render_template("settings.html", searchForm=searchForm, quotes=quotes, settings=settings, form=passwordForm)
def change_password(): form = UpdatePasswordForm(request.form) if request.method == "POST": try: current_user.update(password=bcrypt.generate_password_hash( request.form["password"]).decode('utf-8')) current_user.save() print(f"User {form} \n") response = make_response() response.status_code = 200 print(f"response change password {response}\n") return response except Exception as e: raise Exception( f"Error {e}. \n Couldn't change the password of the user,\n with following form: {form}" )
def account(): if not current_user.is_authenticated: return redirect(url_for("index")) context_dict = { "title": "Account", "user": current_user, "edit_user_form": UpdateUserForm(), "edit_password_form": UpdatePasswordForm() } if hasattr(current_user, 'company_name'): all_snack_brands = list({snack.snack_brand for snack in Snack.objects}) # Remove duplicates company_brands = current_user.company_snackbrands # TODO: I'm not sure if the next line is working as it should - ADAM. all_snack_brands = list( filter(lambda a: a not in company_brands, all_snack_brands)) all_snack_brands_temp = [(snack, snack) for snack in all_snack_brands] search_company_brands = [(snack, snack) for snack in company_brands] all_snack_brands = all_snack_brands_temp all_snack_brands.sort() search_company_brands.sort() default = [("Can't find my brand, create a new brand!", "Can't find my brand, create a new brand!")] search_company_brands = default + search_company_brands all_snack_brands = default + all_snack_brands search_form = CompanySearchBrandForm() search_form.search_snack_brand.choices = search_company_brands add_form = CompanyAddBrandForm() add_form.add_snack_brand.choices = all_snack_brands if request.method == "POST" and add_form.validate_on_submit(): add_snack_brand = add_form.add_snack_brand.data if add_snack_brand != "Can't find my brand, create a new brand!": try: current_user.update( add_to_set__company_snackbrands=add_snack_brand) except Exception as e: raise Exception( f"Error {e}. \n Couldn't add {add_snack_brand},\n with following creation form: {add_form}" ) print(f"A new snack_brand added to company user", file=sys.stdout) return redirect(url_for('account')) else: return redirect(url_for("create_brand")) # TODO: Somebody called it query_set - but actually implemented it as a list - what should be the correct one? query_set = [] if request.method == "POST" and search_form.validate_on_submit(): search_snack_brand = search_form.search_snack_brand.data if search_snack_brand != "Nothing Selected": query_set = [ snack for snack in Snack.objects if snack.snack_brand == search_snack_brand ] context_dict.update({ "company_brands": company_brands, "search_form": search_form, "add_form": add_form, "query_set": query_set }) return render_template('account.html', **context_dict) else: print("User is not a company user") return render_template('account.html', **context_dict)