def user(): form = UserForm() user = User.query.filter_by(id=session["user_id"]).first() if request.method == "GET": form.name.flags.required = False form.email.flags.required = False form.phone.flags.required = False form.info.data = user.info if form.validate_on_submit(): data = form.data user.name = data["name"] user.email = data["email"] user.phone = data["phone"] user.info = data["info"] if not os.path.exists(app.config["FACE_DIR"]): os.makedirs(app.config["FACE_DIR"]) os.chmod(app.config["FACE_DIR"]) if form.face.data != "": file_face = secure_filename(form.face.data.filename) user.face = change_filename(file_face) form.face.data.save(app.config["FACE_DIR"] + user.face) try: db.session.add(user) db.session.commit() flash("信息修改成功", "ok") except Exception as e: print(e) flash("信息修改失败", "error") db.session.rollback() return render_template("home/user.html", form=form, user=user)
def user(): user = User.query.get_or_404(int(session['user_id'])) form = UserDetailForm() form.face.validators = [] form.face.render_kw = {'required': False} if request.method == 'GET': form.name.data = user.name form.email.data = user.email form.phone.data = user.phone form.info.data = user.info if form.validate_on_submit(): data = form.data face_save_path = app.config['UP_DIR'] if not os.path.exists(face_save_path): os.makedirs(face_save_path) # 如果文件保存路径不存在,则创建一个多级目录 import stat os.chmod(face_save_path, stat.S_IRWXU) # 授予可读写权限 if form.face.data: if user.face and os.path.exists( os.path.join(face_save_path + 'users/', user.face)): os.remove(os.path.join(face_save_path, user.face)) # 获取上传文件名称 file_face = secure_filename( form.face.data.filename ) # 前端表单设置 enctype="multipart/form-data" from app.admin.views import change_filename user.face = change_filename(file_face) form.face.data.save(face_save_path + 'users/' + user.face) if user.name != data['name'] and User.query.filter_by( name=data['name']).count() == 1: flash('昵称已经存在', 'err') return redirect(url_for('home.user')) user.name = data['name'] if user.email != data['email'] and User.query.filter_by( email=data['email']).count() == 1: flash('邮箱已经存在', 'err') return redirect(url_for('home.user')) user.email = data['email'] if user.phone != data['phone'] and User.query.filter_by( phone=data['phone']).count() == 1: flash('手机号已经存在', 'err') return redirect(url_for('home.user')) user.phone = data['phone'] user.info = data['info'] db.session.commit() flash('修改资料成功', 'ok') return redirect(url_for('home.user')) return render_template('home/user.html', form=form, user=user)
def user(): login_user = User.query.get_or_404(int(session['login_user_id'])) form = UserDetailForm(name=login_user.name, email=login_user.email, phone=login_user.phone, info=login_user.info) form.face.validators = [] form.face.render_kw = {'required': False} if form.validate_on_submit(): data = form.data face_save_path = app.config['USER_IMAGE'] if not os.path.exists(face_save_path): os.makedirs(face_save_path) # 如果文件保存路径不存在,则创建一个多级目录 import stat os.chmod(face_save_path, stat.S_IRWXU) # 授予可读写权限 if form.face.data: # 上传文件不为空保存 if login_user.face and os.path.exists( os.path.join(face_save_path, login_user.face)): os.remove(os.path.join(face_save_path, login_user.face)) # 获取上传文件名称 file_face = secure_filename(form.face.data.filename) # !!!AttributeError: 'str' object has no attribute 'filename',前端需要加上enctype="multipart/form-data" from app.admin.views import change_filename login_user.face = change_filename(file_face) form.face.data.save(face_save_path + login_user.face) if login_user.name != data['name'] and User.query.filter_by( name=data['name']).count() == 1: flash('昵称已经存在', 'err') return redirect(url_for('home.user')) login_user.name = data['name'] if login_user.email != data['email'] and User.query.filter_by( email=data['email']).count() == 1: flash('邮箱已经存在', 'err') return redirect(url_for('home.user')) login_user.email = data['email'] if login_user.phone != data['phone'] and User.query.filter_by( phone=data['phone']).count() == 1: flash('手机号已经存在', 'err') return redirect(url_for('home.user')) login_user.phone = data['phone'] login_user.info = data['info'] db.session.commit() flash('修改资料成功', 'ok') return redirect(url_for('home.user')) return render_template('home/user.html', form=form, login_user=login_user)
def user(): """ 用户中心 """ form = UserdetailForm() user = User.query.get_or_404(session['user_id']) if request.method == 'GET': form.info.data = user.info if form.validate_on_submit(): data = form.data # 校验用户名,邮箱,手机号码是否已经存在 name_count = User.query.filter_by(name=data['name']).count() if data['name'] != user.name and name_count == 1: flash('昵称已经存在!', category='err') return redirect(url_for('home.user')) email_count = User.query.filter_by(email=data['email']).count() if data['email'] != user.email and email_count == 1: flash('邮箱地址已经存在!', category='err') return redirect(url_for('home.user')) phone_count = User.query.filter_by(phone=data['phone']).count() if data['phone'] != user.phone and phone_count == 1: flash('手机号码已经存在',category='err') return redirect(url_for('home.user')) # 保存头像文件 if not os.path.exists(app.config['FC_DIR']): os.mkdirs(app.config['FC_DIR']) os.chmod(app.config['FC_DIR'], 'rw') if form.face.data.filename != None: filename = secure_filename(form.face.data.filename) user.face = change_filename(filename) form.face.data.save(app.config['FC_DIR'] + user.face) user.name = data['name'] user.email = data['email'] user.phone = data['phone'] user.info = data['info'] # 上传头像 db.session.add(user) db.session.commit() flash("用户信息更新成功!", category="ok") return redirect(url_for('home.user')) return render_template("home/user.html", form=form, user=user)
def user(): form = UserdetailForm() user = User.query.get_or_404(int(session['user_id'])) form.face.validators = [] if request.method == 'GET': form.name.data = user.name form.email.data = user.email form.phone.data = user.phone form.info.data = user.info if form.validate_on_submit(): data = form.data file_face = secure_filename(form.face.data.filename) if not os.path.exists(app.config['FC_DIR']): os.makedirs(app.config['FC_DIR']) os.chmod(app.config['FC_DIR'], 'rw') user.face = change_filename(file_face) form.face.data.save(app.config['FC_DIR'] + user.face) name_count = User.query.filter_by(name=data['name']).count() if data['name'] != user.name and name_count == 1: flash('昵称已经存在!', 'err') return redirect(url_for('home.user')) email_count = User.query.filter_by(email=data['email']).count() if data['email'] != user.email and email_count == 1: flash('邮箱已经存在!', 'err') return redirect(url_for('home.user')) phone_count = User.query.filter_by(phone=data['phone']).count() if data['phone'] != user.phone and phone_count == 1: flash('手机号已经存在!', 'err') return redirect(url_for('home.user')) user.name = data['name'] user.email = data['email'] user.phone = data['phone'] user.info = data['info'] db.session.add(user) db.session.commit() flash('修改成功!', 'ok') return redirect(url_for('home.user')) return render_template("home/user.html", form=form, user=user)
def user() -> Union[str, None, werkzeug.wrappers.Response]: form = UserDetailForm() user_field = User.query.get(int(session['user_id'])) if request.method == 'GET': # 用户访问该页面时,各种属性的默认值 form.face.validators = [] form.name.data = user_field.name form.email.data = user_field.email form.phone.data = user_field.phone form.info.data = user_field.info if form.validate_on_submit() is True: data = form.data # 对name,phone,email进行唯一性检查 if validate_session(form, user_field) is not '': flash(validate_session(form, user_field), 'err') return redirect(url_for('home.user')) else: # 如果没有修改头像,那么头像默认为原来的 if form.face.data.filename is '': form.face.data.filename = user_field.face face = user_field.face else: # 将上传的头像文件保存到本地 path = create_app().config['FC_DIR'] if os.path.exists(path) is False: os.makedirs(path) # 当保存路径不存在时进行创建 √ face = change_filename(form.face.data.filename) form.face.data.save(path + face) user_field.name = data['name'] user_field.email = data['email'] user_field.phone = data['phone'] user_field.face = face user_field.info = data['info'] db.session.add(user_field) db.session.commit() flash('修改成功', 'ok') return redirect(url_for('home.user')) return render_template('home/user.html', form=form, user=user_field)
def user(): form = UserDetailForm() user = User.query.get(session["user_id"]) form.face.validators = [] if request.method == "GET": form.name.data = user.name form.email.data = user.email form.phone.data = user.phone form.info.data = user.info form.face.data = user.face if form.validate_on_submit(): data = form.data if form.face.data: file_face = secure_filename(form.face.data.filename) if not os.path.exists(app.config["UP_DIR"]): os.makedirs(app.config["UP_DIR"]) os.chmod(app.config["UP_DIR"], stat.S_IRWXO) user_face = change_filename(file_face) form.face.data.save(app.config["UP_DIR"] + user_face) user.face = user_face name_count = User.query.filter_by(name=data["name"]).count() if data["name"] != user.name and name_count == 1: flash("昵称已经存在", "err") return redirect(url_for("home.user")) user.name = data["name"] email_count = User.query.filter_by(email=data["email"]).count() if data["email"] != user.email and email_count == 1: flash("邮箱已经存在", "err") return redirect(url_for("home.user")) user.email = data["email"] phone_count = User.query.filter_by(phone=data["phone"]).count() if data["phone"] != user.phone and phone_count == 1: flash("电话已经存在", "err") return redirect(url_for("home.user")) user.phone = data["phone"] user.info = data["info"] db.session.add(user) db.session.commit() flash("修改成功", "ok") return redirect(url_for("home.user")) return render_template("home/user.html", form=form, user=user)
def user(): form = UserdetailForm() user = User.query.get(int(session["user_id"])) if request.method == "GET": form.name.data = user.name form.email.data = user.email form.phone.data = user.phone form.info.data = user.info if form.validate_on_submit(): data = form.data file_face = secure_filename(form.face.data.filename) if not os.path.exists(app.config["FC_DIR"]): os.makedirs(app.config["FC_DIR"]) os.chmod(app.config["FC_DIR"], 777) user.face = change_filename(file_face) form.face.data.save(app.config["FC_DIR"] + user.face) name_count = User.query.filter_by(name=data["name"]).count() if data["name"] != user.name and name_count == 1: flash("name already exists!", "error") return redirect(url_for("home.user")) email_count = User.query.filter_by(email=data["email"]).count() if data["email"] != user.email and email_count == 1: flash("email already exists!", "error") return redirect(url_for("home.user")) phone_count = User.query.filter_by(phone=data["phone"]).count() if data["phone"] != user.phone and phone_count == 1: flash("phone already exists!", "error") return redirect(url_for("home.user")) user.name = data["name"] user.email = data["email"] user.phone = data["phone"] user.info = data["info"] db.session.add(user) db.session.commit() flash("modification succeeded", "ok") return redirect(url_for("home.user")) return render_template("home/user.html", form=form, user=user)
def user(): form = UserdetailForm() user = User.query.get(int(session["user_id"])) if user.face is not None: form.face.validators = [] if request.method == "GET": form.name.data = user.name form.email.data = user.email form.phone.data = user.phone form.info.data = user.info if form.validate_on_submit(): data = form.data if not os.path.exists(DevConfig.UP_DIR + "users" + os.sep): os.makedirs(DevConfig.UP_DIR + "users" + os.sep) os.chmod(DevConfig.UP_DIR + "users" + os.sep, "rw") if form.face.data.filename != '': old_face = user.face file_face = secure_filename(form.face.data.filename) user.face = change_filename(file_face) form.face.data.save(DevConfig.UP_DIR + "users" + os.sep + user.face) if old_face is not None and os.path.exists(DevConfig.UP_DIR + "users" + os.sep + old_face): os.remove(DevConfig.UP_DIR + "users" + os.sep + old_face) user.name = data["name"] user.email = data["email"] user.phone = data["phone"] user.info = data["info"] db.session.add(user) db.session.commit() flash("修改成功!", "ok") return render_template("home/user.html", form=form, user=user)