def register_user(): if request.method == "POST": userName = request.form.get("userName") passWord = request.form.get("passWord") code = request.form.get("code") if code != session.get('code'): result = {'code': "10004", 'msg': "code is error!"} return jsonify(result) if User().getUserByName(userName): result = {'code': "10004", 'msg': "user existed!"} return jsonify(result) if not userName or not passWord: result = { 'code': "10004", 'msg': "username or password cant'n be bull" } return jsonify(result) user = User().registerUser(userName, passWord) session['userid'] = user.id user.updateCredit(50) credit = Credit() credit.creaet(50, "注册帐号") result = {'code': "10001", 'msg': "register ok"} return jsonify(result)
def query_users_from_db(): users = [] sql_select = "SELECT * FROM users" args = [] cur = g.db.execute(sql_select, args) for item in cur.fetchall(): user = User() user.fromList(item[1:]) users.append(user) return users
def query_user_by_name(user_name): sql_select = "SELECT * FROM users where name=?" args = [user_name] cur = g.db.execute(sql_select, args) items = cur.fetchall() if len(items) < 1: return None first_item = items[0] user = User() user.fromList(first_item[1:]) return user
def user_regist(): form = RegistForm() if form.validate_on_submit(): # 查看用户名是否已经存在 user_name = form.user_name.data user_x = query_user_by_name(user_name) if user_x: flash("用户名已经存在!", category='err') return render_template('user_regist.html', form=form) # 如果用户不存在,执行注册 user = User() user.name = form.user_name.data user.pwd = form.user_pwd.data user.email = form.data['user_email'] user.age = form.user_edge.data user.birthday = form.data["user_birthday"] filestorage = request.files["user_face"] user.face = secure_filename_with_uuid(filestorage.filename) # 如果用户不存在,执行插入操作 insert_user_to_db(user) # 保存用户头像文件 user_folder = os.path.join(app.config["UPLOADS_FOLDER"], user.name) create_folder(user_folder) filestorage.save(os.path.join(user_folder, user.face)) flash("用户注册成功!", category='ok') return redirect(url_for("user_login", username=user.name)) return render_template('user_regist.html', form=form)
def user_regist(): form = RegistForm() if form.validate_on_submit(): # 查看用户名是否已经存在 user_name = form.user_name.data user_x = query_user_by_name(user_name) if user_x: flash("用户名已经存在!", category='err') return render_template('user_regist.html', form=form) # 如果用户不存在,执行注册 user = User() user.name = form.user_name.data user.pwd = form.user_pwd.data user.email = form.data['user_email'] user.age = form.user_edge.data user.birthday = form.data["user_birthday"] filestorage = request.files["user_face"] user.face = secure_filename_with_uuid(filestorage.filename) # 如果用户不存在,执行插入操作 insert_user_to_db(user) # 保存用户头像文件 try: photosSet.save(storage=filestorage, folder=user.name, name=user.face) flash("用户注册成功!", category='ok') return redirect(url_for("user_login", username=user.name)) except UploadNotAllowed: flash("头像文件格式不对!", category='err') return render_template('user_regist.html', form=form) return render_template('user_regist.html', form=form)
def user_regist(): # 注册 form = RegistForm() if form.validate_on_submit( ): # 检查提交方式是否为post 验证forms.py定义的validators 验证是否通过 # 检查用户上传的头像文件名是否符合要求 if not check_files_extension([form.user_face.data.filename], ALLOWED_IMAGEEXTENSIONS): flash("头像文件格式错误!", category="err") return render_template("user_regist.html", form=form) # 查看用户是否存在 user_name = form.user_name.data user_one = query_user_by_name(user_name) if user_one: # 返回注册界面,重新注册 flash("用户名已存在!", category="err" ) # Flashes a message to the next request 闪现一条消息到下一次消息请求 return render_template("user_regist.html", form=form) # print("form", form.user_name.data) # print("form", form.data) # print("form", form.data["user_name"]) # print("request.form", request.form) user = User() # user.name = request.form["user_name"] user.name = form.user_name.data # user.pwd = request.form["user_pwd"] user.pwd = form.user_pwd.data # user.age = request.form["user_age"] user.age = form.user_age.data # user.birthday = request.form["user_birthday"] user.birthday = form.user_birthday.data # user.email = request.form["user_email"] user.email = form.user_email.data # user.face = request.form["user_face"] # user.face = form.user_face.data # filerstorage=form.user_face.data filerstorage = request.files["user_face"] # 获取头像文件 user.face = secure_filename_with_uuid( filerstorage.filename ) # secure_filename 文件名安全性检测,如果文件名有特殊字符,会将特殊字符转义,没有就返回原文件名 # print(user.face) # 如果不存在执行插入操作 # 插入一条数据 instert_user_to_db(user) # 保存用户头像文件 user_folder = os.path.join(app.config["UPLOADS_FOLDER"], user.name) create_folder(user_folder) # 创建用户文件夹 filerstorage.save(os.path.join(user_folder, user.face)) flash("注册成功!", category="ok") # username作为查询参数带到url中去 # 重定向页面 生成url 执行 user_login 函数 跳转到登录界面 return redirect(url_for("user_login", username=user.name)) return render_template("user_regist.html", form=form)
def query_user_by_name(user_name): sql_select = "SELECT *FROM users WHERE name =?" args = [user_name] cur = g.db.execute(sql_select, args) items = cur.fetchall() # 取出第一条数据 if len(items) < 1: return None first_item = items[0] user = User() # item[0] 为id # user.name = first_item[1] # user.pwd = first_item[2] # user.email = first_item[3] # user.age = first_item[4] # user.birthday = first_item[5] # user.face = first_item[6] user.fromList(first_item[1:]) # 第一位为id 从第二位才开始赋值 return user
def user_regist(): form = RegistForm() if form.validate_on_submit(): user_name = request.form['user_name'] #判断用户是否存在 user_x = User.query.filter_by(name=user_name).first() if user_x: flash('用户已经存在', category='err') else: user = User() user.name = user_name user.password = request.form['user_password'] user.email = request.form['user_email'] user.phone = request.form['user_phone'] user.sign = request.form['user_sign'] user.uuid = str(uuid4().hex) # 获取头像文件,是一个对象(文件名,属性) filestorage = request.files['user_face'] # 头像文件名称 if filestorage.filename != '': fix = '.' + str(filestorage.filename).split('.')[-1] name = str(uuid4().hex) + fix folder = user.uuid try: fname = userfiles.save(storage=filestorage, folder=folder, name=name) # 创建保存压缩图返回文件名 fname_small = create_face(path=os.path.join(app.config['USERS_FILES'], folder), filename=name, base_width=200) fpath = userfiles.path(fname) os.remove(fpath) except Exception as i: print(i) return render_template('404.html') user.face = fname_small """写入""" db.session.add(user) db.session.commit() flash('注册成功', category='ok') return redirect(url_for('user_login', username=user.name)) return render_template('user_regist.html', form=form)
def query_users_from_db(): users = [] sql_select = "SELECT *FROM users" args = [] cur = g.db.execute(sql_select, args) for item in cur.fetchall(): user = User() # item[0] 为id # user.name = item[1] # user.pwd = item[2] # user.email = item[3] # user.age = item[4] # user.birthday = item[5] # user.face = item[6] user.fromList(item[1:]) # 第一位为id 从第二位才开始赋值 users.append(user) return users pass
def register(): form = RegisterForm() if form.validate_on_submit(): user = User(username=form.username.data, password=form.password.data, email=form.email.data) db.session.add(user) db.session.commit() # 发激活邮件 # 生成一个token,令牌,包含失效,包含用户信息() token = user.generate_activate_token() send_mail([user.email], '激活邮件', 'email/activate', username=user.username, token=token) flash('注册成功') return redirect(url_for('users.login')) return render_template('users/register.html', form=form)
def addUser(user_name, user_age, user_id): leader = User(user_name=user_name, user_age=user_age, user_id=user_id) db.session.add(leader) try: db.session.commit() print("user添加成功!") except Exception as e: print(e) db.session().rollback() return False return True
def success_change_email(token): data = User.check_token(token) if data: user = User.query.get(data['user_id']) if user.email != data['newemail']: user.email = data['newemail'] db.session.add(user) flash('邮箱修改成功,请查看个人信息') # return redirect(url_for('main.pzl')) return redirect(url_for('users.login')) else: flash('邮件已失效,请重新发送') return redirect(url_for('users.change_email'))
def change_email(): form = EmailForm() if form.validate_on_submit(): newemail = form.email.data infoDict = {'user_id': current_user.id, 'newemail': newemail} token = User.generate_token(infoDict) send_mail([newemail], '修改邮箱邮件', 'email/change_email', username=current_user.username, token=token) flash('邮件已发送,注意查收') form.email.data = '' return render_template('users/change_email.html', form=form)
def post(self): uname = request.form['uname'] passwd = request.form['passwd'] real_passwd = request.form['real_passwd'] nick_name = request.form['nick_name'] sex = request.form['sex'] phone = request.form['phone'] cid = request.form['cid'] user = User.query.filter_by(uname=uname).first() if user: return render_template('register.html', msg='用户名已存在') else: user = User(uname, passwd, nick_name, sex, phone, cid) clazz = Clazz.query.filter_by(id=cid).first() user.clazzs.append(clazz) db.session.add(user) db.session.commit() return redirect(url_for('user.login'))
def login_in(): if request.method == "POST": username = request.form.get("userName") password = request.form.get("passWord") ver_code = request.form.get("code") if ver_code.lower() != session.get("verifyCode").lower(): result = {'code': "10004", 'msg': "code is error!"} return jsonify(result) user = User().getUserByAll(username, password) if not user: result = {'code': "10004", 'msg': "username or password is error!"} return jsonify(result) else: session['username'] = user.username session['userid'] = user.id result = {'code': "10001", 'msg': "success", "data": user.username} resp = make_response(jsonify(result)) # 设置响应体 resp.set_cookie("username", user.username, max_age=3600) resp.set_cookie("userid", str(user.id), max_age=3600) return resp
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.orm import sessionmaker from apps.db import engine from apps.model import User, Tag, Article, Credit, Comment Base = declarative_base() faker = Factory.create() Session = sessionmaker(bind=engine) session = Session() faker_users = [User( username=faker.name(), password=faker.word(), email=faker.email(), ) for i in range(10)] session.add_all(faker_users) faker_tags= [Tag(name=faker.word()) for i in range(20)] session.add_all(faker_tags) faker_article=[] for i in range(100): article = Article( titile=faker.sentence(), content=' '.join(faker.sentences(nb=random.randint(70, 100))), author=random.choice(faker_users), pubdate = faker.date_time(),