def test_login_with(): data = { "openid": "0395BA18A5CD6255E5BA185E7BEBA242", "access_token": "12345678-SaMpLeTuo3m2avZxh5cjJmIrAfx4ZYyamdofM7IjU", "expires_in": 1382686496 } User.login_with('weixin', data)
def test_request_mobile_phone_verify(): try: User.request_mobile_phone_verify('1861111' + str(random.randrange(1000, 9999))) except LeanCloudError as e: if e.code not in (213, 601): raise e
def test_request_password_reset(): try: User.request_password_reset('*****@*****.**') except LeanCloudError as e: print(e.code) assert u'请不要往同一个邮件地址发送太多邮件。' in e.error \ or 'Too many emails sent to the same email address' in str(e)
def POST(self): it = web.input() try: grade = it.get('grade') token = it.get('token') tryId = it.get('tryId') now = int(it.get('now', time.time())) user = User().become(token) except leancloud.errors.LeanCloudError: return {'code': 1} obj = TryWords.query.get(tryId) delay = obj.get('delay') tryCount = obj.get('tryCount') if grade == 'c': delay = delay / 2.0 elif grade == 'b': delay = delay / 1.1 elif grade == 'a': if delay > 30 * 86400 - 1: getObj = GetWords() getObj.set('userId', user.id) getObj.set('wordAndExpl', obj.get('word')+'|'+obj.get('expl')) getObj.set('tryCount', obj.get('tryCount') + 1) getObj.set('totalTime', now - obj.get('startTime')) getObj.save() obj.destroy() return {'code': 0} delay *= math.exp(math.log(30) / (user.get('avgGetCount') - 0.1)) delay = min(delay, 40 * 86400) recall_day = int((now + 8 * 3600 + delay) / 86400) obj.set('delay', delay) obj.set('tryCount', tryCount + 1) obj.set('recallDay', recall_day) obj.save() return {'code': 0}
def test_reset_password_by_sms_code(): # type: () -> None try: User.reset_password_by_sms_code(str(random.randrange(100000, 999999)), "password") except LeanCloudError as e: if e.code != 603: raise e
def test_request_password_reset_by_sms_code(): try: User.request_password_reset_by_sms_code( '1861111' + str(random.randrange(1000, 9999))) except LeanCloudError as e: if e.code not in (213, 601): raise e
def test_login(): user = User() user.set('username', 'user1_name') user.set('password', 'password') user.login() user = User() user.login('user1_name', 'password')
def register(): try: user = User() user.set("username", request.get_json()["username"]) user.set("password", request.get_json()["password"]) user.sign_up() except LeanCloudError, e: raise e
def test_request_email_verify(): # type: () -> None try: User.request_email_verify('*****@*****.**') except LeanCloudError as e: assert '邮件验证功能' in str(e) \ or '请不要往同一个邮件地址发送太多邮件' in str(e) \ or 'Too many emails sent to the same email address' in str(e)\ or 'Please enable verifying user email option in application settings' in str(e)
def validate_password(self, field): if self.email.data: try: user = User() user.login(self.email.data, self.password.data) self.user = user except LeanCloudError: raise ValueError("密码不正确.")
def signin_user(email, password, permenent=True): """Sign in user.""" session.permanent = True leanuser = User() leanuser.login(email, password) token = leanuser.get_session_token() session['session_token'] = token
def test_request_email_verify(): # type: () -> None try: User.request_email_verify('*****@*****.**') except LeanCloudError as e: assert '邮件验证功能' in str(e) \ or '请不要往同一个邮件地址发送太多邮件' in str(e) \ or 'Too many emails' in str(e)\ or 'Please enable the option to verify user emails in application settings.' in str(e)
def test_login(): # type: () -> None user = User() user.set('username', 'user1_name') user.set('password', 'password') user.login() user = User() user.login('user1_name', 'password')
def test_request_email_verify(): # type: () -> None try: User.request_email_verify("*****@*****.**") except LeanCloudError as e: assert ( "邮件验证功能" in str(e) or "请不要往同一个邮件地址发送太多邮件" in str(e) or "Too many emails" in str(e) or "Please enable the option to verify user emails in application settings." in str(e))
def POST(self, _phone, _password): user = User() try: user.login(_phone, _password) token = user.get_session_token() trueName = user.get("nickName") web.setcookie('session', token) return {'status': 0, 'token': token, 'name': trueName} except leancloud.errors.LeanCloudError: return {'status': 1, 'message': 'login failed'}
def test_follower_query(): user1 = User() user1.login('user1_name', 'password') user2 = User() user2.login('user2_name', 'password') user2.follow(user1.id) query = User.create_follower_query(user1.id) assert query.first().id == user2.id
def validate_password(self, field): user = User() try: user.login(self.account.data.strip(), self.password.data) self.user = user except Exception, e: if e.code: if e.code == 210: raise ValueError('用户名/密码不正确') if e.code == 211: raise ValueError('账号不存在') raise ValueError('用户名/密码不正确')
def POST(self): it = web.input() username = it.get('username', '') password = it.get('password', '') user = User() try: user.login(username, password) token = user.get_session_token() web.setcookie('token', token) return {'code': 0, 'token': token} except leancloud.errors.LeanCloudError: return {'code': 1}
def register(): if request.method == 'POST': username = request.form.get('username') password = request.form.get('password') next_url = request.args.get('next') or '/' user = User() user.set('username', username) user.set('password', password) user.sign_up() flash('User successfully registered') return redirect(next_url) return render_template('register.html')
def test_get_roles(): # type: () -> None role = leancloud.Role('xxx') role.save() user = User() user.login('user1_name', 'password') role.get_users().add(user) role.save() bind_roles = user.get_roles() assert len(bind_roles) == 1 assert bind_roles[0].get('name') == 'xxx' role.destroy()
def test_get_roles(): # type: () -> None role = leancloud.Role("xxx") role.save() user = User() user.login("user1_name", "password") role.get_users().add(user) role.save() bind_roles = user.get_roles() assert len(bind_roles) == 1 assert bind_roles[0].get("name") == "xxx" role.destroy()
def h(*a, **b): ck = web.cookies() tokenName = 'session' token = ck.get(tokenName, None) or web.input().get(tokenName, '!') try: user = User().become(token) web.ctx.userId = user.id web.ctx.userName = user.get("nickName") return g(*a, **b) except leancloud.errors.LeanCloudError as e: if isRedirectOn: web.seeother("/login.html", True) return {'code': e.code, 'message': e.error}
def post(self): username = self.get_argument("username") password = self.get_argument("password") try: user = User() user.login(username, password) except leancloud.errors.LeanCloudError as e: print(e.code, e.error) self.set_secure_cookie("user_id", user.id) self.set_secure_cookie("username", username) #self.redirect('/') self.redirect('/user/')
def login(): form = LoginForm(request.form) if request.method == 'POST' and form.validate_on_submit(): try: u = User() u.login(form.username.data, form.password.data) data = u.attributes data['id'] = u.id data['updated_at'] = u.updated_at data['created_at'] = u.created_at session['user'] = u.attributes return redirect(url_for('.index')) except LeanCloudError, e: flash(e.error, 'danger')
def login(): username = request.form['username'] password = request.form['password'] user = User() try: user.login(username, password) result = { 'token': user.get_session_token(), 'error': 0 } except LeanCloudError as e: result = { 'error': e.error } return jsonify(result), 200, AccessControl
def login(): if request.method == 'GET': return render_template('users/login.html') if request.method == 'POST': user = User() username = request.form.get('username', '').strip() password = request.form.get('password', '').strip() if not username or not password: flash('用户名和密码不能为空。') return redirect(url_for('users.login')) try: user.login(username, password) except LeanCloudError as e: flash(e.error) return redirect(url_for('users.login')) return redirect(url_for('recommends.show'))
def GET(self): it = web.input() try: now = int(it.get('now', time.time())) token = it.get('token') user = User().become(token) except leancloud.errors.LeanCloudError: return {'code': 1} today = int((now + 8 * 3600) / 86400) cnt = TryWords.query.less_than_or_equal_to("recallDay", today).count() if cnt > 0: obj = TryWords.query.less_than_or_equal_to("recallDay", today).limit(1).first() hint = get_hint(obj.get("word")) return {"code": 0, "count": cnt, "testId": obj.id, "word": obj.get("word").lower(), "context": obj.get("context"), "explain": obj.get("expl"), "tryCount": obj.get("tryCount"), "delay": obj.get("delay"), "hint": hint, } else: return {"code": 0, "count": cnt}
def login(): if request.method == 'POST': next_url = request.args.get('next') or '/' username = request.form.get('username') password = request.form.get('password') remember_me = False user = User() print next_url, username, password try: user.login(username, password) login_user(user, remember=remember_me) flash('Logged in successfully') except LeanCloudError: return redirect('/login') return redirect(next_url) return render_template('login.html')
def test_is_linked(): # type: () -> None data = { 'uid': '1', 'access_token': 'xxx' } user = User.login_with('xxx', data) assert user.is_linked('xxx')
def get_current_user(): """Get current user.""" if not 'session_token' in session: return None try: user = User.become(session['session_token']) if not user: signout_user() return user except Exception, e: signout_user() return None # user = User.get_current() # return user # if 'session_token' in session: # user = User.become(session['session_token']) # if not 'token' in session: # return None # token = session['token'] # user = User.become(token) # if not user: # signout_user() # return None # return user # if not user: # # signout_user() # return None # return user
def post(self): username = self.get_argument("username") password = self.get_argument("password") if "admin" == username: try: user = User() user.login(username, password) except leancloud.errors.LeanCloudError as e: print(e.code, e.error) self.redirect("/admin/login/") self.set_secure_cookie("user_id", user.id) self.set_secure_cookie("username", username) self.redirect("/admin/") else: self.redirect("/admin/login/")
def test_unlink_from(): data = { 'uid': '1', 'access_token': 'xxx' } user = User.login_with('xxx', data) user.unlink_from('xxx')
def test_is_linked(): data = { 'uid': '1', 'access_token': 'xxx' } user = User.login_with('xxx', data) assert user.is_linked('xxx')
def test_unlink_from(): # type: () -> None data = { 'uid': '1', 'access_token': 'xxx' } user = User.login_with('xxx', data) user.unlink_from('xxx')
def test_followee_query(): # type: () -> None query = User.create_followee_query("1") assert query._friendship_tag == "followee" assert query.dump() == { "where": { "user": {"__type": "Pointer", "className": "_User", "objectId": "1"}, }, }
def test_file_field(): user = User() user.login('user1_name', 'password') user.set('xxxxx', File('xxx.txt', buffer('qqqqq'))) user.save() q = Query(User) saved_user = q.get(user.id) assert isinstance(saved_user.get('xxxxx'), File) assert saved_user.get('xxxxx').name == 'xxx.txt'
def test_file_field(): # type: () -> None user = User() user.login('user1_name', 'password') user.set('xxxxx', File('xxx.txt', io.BytesIO(b'qqqqq'))) user.save() q = Query(User) saved_user = q.get(user.id) assert isinstance(saved_user.get('xxxxx'), File) assert saved_user.get('xxxxx').name == 'xxx.txt'
def test_file_field(): # type: () -> None user = User() user.login("user1_name", "password") user.set("xxxxx", File("xxx.txt", io.BytesIO(b"qqqqq"))) user.save() q = Query(User) saved_user = q.get(user.id) assert isinstance(saved_user.get("xxxxx"), File) assert saved_user.get("xxxxx").name == "xxx.txt"
def reg(): username = request.form['username'] password = request.form['password'] user = User() user.set("username", username) user.set("password", password) user.sign_up() return user.get_session_token(), 200, AccessControl
def signup(): """Signup""" print request.args.get('next') form = SignupForm() if form.validate_on_submit(): params = form.data.copy() params.pop('repassword') user = User() user.set("username", params['name']) user.set("password", params['password']) user.set("email", params['email']) user.sign_up() signin_user(email=params['email'], password=params['password']) return redirect(request.args.get('next') or url_for('site.index')) return render_template('account/signup/signup.html', form=form)
def get_admin(self, user_name, user_pass): try: User().login(user_name, user_pass) user_query = query_db("_User").equal_to("username", user_name).first() objid = str(user_query.id) return objid except Exception, e: print e return ""
def sign_up(username, password, email): email = email.strip().lower() if not email or not _RE_EMAIL.match(email): raise Err('value:invalid', 'email', 'email cannot be empty.') user = User() user.set("username", username) user.set("password", password) user.set("email", email) try: user.sign_up() except LeanCloudError, e: raise e
def login(): if request.method == 'GET': return render_template('users/login.html') if request.method == 'POST': user = User() username = request.form.get('username', '').strip() password = request.form.get('password', '').strip() if (not username or not password) or (not ((username == 'WX') or (username == 'DJ') or (username == 'ZY') or (username == 'GOD'))): flash('用户名和密码不能为空,或者你不是公司内部成员。') return redirect(url_for('users.login')) try: user.login(username, password) except LeanCloudError as e: flash(e.error) return redirect(url_for('users.login')) return redirect(url_for('todos.show'))
def test_followee_query(): query = User.create_followee_query('1') assert query._friendship_tag == 'followee' assert query.dump() == { 'where': { 'user': { '__type': 'Pointer', 'className': '_User', 'objectId': '1', }, }, }
def test_file_field(): user = User() user.login('user1_name', 'password') user.set('xxxxx', File('xxx.txt', buffer_type(b'qqqqq'))) user.save() q = Query(User) saved_user = q.get(user.id) assert isinstance(saved_user.get('xxxxx'), File) assert saved_user.get('xxxxx').name == 'xxx.txt'
def add(): content = request.form['content'] todo = Todo() todo.set('content', content) todo.set('status', PLANNED) author = User.get_current() if author: todo.set('author', author) # 关联 todo 的作者 try: todo.save() except LeanCloudError as e: flash(e.error) return redirect(url_for('todos.show'))
class Developer: def __init__(self, user_id=None): self.user = User() self.user_id = user_id @classmethod def is_valid_email(self, email): query = Query(User) query.exists("email", email) return 0 if query.find() else 1 def signup(self, email, username, password): self.user.set("email", email) self.user.set("username", username) self.user.set("password", password) try: result = self.user.sign_up() print result return 1 except LeanCloudError, e: print e return 0
def post(self): email = self.get_argument('email') username = self.get_argument('username') password = self.get_argument('password') user = User() user.set("email", email) user.set("username", username) user.set("password", password) user.sign_up() self.set_secure_cookie("username", username) self.set_secure_cookie("user_id", user.id) self.redirect('/')
def register(): form = UserForm(request.form) if request.method == 'POST' and form.validate_on_submit(): user = User() user.set("username", form.username.data) user.set("password", form.password.data) user.set("email", form.email.data) user.sign_up() flash(u'注册成功', 'success') return redirect(url_for('.login')) return render_template('register.html', form=form)