def user(): form = UserForm() 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 # 获取到头像文件 face_name = form.face.data.filename # 判断文件夹是否存在,如果不存在就创建 if not os.path.exists(app.config['UP_DIR'] + "/user/"): os.mkdir(app.config["UP_DIR"] + "/user/") # 对头像文件进行重命名 face = change_filename(face_name) # 保存头像到本地文件 form.face.data.save(app.config["UP_DIR"] + "/user/" + face) # 进行修改入库操作 user.name = data.get('name') user.email = data.get('email') user.phone = data.get('phone') user.face = face user.info = data.get('info') db.session.add(user) db.session.commit() flash("资料修改成功") return redirect(url_for('home.user')) return render_template("home/user.html", form=form, user=user)
def user(): ''' 用户信息 ''' user = User.query.filter_by(id=session["user_id"]).first() form = UserForm() 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(): new_name = form.name.data if User.query.filter_by(name=new_name).first(): flash("用户名已存在", "success") return redirect(url_for("home.user")) user.name = new_name user.email = form.email.data user.phone = form.phone.data user.info = form.info.data db.session.add(user) db.session.commit() flash("用户信息已修改", "success") return render_template("home/user.html", form=form, user=user)
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(): username = get_username() form=UserForm() user=User.query.get_or_404(int(session['user_id'])) if request.method=="GET": form.name.data=user.name form.email.data=user.email form.info.data=user.info form.face.data=user.face form.phone.data=user.phone if form.validate_on_submit(): data=form.data if not os.path.exists(app.config['UPUSER_DIR']): os.makedirs(app.config["UPUSER_DIR"]) os.chmod(app.config["UPUSER_DIR"],os.O_RDWR) if form.face.data.filename != None: file_url = secure_filename(form.face.data.filename) url = change_filename(file_url) form.face.data.save(app.config["UPUSER_DIR"] + url) name_count=User.query.filter_by(name=data['name']).count() if name_count==1 and user.name != data['name']: flash('昵称重复','err') return redirect(url_for('home.user')) phone_count = User.query.filter_by(phone=data['phone']).count() if phone_count == 1 and user.phone != data['phone']: flash('手机号重复', 'err') return redirect(url_for('home.user')) email_count = User.query.filter_by(email=data['email']).count() if email_count == 1 and user.email != data['email']: flash('邮箱重复', 'err') return redirect(url_for('home.user')) user.name=data['name'] user.phone=data['phone'] user.face = url user.email=data['email'] user.info=data['info'] db.session.add(user) db.session.commit() flash('修改成功','ok') return redirect(url_for('home.user')) if username != None: context = { 'login': username } else: context = { 'login': "******" } return render_template("home/user/user.html",context=context,form=form,user=user)
def register(): form = UserForm() if form.validate_on_submit(): data = form.data user = User( name=data['name'], email=data['email'], phone=data['phone'], pwd=generate_password_hash(data['pwd']) ) db.session.add(user) db.session.commit() flash('用户注册成功', 'ok') session['user'] = user.name return redirect(url_for('home.index')) return render_template('home/register.html', form=form)
def user(): form = UserForm() user = User.query.get(session["user_id"]) form.img.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 name_count = User.query.filter_by(name=data["name"]).count() if name_count and user.name != data["name"]: flash("用户名已存在", "Error") email_count = User.query.filter_by(email=data["email"]).count() if email_count and user.email != data["email"]: flash("邮箱已存在", "Error") phone_count = User.query.filter_by(phone=data["phone"]).count() if phone_count and user.phone != data["phone"]: flash("手机号码已存在", "Error") if not os.path.exists(app.config["IMG_DIR"]): os.makedirs(app.config["IMG_DIR"]) os.chmod(app.config["IMG_DIR"], "rw") if form.img.data: img_filename = secure_filename(form.img.data.filename) user.img = change_filename(img_filename) form.img.data.save(app.config["IMG_DIR"] + user.img ) 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(): form=UserForm() user=User.query.filter_by(name=session.get('user',None)).first() form.username.data=user.name form.email.data=user.email form.phone.data=user.phone form.info=user.info if form.validate_on_submit(): username=request.form['username'] email=request.form['email'] face=form.face.data phone=request.form['phone'] print(phone) info=request.form['info'] if User.query.filter_by(name=username).count() and username!=user.name: flash('更新失败,用户名已存在') return redirect(url_for('home.user')) if User.query.filter_by(email=email).count() and email!=user.email: flash('更新失败,邮箱已注册') return redirect(url_for('home.user')) if not os.path.exists(app.config['FC_DIR']): os.makedirs(app.config['FC_DIR']) if face: face_name=change_file(face.filename) if user.face and os.path.exists(os.path.join(app.config['FC_DIR'],user.face)): os.remove(os.path.join(app.config['FC_DIR'],user.face)) form.face.data.save(os.path.join(app.config['FC_DIR'],face_name)) user.face = face_name user.name=username user.email=email user.phone=phone user.info=info db.session.add(user) db.session.commit() flash('更新信息成功') return redirect(url_for('home.logout')) return render_template('home/user.html',form=form)
def user(): form = UserForm() form.face.validators = [] user = User.query.get_or_404(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 user_count = User.query.filter_by(name=data["name"]).count() if user_count == 1 and user.name != data["name"]: flash(u"用户已存在!", 'err') return redirect(url_for('home.user')) email_count = User.query.filter_by(email=data["email"]).count() if email_count == 1 and user.email != data["email"]: flash(u"邮箱已存在!", 'err') return redirect(url_for('home.user')) phone_count = User.query.filter_by(phone=data["phone"]).count() if phone_count == 1 and user.phone != data["phone"]: flash(u"手机号码已存在!", 'err') return redirect(url_for('home.user')) if form.face.data.filename != "": file_face = secure_filename(form.face.data.filename) user.face = change_filename(file_face) form.face.data.save(app.config["UP_DIR"] + "users/" + user.face) user.name = data["name"] user.info = data["info"] user.email = data["email"] user.phone = data["phone"] db.session.add(user) db.session.commit() flash(u"用户信息修改成功!", 'OK') return redirect(url_for('home.user')) return render_template("home/user.html", form=form, user=user)