def user_info(): form = InfoForm() user = query_user_by_name(session.get("user_name")) if form.validate_on_submit(): old_name = user.name user.name = form.data["user_name"] user.email = form.data["user_email"] user.age = form.data["user_edge"] user.birthday = form.data["user_birthday"] filestorage = form.user_face.data # 判断用户是否上传了新的头像文件 if filestorage.filename != "": # 如果上传了符合要求的新的头像文件,则首先删除旧的,再保存新的 user_folder = os.path.join(app.config["UPLOADS_FOLDER"], old_name) os.remove(path=os.path.join(user_folder, user.face)) # 更新 user.face 中保存的头像文件名 user.face = secure_filename_with_uuid(filestorage.filename) filestorage.save(os.path.join(user_folder, user.face)) # 如果用户修改了用户名, 就修改用户的上传文件夹 if old_name != user.name: os.rename(os.path.join(app.config["UPLOADS_FOLDER"], old_name), os.path.join(app.config["UPLOADS_FOLDER"], user.name)) # 更新数据项 update_user_by_name(old_name, user) session["user_name"] = user.name return redirect(url_for("user_detail")) return render_template("user_info.html", user=user, form=form)
def user_info(): form = InfoForm() user = query_user_by_name(session.get("user_name")) if form.validate_on_submit(): old_name = user.name user.name = form.data["user_name"] user.email = form.data["user_email"] user.age = form.data["user_edge"] user.birthday = form.data["user_birthday"] filestorage = request.files["user_face"] # 判断用户是否上传了新的头像文件 if filestorage.filename != "": # 检查用户上传的头像文件名是否符合要求 if not check_files_extension([form.user_face.data.filename], ALLOWED_IMAGE_EXTENSIONS): flash("头像文件扩展名不对!", category='err') return redirect(url_for("user_info")) # 如果上传了符合要求的新的头像文件,则首先删除旧的,再保存新的 user_folder = os.path.join(app.config["UPLOADS_FOLDER"], old_name) os.remove(path=os.path.join(user_folder, user.face)) # 更新 user.face 中保存的头像文件名 user.face = secure_filename_with_uuid(filestorage.filename) filestorage.save(os.path.join(user_folder, user.face)) # 如果用户修改了用户名, 就修改用户的上传文件夹 if old_name != user.name: os.rename(os.path.join(app.config["UPLOADS_FOLDER"], old_name), os.path.join(app.config["UPLOADS_FOLDER"], user.name)) # 更新数据项 update_user_by_name(old_name, user) session["user_name"] = user.name return redirect(url_for("user_detail")) return render_template("user_info.html", user=user, form=form)
def user_info(): # 修改个人信息 user = query_user_by_name(session.get("user_name")) # 打开修改信息页面,将原来信息展示出来,消息回填 form = InfoForm() if form.validate_on_submit(): current_login_name = session.get("user_name") old_name = user.name new_name = request.form["user_name"] query_user = query_user_by_name(new_name) if query_user == None or current_login_name == query_user.name: # 如果数据库没有这个用户名或者当前登录的用户名和更改的用户名一样(本人操作),都可以更新个人信息 user.name = request.form["user_name"] user.email = request.form["user_email"] user.age = request.form["user_age"] user.birthday = request.form["user_birthday"] filestorage = request.files["user_face"] # 获取头像文件 # filestorage = form.user_face.data # 获取头像文件 if filestorage.filename != "": # # 检查用户上传的头像文件名是否符合要求 # if not check_files_extension([form.user_face.data.filename], ALLOWED_IMAGEEXTENSIONS): # flash("头像文件格式错误!", category="err") # return redirect(url_for("user_info")) # 若果上传了新的头像文件,首先删除旧的,再保存新的 # user_folder = os.path.join(app.config["UPLOADS_FOLDER"], old_name) # 删除就旧的头像 # os.remove(path=os.path.join(user_folder, user.face)) # 获取旧头像的存储路径 userface_path = photosSet.path(filename=user.face, folder=old_name) # 删除就旧的头像 os.remove(userface_path) # 保存新的 user.face = secure_filename_with_uuid(filestorage.filename) # filestorage.save(os.path.join(user_folder, user.face)) photosSet.save(storage=filestorage, folder=old_name, name=user.face) pass # 判断是否修改了用户名:如果修改了则同时修改用户上传资源文件夹 if old_name != new_name: os.rename(os.path.join(app.config["UPLOADS_FOLDER"], old_name), os.path.join(app.config["UPLOADS_FOLDER"], new_name)) pass update_user_by_name(old_name, user) flash(message="用户信息已更新!", category="ok") session.pop("user_name", None) # 修改密码后需要重新登录,然后清除session中的数据 session["user_name"] = user.name return redirect(url_for("user_detail")) else: flash(message="用户名已存在!", category="err") return render_template("user_info.html", user=user, form=form)
def user_pwd(): form = PwdForm() if form.validate_on_submit(): old_pwd = form.old_pwd.data new_pwd = form.data["new_pwd"] user = query_user_by_name(session.get("user_name")) if str(old_pwd) == str(user.pwd): user.pwd = new_pwd update_user_by_name(user.name, user) session.pop("user_name", None) flash(message="修改密码成功,请重新登录!", category='ok') return redirect(url_for("user_login", username=user.name)) else: flash(message="旧密码输入错误!", category='err') return render_template("user_pwd.html", form=form) return render_template("user_pwd.html", form=form)
def user_pwd(): # 修改个人密码 form = PwdForm() if form.validate_on_submit(): old_pwd = request.form["old_pwd"] new_pwd = request.form["new_pwd"] user = query_user_by_name(session.get("user_name")) if str(old_pwd) == str(user.pwd): user.pwd = new_pwd update_user_by_name(user.name, user) session.pop("user_name", None) # 修改密码后需要重新登录,然后清除session中的数据 flash(message="密码修改成功!请重新登录!", category="ok") return redirect(url_for("user_login", username=user.name)) else: flash(message="旧密码输入错误!", category="err") return render_template("user_pwd.html", form=form) return render_template("user_pwd.html", form=form)