def _(response): user_info = response.body user_info = json.loads(user_info) google_id = user_info['id'] ua = UserAuth.get_or_create(id=google_id) expire_time = auth.get('expires_in')+time.time() expire_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(expire_time)) ua.expire_time =expire_time ua.access_token = auth.get('access_token') ua.token_type = auth.get('token_type') ua.id_token = auth.get('id_token') ua.refresh_token = auth.get('refresh_token') ua.save() u = User.get_or_create(id=google_id) if not u.email: u.emial = user_info.get('email') u.picture = user_info.get('picture') u.gender = user_info.get('gender') u.birthday = user_info.get('birthday') u.name = user_info.get('name') u.save() self.set_secure_cookie('S', google_id) try: user_auth_new_blog(ua) except Exception, e: print e, 'bad things happen for user:%s'%ua.id
def create_user(email, password, name): try: user = User(created=dt.datetime.now()) user.auth = UserAuth(name=name, password=password, email=email) session.add(user) session.commit() except IntegrityError as err: session.rollback() raise err.orig
def main(): for ua in UserAuth.where(): print ua.id, ua.expire_time data = dict( client_id = CLIENT_ID, client_secret = CLIENT_SECRET, refresh_token = ua.refresh_token, grant_type = GRANT_TYPE, ) data = urllib.urlencode(data) t = requests.post(REFRESH_API, data) print t.content
def login(): login_form = form.LoginForm() if request.method == 'POST': try: user = User().authenticate(request.form['field_username'], request.form['field_password']) login_user(UserAuth(user.username, user.id)) return redirect('/') except DoesNotExist: flash('Invalid username or password', 'error') return render_template('login.html', form=login_form)
def load_user(id): usr = User.get(User.id == id) return UserAuth(usr.username, usr.id)