本文整理汇总了Python中models.user.UserModel.populate_from_profile方法的典型用法代码示例。如果您正苦于以下问题:Python UserModel.populate_from_profile方法的具体用法?Python UserModel.populate_from_profile怎么用?Python UserModel.populate_from_profile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类models.user.UserModel
的用法示例。
在下文中一共展示了UserModel.populate_from_profile方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: connect
# 需要导入模块: from models.user import UserModel [as 别名]
# 或者: from models.user.UserModel import populate_from_profile [as 别名]
def connect():
code = request.data
app.logger.debug("connect - code: {0}".format(code))
try:
# Upgrade the authorization code into a credentials object
oauth_flow = flow_from_clientsecrets('client_secrets.json', scope='')
oauth_flow.redirect_uri = 'postmessage'
credentials = oauth_flow.step2_exchange(code)
app.logger.debug("credentials: {0}".format(credentials.to_json()))
# Store the access token in the session for later use.
session['credentials'] = pickle.dumps(credentials)
gplus_id = credentials.id_token['sub']
http = credentials.authorize(httplib2.Http())
try: # load the existing user
user = UserModel.get(gplus_id)
app.logger.debug("user found: {0}".format(gplus_id))
except UserModel.DoesNotExist: # create a new record for this user
google_request = SERVICE.people().get(userId='me')
profile = google_request.execute(http=http)
#app.logger.debug('profile: %s' % profile)
user = UserModel(gplus_id)
user.populate_from_profile(profile)
app.logger.debug("user added: {0}".format(gplus_id))
user.last_login_datetime = datetime.datetime.now()
user.save()
# fetch visible connections, store new connections to the db
# NOTE: we do not purge connections that are no longer valid...yet
google_request = SERVICE.people().list(userId='me', collection='visible')
for profile in google_request.execute(http=http).get('items'):
connection = user.update_connection(profile)
#app.logger.debug("Created connection: {0}".format(connection.to_json()))
except FlowExchangeError:
app.logger.warn('connect - FlowExchangeError')
session.pop('credentials', None)
response = make_response(json.dumps('Failed to upgrade the authorization code.'), 401)
response.headers['Content-Type'] = 'application/json'
return response
response = make_response(json.dumps('Successfully connected user.'), 200)
response.headers['Content-Type'] = 'application/json'
return response