def register(): error = None form = RegisterForm() if form.validate_on_submit() and request.method == "POST": validate_code = session['validate_code'] validate = form.code.data if validate.lower() != validate_code.lower(): return jsonify(judge=False, error=_('The validate code is error')) else: useremail = User.load_by_email(form.email.data) username = User.load_by_name(form.name.data) if username is not None: error = _('The name has been registered') return jsonify(judge=False, error=error) elif useremail is not None: error = _('The email has been registered') return jsonify(judge=False, error=error) else: account = User() account.name = form.name.data, account.email = form.email.data, account.passwd = form.passwd.data, account.roles = 'visitor' account.registered_time = datetime.now() '''邮箱验证''' token = email_token(account.email) confirm_url = url_for('auth.confirm', token=token, _external=True) html = render_template('templet/email.html', confirm_url=confirm_url) subject = _("Please confirm your email") # email_send(account.email, html, subject) account.send_email_time = datetime.now() db.session.add(account) db.session.commit() login_user(account) identity_changed.send(current_app._get_current_object(), identity=Identity(account.id)) flash(_('An email has been sent to your.Please receive')) return jsonify(judge=True, error=error) else: if form.errors: return return_errors(form) else: pass return render_template('auth/register.html', form=form)
def create_user(username, email, password): if username is None: username = input('Username(default admin):') or 'admin' if email is None: email = input('Email:') if password is None: password = getpass('Password:'******'Super' user.confirmed_time = datetime.utcnow() db.session.add(user) db.session.commit()
def validate(self): rv = BaseForm.validate(self) if not rv: return False user = User.query.filter_by(name=self.name.data).first() print(user) if user is None: self.name.errors.append('Unknown username') return False a = User() if not user.check_password(self.passwd.data): self.passwd.errors.append('Invalid password') return False self.user = user return True
def create_user(username, email, password): if username == 'admin': username = input('Username(default admin):') if email is None: email = input('Email:') if password is None: password = getpass('Password:'******'super').first() if role is None: role = Role() role.rolename = 'super' user.roles.append(role) db.session.add(user) db.session.commit()
def register(): error = None form = RegisterForm() if form.validate_on_submit() and request.method == "POST": validate_code = session['validate_code'] validate = form.code.data if validate.lower() != validate_code.lower(): return jsonify(judge=False, error=u'验证码错误') else: useremail = User.load_by_email(form.email.data) username = User.load_by_name(form.name.data) if username is not None: error = u'用户名已存在' return jsonify(judge=False, error=error) elif useremail is not None: error = u'邮箱已被注册' return jsonify(judge=False, error=error) else: account = User(name=form.name.data, email=form.email.data, passwd=form.passwd.data) userinfor = UserInfor() usersetting = UserSetting() roles = Role(name='unconfirmed', rank=1) account.infor = userinfor account.setting = usersetting account.roles.append(roles) '''邮箱验证''' token = email_token(account.email) confirm_url = url_for('auth.confirm', token=token, _external=True) html = render_template('templet/email.html', confirm_url=confirm_url) subject = "请验证你的邮箱" email_send(account.email, html, subject) db.session.add(account) db.session.commit() '''记录用户数''' redis_data.hincrby('user', 'all:count', 1) login_user(account) identity_changed.send(current_app._get_current_object(), identity=Identity(account.id)) '''发送邮件时间''' from time import time time = int(time()) + 28800 user = '******' % str(current_user.id) redis_data.hset(user, 'send_email_time', time) flash(u'一封验证邮件已发往你的邮箱,請查收.') return jsonify(judge=True, error=error) else: if form.errors: return return_errors(form) else: pass if request.args.get('mode') == 'agree': return render_template('auth/register.html', form=form, error=error) else: return render_template('auth/register_service.html', form=form, error=error)