def load_user(user_id): """Flask-Login hook to load a User instance from ID.""" u = mongo.db.users.find_one({"username": user_id}) if not u: return None return User(u['username'])
def login(): if current_user.is_authenticated: return redirect(url_for('products_list')) form = LoginForm(request.form) error = None if request.method == 'POST' and form.validate(): username = form.username.data.lower().strip() password = form.password.data.lower().strip() user = mongo.db.users.find_one({"username": form.username.data}) if user and User.validate_login(user['password'], form.password.data): user_obj = User(user['username']) login_user(user_obj) return redirect(url_for('products_list')) else: error = 'Incorrect username or password.' return render_template('user/login.html', form=form, error=error)
def register(): if current_user.is_authenticated: return redirect(url_for('main.home')) form = RegistrationForm() if form.validate_on_submit(): 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('Your account has been created! and you are now able to login', 'success') return redirect(url_for('users.login')) return render_template('register.html', title='Register', form=form)
def my_register(): mobile = request.form.get('mobile') imageCode = request.form.get('imageCode') passwd = request.form.get('passwd') passwd2 = request.form.get('passwd2') # 验证参数是否完整 if not all([mobile, imageCode, passwd, passwd2]): return jsonify(status_code.USER_REGISTER_PARAMS_VALID_ERROR) # 验证验证码是否输入正确 if session.get('code') != imageCode: return jsonify(status_code.USER_REGISTER_CODE_ERROR) user = User() # 验证手机号是否符合规则 if not re.match(r'^1\d{10}$', mobile): return jsonify(status_code.USER_REGISTER_MOBILE_INVALID) # 验证两次密码是否输入一直 if not passwd == passwd2: return jsonify(status_code.USER_REGISTER_PASSWORD_SAME) # 验证手机号码是否存在 if User.query.filter(User.phone == mobile).count(): return jsonify(status_code.USER_REGISTER_MOBILE_EXSITS) user = User() user.username = mobile user.phone = mobile user.password = passwd try: user.add_update() return jsonify(status_code.SUCCESS) except: return jsonify(status_code.DATABASE_ERROR)
def register(request): if request.session.get('is_login', None): return redirect('/base/') if request.method == 'POST': register_form = RegisterForm(request.POST) message = "请检查填写的内容!" if register_form.is_valid(): username = register_form.cleaned_data.get('username') password = register_form.cleaned_data.get('password') confirmPassword = register_form.cleaned_data.get('confirmPassword') email = register_form.cleaned_data.get('email') sex = register_form.cleaned_data.get('sex') if password != confirmPassword: message = '两次输入的密码不同!' return render(request, 'django_sb_admin/register.html', locals()) else: same_user_name = User.objects.filter(username=username) if same_user_name: message = '用户名已存在!' return render(request, 'django_sb_admin/register.html', locals()) same_user_email = User.objects.filter(email=email) if same_user_email: message = '该邮箱已被注册了!' return render(request, 'django_sb_admin/register.html', locals()) new_user = User() new_user.username = username new_user.password = password new_user.email = email new_user.sex = sex new_user.save() return redirect('/login/') else: return render(request, 'django_sb_admin/register.html', locals()) else: register_form = RegisterForm() return render(request, 'django_sb_admin/register.html', {'register_form': register_form})
def reset_token(token): 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(): hashed_password = bcrypt.generate_password_hash( form.password.data).decode('utf-8') user.password = hashed_password db.session.commit() flash('Your password has been updated! and you are now able to login', 'success') return redirect(url_for('users.login')) return render_template('reset_token.html', title='Reset Password', form=form)
def register(): if current_user.is_authenticated: return redirect(url_for('index')) form = RegistrationForm() if form.validate_on_submit(): data = {} data['username'] = form.username.data data['email'] = form.email.data if is_new_User(db.session, data): newUser = {} newUser['id'] = get_next_userid(db.session) # Doubt newUser['username'] = form.username.data newUser['email'] = form.email.data newUser['password'] = User.set_password(form.password.data) insert_User(db.session, newUser) return redirect(url_for('login')) return render_template('register.html', title='Register', form=form)
def post(self): parse = parser_post.parse_args() user = User() user.name = parse.get('name') # user.password = parse.get('password') # 密码加密处理 user.password = generate_password_hash(parse.get('password')) user.email = parse.get('email') user.icon = parse.get('icon') user.token = str(uuid.uuid4()) returndata = {} users = User.query.filter(User.name == user.name).filter(User.email == user.email) if users.count() > 0: returndata['status'] = 406 returndata['msg'] = '注册失败' returndata['error'] = '用户名和邮箱已存在,请直接登录!' return returndata else: users = User.query.filter(User.email == user.email) if users.count() > 0: returndata['status'] = 406 returndata['msg'] = '注册失败' returndata['error'] = '邮箱已存在,请重新输入' return returndata users = User.query.filter(User.name == user.name) if users.count() > 0: returndata['status'] = 406 returndata['msg'] = '注册失败' returndata['error'] = '用户名已存在,请重新输入!' return returndata db.session.add(user) db.session.commit() # send_mail(user) returndata['status'] = 200 returndata['msg'] = '注册成功' returndata['data'] = user return returndata
def post(self, request): # 获取用户输入数据 username = request.POST.get('username') password = request.POST.get('password') email = request.POST.get('email') # 判断用户名是否存在 try: user = User.objects.get(name=username) except User.DoesNotExist: user = None if user: return render(request, 'myApp/register.html', {'errmsg': "用户名重复"}) # 判断邮箱是否被注册 try: user = User.objects.get(email=email) except User.DoesNotExist: user = None if user: return render(request, 'myApp/register.html', {'errmsg': "该邮箱已被注册"}) # 判断数据是否完整 if not all([username, password, email]): return render(request, 'myApp/register.html', {'errmsg': "数据不完整"}) # 判断邮箱格式是否正确 if not re.match(r'^[a-z0-9][\w.\-]*@[a-z0-9\-]+(\.[a-z]{2,5}){1,2}$', email): return render(request, 'myApp/register.html', {'errmsg': "邮箱格式不正确"}) # 用户注册信息存入数据库 auth_user = UserRe.objects.create_user(username, email, password) user = User() user.name = auth_user.username user.email = auth_user.email user.password = password user.save() # 向用户发送邮件 secret = Secret(settings.SECRET_KEY, 3000) info = {'id': user.id} token = secret.dumps(info) token = token.decode() subject = '美食网站欢迎信息' message = '' sender = settings.EMAIL_FROM receiver = [email] html_message = '<h1>欢迎您:%s</h1>请点击下面链接激活您的账户<br/>' \ '<a href="http://127.0.0.1:8000/user/active/%s">http://127.0.0.1:8000/user/active/%s</a>' % ( username, token, token) send_mail(subject, message, sender, receiver, html_message=html_message) return redirect(reverse('myApp:index'))
def insert_User(session, data): print("YOOOOOOOOOO", data) new = User(id = data['id'], username = data['username'], email=data['email'], password_hash=data['password']) db.session.add(new) db.session.commit() flash("Congratulations, you are a registered user now")