def register_handler(): form = RegisterForm(request.forms) dic = {"status": "error", "reason": None} if form.validate(): email = form.email.data.lower() password = red.hget('userinfo:%s' % email, 'password') dic["status"], dic["reason"] = "success", u"帐号确认中..." if not password: # 若用户不存在,即邮箱未注册,则邮箱可用 now = datetime.datetime.now() name = email.split('@')[0] red.hmset('userinfo:%s' % email, {'password': encrypt(form.password.data), 'name': name, 'visited_times': 0, 'date_join': now}) s = request.environ.get('beaker.session') s['username'] = email s.save() red.set('userinfo:%s:visited_times' % email, 0) else: dic["status"], dic["reason"] = "invalid", u"用户名已存在" form_errors = (form.email.errors if form.email.errors else form.password.errors) if form_errors: dic['status'] = "invalid" dic['reason'] = form_errors return dic
def register(): form = RegisterForm(request.form) if request.method == 'POST' and form.validate(): name = form.name.data email = form.email.data username = form.username.data # TODO password = sha256_crypt.encrypt(str(form.password.data)) user = User(name, email, username, password) if db.register_user(user): flash('You are now registered and can log in', 'success') return redirect(url_for('index_page.index')) else: flash('User already exist!', 'danger') return render_template('register.html', form=form) return render_template('register.html', form=form)
def post(self): form = RegisterForm(self.request.arguments) dic = {"status": "error", "reason": None} if form.validate(): password = yield tornado.gen.Task(client.hget, 'userinfo:%s' % form.email.data, 'password') dic["status"], dic["reason"] = "success", u"帐号确认中..." if not password: # database handle with client.pipeline(transactional=True) as pipe: pipe.hset('subchannels:%s' % form.email.data, u'Nemesis', 0) # add accounts to channel pipe.sadd('subunames:%s' % u'Nemesis', form.email.data) pipe.hset('userinfo:%s' % form.email.data, 'password', form.password.data) yield tornado.gen.Task(pipe.execute) # print res, len(res) self.set_secure_cookie('username', form.email.data) else: dic["status"], dic["reason"] = "invalid", u"用户名已存在" form_errors = form.email.errors if form.email.errors else form.password.errors if form_errors: dic['status'] = "invalid" dic['reason'] = form_errors self.write(dic)
def test_email_already_registered(self): form = RegisterForm(email='*****@*****.**', password='******', password2='password') self.assertFalse(form.validate())
def test_invalid_password_format(self): form = RegisterForm(username='******', email='*****@*****.**', password='******', password2='different') self.assertFalse(form.validate())
def test_validate_success_register_form(self): form = RegisterForm(username='******', email='*****@*****.**', password='******', password2='password') self.assertTrue(form.validate())