示例#1
0
def get_persistent_graph(request, *args, **kwargs):
    '''
    Wraps itself around get facebook graph
    But stores the graph in the session, allowing usage across multiple
    pageviews.
    Note that Facebook session's expire at some point, you can't store this
    for permanent usage
    Atleast not without asking for the offline_access permission
    '''
    from open_facebook.api import OpenFacebook
    if not request:
        logger.info("GPG01 no request")
        raise (ValidationError,
               'Request is required if you want to use persistent tokens')

    graph = None
    # some situations like an expired access token require us to refresh our
    # graph
    require_refresh = False
    code = request.REQUEST.get('code')
    logger.info("GPG02 code = %s" % code)
    if code:
        require_refresh = True

    local_graph = getattr(request, 'facebook', None)
    logger.info("GPG03 local_graph %s" % local_graph)
    if local_graph:
        # gets the graph from the local memory if available
        graph = local_graph

    if not graph:
        logger.info("GPG04 no graph")
        # search for the graph in the session
        cached_graph_dict = request.session.get('graph_dict')
        if cached_graph_dict:
            logger.info("GPG05 graph in session")
            graph = OpenFacebook()
            graph.__setstate__(cached_graph_dict)
            graph._me = None

    if not graph or require_refresh:
        # gets the new graph, note this might do token conversions (slow)
        logger.info("GPG06 getting graph")
        graph = get_facebook_graph(request, *args, **kwargs)
        # if it's valid replace the old cache
        if graph is not None and graph.access_token:
            logger.info("GPG07 put graph into session")
            request.session['graph_dict'] = graph.__getstate__()

    # add the current user id and cache the graph at the request level
    _add_current_user_id(graph, request.user)
    request.facebook = graph

    return graph
示例#2
0
def get_persistent_graph(request, *args, **kwargs):
    '''
    Wraps itself around get facebook graph
    But stores the graph in the session, allowing usage across multiple
    pageviews.
    Note that Facebook session's expire at some point, you can't store this
    for permanent usage
    Atleast not without asking for the offline_access permission
    '''
    from open_facebook.api import OpenFacebook
    if not request:
        logger.info("GPG01 no request")
        raise(ValidationError,
              'Request is required if you want to use persistent tokens')

    graph = None
    # some situations like an expired access token require us to refresh our
    # graph
    require_refresh = False
    code = request.REQUEST.get('code')
    logger.info("GPG02 code = %s" % code)
    if code:
        require_refresh = True

    local_graph = getattr(request, 'facebook', None)
    logger.info("GPG03 local_graph %s" % local_graph)
    if local_graph:
        # gets the graph from the local memory if available
        graph = local_graph

    if not graph:
        logger.info("GPG04 no graph")
        # search for the graph in the session
        cached_graph_dict = request.session.get('graph_dict')
        if cached_graph_dict:
            logger.info("GPG05 graph in session")
            graph = OpenFacebook()
            graph.__setstate__(cached_graph_dict)
            graph._me = None

    if not graph or require_refresh:
        # gets the new graph, note this might do token conversions (slow)
        logger.info("GPG06 getting graph")
        graph = get_facebook_graph(request, *args, **kwargs)
        # if it's valid replace the old cache
        if graph is not None and graph.access_token:
            logger.info("GPG07 put graph into session")
            request.session['graph_dict'] = graph.__getstate__()

    # add the current user id and cache the graph at the request level
    _add_current_user_id(graph, request.user)
    request.facebook = graph

    return graph