Example #1
0
 def save_token(self, session, user):
     uid = user.username
     try:
         token = Token.objects.get(uid=uid, user=user)
     except Exception:
         token = Token(uid=uid, user=user)
     token.access_token = session.access_token
     token.expires = session.expires
     token.save()
     
     return token
Example #2
0
def save_token(token, request, *args, **kwargs):
    # app.logger.debug('save_token')
    #toks = db.session.query(Token).filter_by(client_id=request.client.client_id,
    #                             user_id=request.user.id).all()
    #app.logger.debug('client_id={client_id}, user_id={user_id}'.format(client_id=request.client.client_id, user_id=request.user.id))
    #app.logger.debug(toks)
    ## make sure that every client has only one token connected to a user
    #db.session.delete(toks)

    expires_in = token.pop('expires_in')
    expires = datetime.utcnow() + timedelta(seconds=expires_in)

    # app.logger.debug(token)

    #from pprint import pprint
    # import pprint
    # from inspect import getmembers
    #
    # pp = pprint.PrettyPrinter(indent=4)
    #
    # app.logger.debug('=' * 80)
    # app.logger.debug(pp.pformat(getmembers(request)))
    # app.logger.debug('=' * 80)
    # app.logger.debug(pp.pformat(getmembers(current_user)))
    # app.logger.debug('=' * 80)
    #app.logger.debug(current_user.dir())
    tok = Token(**token)
    tok.expires = expires
    tok.client_id = request.client.client_id

    if not request.user:
        tok.user_id = current_user.id
    else:
        tok.user_id = request.user.id

        #if hasattr(request, 'user'):
        #tok.user_id = request.user.id
        #elif current_user.id:
        #tok.user_id = current_user.id
    #tok.user_id = current_user.id
    db.session.add(tok)
    db.session.commit()
    return tok