def add_user(): user = User() user.id = 1 user.name = 'Tom' db.session.add(user) db.session.commit() return '创建成功'
def api_admin_insert_user(): userInfo = request.get_json()['params']['data'] son = userInfo['studentNumber'] password = userInfo['initPassword'] oldUser = User.query.get(son) if oldUser: return jsonify({'status': 'fail'}) newUser = User() newUser.sno = son newUser.password = password db.session.add(newUser) db.session.commit() return jsonify({'status': 'success'})
def reset_token(token): #Check for authentication# if current_user.is_authenticated: return redirect(url_for('main.home')) user = User.verify_reset_token(token) if user is None: flash('That is an invalid or expired token', 'warning') return redirect(url_for('users.reset_request')) form = ResetPasswordForm() if form.validate_on_submit(): #add user to database# hashed_password = bcrypt.generate_password_hash( form.password.data).decode('utf-8') user.password = hashed_password db.session.commit() flash(f"Your password has been updated. You are now able to login!", "success") return redirect(url_for('users.login')) return render_template('users/reset_token.html', title='Reset Password', form=form)
def register(): if request.method == 'GET': pass elif request.method == 'POST': username = request.form["username"] password1 = request.form["password1"] password2 = request.form["password2"] error = None if not username: error = "Username is required." elif not password1: error = "Password is required." elif not password2: error = "Repeat password is required." elif not (password1 == password2): error = "Twice password not the same" else: user = User.query.filter_by(username=username).first() if user is not None: error = "User {0} is already registered.".format(username) if error is None: # the name is available, store it in the database and go to # the login page user = User(username=username, password=generate_password_hash(password1)) db.session.add(user) db.session.commit() return redirect(url_for("auth.login")) flash(error) else: abort(401) return render_template('auth/register.html')
def register(): """注册""" if request.method == 'GET': return render_template('register.html') if request.method == 'POST': mobile = request.form.get('mobile') # imagecode = request.form.get('imagecode') password = request.form.get('password') password2 = request.form.get('password2') # 验证参数是否填完 if not all([mobile, password, password2]): return jsonify(status_code.PARAMS_ERROR) # 验证手机号码格式是否正确 if not re.fullmatch(r'^1[34578]\d{9}$', mobile): return jsonify(status_code.USER_LOGIN_PHONE_ERROR) # 验证手机号是否已经注册过 if User.query.filter_by(phone=mobile).first(): return jsonify(status_code.USER_REGISTER_USER_PHONE_EXSITS) # 若全部验证成功 user = User() user.name = mobile user.phone = mobile user.password = password try: user.add_update() return jsonify(status_code.SUCCESS) except: return jsonify(status_code.USER_REGISTER_USER_ERROR)
def register(): """注册""" if request.method == 'POST': phone = request.form.get('mobile') pwd = request.form.get('password') password2 = request.form.get('passwd2') #验证完整性 if not all([phone, pwd, password2]): return jsonify(status_code.USER_REGISTER_DATA_NOT_NULL) #验证手机正确性 if not re.match(r'1[34578]\d{9}$', phone): return jsonify(status_code.USER_REGISTER_MOBILE_ERROR) #验证密码 if pwd != password2: return jsonify(status_code.USER_REGISTER_PASSWORD_NOT_VALID) #4.保存数据 user = User.query.filter(User.phone == phone).first() if user: return jsonify(status_code.USER_REGISTER_MPBILE_EXSITS) else: user = User() user.phone = phone user.password = pwd user.name = phone user.add_update() return jsonify(status_code.SUCCESS)
def register(request): if request.method == 'GET': return render(request,'mine/register.html') elif request.method == 'POST': user = User() user.email = request.POST.get('email') user.password = generate_password(request.POST.get('password')) user.name = request.POST.get('name') user.phone = request.POST.get('phone') # 状态保持 user.token = generate_token() user.save() request.session['token'] = user.token return redirect('axf:mine')
def register(): if request.method == 'GET': return render_template('user/register.html') if request.method == 'POST': username = request.form.get('username') email = request.form.get('email') password = request.form.get('password') re_password = request.form.get('re_password') if all([username, password, re_password]): if password == re_password: user = User() user.username = username user.password = password user.email = email db.session.add(user) db.session.commit() return redirect(url_for('user.login')) return render_template('user/register.html')
def register(request): if request.method == 'GET': return render(request, 'register.html') elif request.method == 'POST': user = User() user.username = request.POST.get('username') user.password = genarate_password(request.POST.get('password')) user.token = str(uuid.uuid5(uuid.uuid4(), 'register')) user.save() request.session['token'] = user.token return redirect('app:index')
def test_make_user(): print('request received') new_user = User( #Authentication email = '*****@*****.**', password = '******', auth_token = 'authtoken', #User Personal Info firstName = 'Test', preferedName = 'T', lastName = 'User', middleName = '', birthdate = datetime.date.today(), address = '', phone = '', altemail = '', room = '', yearin = 4, yearoutof = 5, mailbox = 1300, shirtsize = 4, major = '', greekOrg = 0, #Apo stuff # nmClass = db.ReferenceField('NMClass', name='NewMemberClass'), # photopath = '', # APO_id = 1, # family = db.ReferenceField('Family', name='family'), # big = db.ReferenceField('User', name='big'), # littles = db.ListField(db.ReferenceField('User', name='littles')), # semesters = db.ListField(db.ReferenceField('USemester', name='semesters')), ).save() print('save') return new_user.to_json()
def test_follow(self): u1 = User(username='******', email='*****@*****.**') u2 = User(username='******', email='*****@*****.**') db.session.add(u1) db.session.add(u2) db.session.commit() self.assertEqual(u1.followed.all(), []) self.assertEqual(u1.followers.all(), []) u1.follow(u2) db.session.commit() self.assertTrue(u1.is_following(u2)) self.assertEqual(u1.followed.count(), 1) self.assertEqual(u1.followed.first().username, 'susan') self.assertEqual(u2.followers.count(), 1) self.assertEqual(u2.followers.first().username, 'john') u1.unfollow(u2) db.session.commit() self.assertFalse(u1.is_following(u2)) self.assertEqual(u1.followed.count(), 0) self.assertEqual(u2.followers.count(), 0)
def register(): reg_error = False if request.method == 'POST': user = User.query.filter_by( username=request.form.get('username')).first() if user: reg_error = u'用户名已经存在' return render_template('login.html', danger=True, reg_error=reg_error) register_user = User(username=request.form.get('username'), password=request.form.get('password')) db.session.add(register_user) db.session.commit() db.session.close() return redirect(url_for('users.login')) return render_template('login.html')
def register(request): if request.method == "GET": return render(request, 'register.html') elif request.method == "POST": username = request.POST.get("username") password = request.POST.get("password") email = request.POST.get("email") user = User() user.username = username user.password = password user.email = email user.save() return render(request, "register_success.html")
def regist(): # 在添加用户名前,查找数据库是否存在用户 json_data = {} data = {} if request.method == 'POST': print(data) data = request.get_data() print(data) json_data = json.loads(data) print(json_data) tag = User.query.filter_by(name=json_data["name"]).count() # tag = User.query.filter_by(name="weilan3").count() if tag == 1: json_data['SUCCESS'] = 'faile' return json.dumps(json_data, ensure_ascii=False) user = User(name=json_data['name'], password=json_data['password']) db.session.add(user) db.session.commit() json_data['SUCCESS'] = 'SUCCESS' return json.dumps(json_data, ensure_ascii=False)
def registration(): #Check for authentication# if current_user.is_authenticated: return redirect(url_for('main.home')) form = RegistrationForm() if form.validate_on_submit(): #add user to database# hashed_password = bcrypt.generate_password_hash( form.password.data).decode('utf-8') user = User(username=form.username.data, email=form.email.data, password=hashed_password) db.session.add(user) db.session.commit() flash(f"Your account has been created. You are now able to login!", "success") return redirect(url_for('users.login')) return render_template('users/register.html', title="Register", form=form)
def register(request): if request.method == "GET": data = {"title": "注册"} return render(request, 'user/register.html', context=data) elif request.method == "POST": username = request.POST.get("username") email = request.POST.get("email") password = request.POST.get("password") icon = request.FILES.get("icon") # password = hash_str(password) password = make_password(password) user = User() user.u_username = username user.u_password = password user.u_email = email user.u_icon = icon user.save() u_token = uuid.uuid4().hex cache.set(u_token, user.id, timeout=60 * 60 * 24) print(u_token) send_email_activate(username, email, u_token) return redirect(reverse("axf:login"))
def api_user_register(): userInfo = request.get_json()['params'] print(userInfo) name = userInfo['studentName'] email = userInfo['email'] mobile = userInfo['mobile'] password_md5 = userInfo['password'] studentNumber = userInfo['studentNumber'] print(email, mobile, password_md5, studentNumber) # ------------------------------------# # sql reader = User() reader.sno = studentNumber reader.name = name reader.password = password_md5 reader.phone = mobile reader.email = email db.session.add(reader) db.session.commit() # ------------------------------------# return 'success'
def sure_register(): # 取出提交页面中的内容,是个字典 register_dict = request.form mobile = register_dict.get('mobile') password = register_dict.get('password') password2 = register_dict.get('password') # 填写信息不全 if not all([mobile, password, password2]): return jsonify(status_code.USER_REGISTER_PARAMS_ERROR) # 不符合号码规则 if not re.match(r'^1[24578]\d{9}$', mobile): return jsonify(status_code.USER_REGISTE_MOBILE_ERROR) # 用户是否已注册 if User.query.filter(User.phone == mobile).count(): return jsonify(status_code.USER_REGISTE_MOBILE_IS_EXISTS) # 两次输入的密码不相等 if password != password2: return jsonify(status_code.USER_REGISTER_PASSWORD_IS_ERROR) # 创建字段 user = User() user.phone = mobile user.name = mobile user.password = password try: # 保存字段 user.add_update() return jsonify(status_code.SUCCESS) except Exception as e: return jsonify(status_code.DATABASE_ERROR)
def test_follow_posts(self): # create four users u1 = User(username='******', email='*****@*****.**') u2 = User(username='******', email='*****@*****.**') u3 = User(username='******', email='*****@*****.**') u4 = User(username='******', email='*****@*****.**') db.session.add_all([u1, u2, u3, u4]) # create four posts now = datetime.utcnow() p1 = Post(body="post from john", author=u1, timestamp=now + timedelta(seconds=1)) p2 = Post(body="post from susan", author=u2, timestamp=now + timedelta(seconds=4)) p3 = Post(body="post from mary", author=u3, timestamp=now + timedelta(seconds=3)) p4 = Post(body="post from david", author=u4, timestamp=now + timedelta(seconds=2)) db.session.add_all([p1, p2, p3, p4]) db.session.commit() # setup the followers u1.follow(u2) # john follows susan u1.follow(u4) # john follows david u2.follow(u3) # susan follows mary u3.follow(u4) # mary follows david db.session.commit() # check the followed posts of each user f1 = u1.followed_posts().all() f2 = u2.followed_posts().all() f3 = u3.followed_posts().all() f4 = u4.followed_posts().all() self.assertEqual(f1, [p2, p4, p1]) self.assertEqual(f2, [p2, p3]) self.assertEqual(f3, [p3, p4]) self.assertEqual(f4, [p4])
def test_avatar(self): u = User(username='******', email='*****@*****.**') self.assertEqual(u.avatar(128), ('https://www.gravatar.com/avatar/' 'd4c74594d841139328695756648b6bd6' '?d=identicon&s=128'))
def test_password_hashing(self): u = User(username='******') u.set_password('cat') self.assertFalse(u.check_password('dog')) self.assertTrue(u.check_password('cat'))
def register_p(): username = request.form.get('username') pwd1 = request.form.get('pwd1') pwd2 = request.form.get('pwd2') img_file = request.files.get('avatar') company = request.form.get('company') weibo = request.form.get('weibo') job = request.form.get('job') we_chat = request.form.get('we_chat') e_mail = request.form.get('e_mail') we_public = request.form.get('we_public') info = request.form.get('info') if not all([username, pwd1, pwd2]): return '请填写必填字段' if pwd1 != pwd2: return '两次输入的密码不符合' user = User.query.filter_by(name=username).first() if user: return '该用户名已经存在' else: user = User() user.name = username user.password = pwd1 image_path = os.path.join(UPLOAD_DIR, img_file.filename) img_file.save(image_path) user.avatar = os.path.join('upload', img_file.filename) user.company = company user.weibo = weibo user.job = job user.we_chat = we_chat user.e_mail = e_mail user.info = info try: user.add_update() except Exception as e: db.session.rollback() return '创建用户失败' return redirect(url_for('user.login_g'))