def login_with_token(): data = request.json token = data.get('token', '') user = Models.User.verify_auth_token(token) if not user: return jsonify(message='Failed') login_user(user, remember=True) return jsonify(message='OK', user=Json.get_user_info(user), remember_token=user.generate_auth_token())
def login_mobile_number(): data = request.json mobile_number = data.get('mobile_number', '') user, authenticated = Models.User.authenticate( mobile_number=mobile_number, password=data.get('password', '')) print(user.roles) if not authenticated: return jsonify(message='Failed, wrong password or username') if "ADMIN" in user.roles: login_user(user, remember=True) return jsonify(message='OK', user=Json.get_user_info(user), remember_token=user.generate_auth_token()) if not user.verified: return jsonify(message='Failed, you need to active your account through sms') login_user(user, remember=True) return jsonify(message='OK', user=Json.get_user_info(user), remember_token=user.generate_auth_token())
def update_username(): username = request.json.get('username') if username: if len(username) >16: return jsonify(message="Failed", error=_('Username is too long')) user = current_user._get_current_object() user.name = username user.save() return jsonify(message='OK', user=Json.get_user_info(user)) return jsonify(message='Failed', error=u"参数不对")
def update_avatar(): path = request.json.get('avatar_url') if path: url = "http://assets.maybi.cn/%s" % path Jobs.image.make_thumbnails('maybi-img', path, url) user = current_user._get_current_object() user.avatar_url = url user.save() return jsonify(message='OK', user=Json.get_user_info(user))
def login_email(): data = request.json email = data.get('email', '') user, authenticated = Models.User.authenticate( email=email, password=data.get('password', '')) if not authenticated: return jsonify(message='Failed') login_user(user, remember=True) return jsonify(message='OK', user=Json.get_user_info(user), remember_token=user.generate_auth_token())
def bind_email(): email = request.json.get('email') user_id = request.json.get('user_id') if not email: return jsonify(message='Failed', error=_('no email')) if Models.User.objects(account__email=email): return jsonify(message='Failed', error=_('The email already exists')) u = Models.User.objects(id=user_id).first() u.account.email = email u.account.is_email_verified = True u.save() login_user(u, remember=True) return jsonify(message='OK', user=Json.get_user_info(u), remember_token=u.generate_auth_token())
def callback(sitename): from application.models import SocialOAuth if sitename in ['weibo_app', 'qq_app', 'facebook_app']: s, msg = parse_token_response(sitename, request.args) app = 'IOS' else: code = request.args.get('code') s, msg = get_oauth_token(sitename, code) app = sitename !='wechat_app' and 'MOBILEWEB' or 'IOS' if s is None: print (msg) return jsonify(message='Failed', error=msg) if sitename in ['wechat', 'wechat_app']: oauth = SocialOAuth.objects(unionid=s.unionid).first() else: oauth = SocialOAuth.objects(site_uid=s.uid).first() if not oauth: oauth = SocialOAuth.create(s.site_name, s.uid, s.name, s.access_token, s.expires_in, s.refresh_token, app=app, unionid=getattr(s, 'unionid', None), gender=s.gender) path = 'avatar/{}/{}.jpeg'.format(oauth.user.id, str(time.time()).replace('.','')) Jobs.image.save_avatar('maybi-img', path, url=s.avatar_large, save_original=True) url = "http://assets.maybi.cn/%s"%path oauth.update_avatar(url) user_id = str(oauth.user.id) login_user(oauth.user, remember=True) return jsonify(message='OK', login=False, user_id=user_id) else: oauth.re_auth(s.access_token, s.expires_in, s.refresh_token, getattr(s, 'unionid', None)) if oauth.user.account.is_email_verified: login_user(oauth.user, remember=True) return jsonify(message='OK', login=True, remember_token=oauth.user.generate_auth_token(), user=Json.get_user_info(oauth.user)) else: user_id = str(oauth.user.id) return jsonify(message='OK', login=False, user_id=user_id)
def email_signup(): data = request.json email = data.get('email') password = data.get('password') name = data.get('name') if not password: # 不能为空 return jsonify(message='Failed', error=_(u'Please fill in.')) if Models.User.objects(account__email=email): return jsonify(message='Failed', error=_(u'This email has been registered.')) if not name: name = 'Maybi' + str(time.time()).replace('.','') user = Models.User.create(email=email, password=password, name=name) login_user(user, remember=True) return jsonify(message='OK', user=Json.get_user_info(user), remember_token=user.generate_auth_token())
def user_info(): if not current_user.is_authenticated: return jsonify(message='Failed', logged_in=False) info = Json.get_user_info(current_user) return jsonify(message='OK', logged_in=True, user=info)