<commit_before>from django.shortcuts import render, redirect, render_to_response
from django.core.urlresolvers import reverse
from facebook import get_user_from_cookie, GraphAPI
from settings.base import FB_APP_ID, FB_SCOPE, FB_APP_SECRET
from .models import SocialUser as User


class SocialBinderMiddleware():
    
    def process_request(self, request):
        if request.session.get('user'):
            request.user = request.session.get('user')
    
    def process_response(self, request, response):
        
        result = get_user_from_cookie(
                cookies=request.COOKIES,
                app_id=FB_APP_ID,
                app_secret=FB_APP_SECRET)

        # If there is no result, we assume the user is not logged in.
        if result:
            # Check to see if this user is already in our database.
            user = User.objects.filter(id = result.get('uid')).first()
    
            if not user:
                # Not an existing user so get info
                graph = GraphAPI(result['access_token'])
                profile = graph.get_object('me')
                # Create the user and insert it into the database
                user = User(id=str(profile.get('id')), name=profile.get('name'),
                            profile_url=profile.get('link'),
                            access_token=result.get('access_token'))
            elif not user.has_valid_access_token(result.get('access_token')):
                # If an existing user, update the access token
                user.access_token = result.get('access_token')
    
            # Add the user to the current session
            response.session['user'] = dict(name=user.name,
                                           profile_url=user.profile_url,
                                           id=user.id,
                                           access_token=user.access_token)

            user.save()
        return response<commit_msg>(fix) removed unused imports, make middleware lean<commit_after>from django.conf import settings
from facebook import get_user_from_cookie, GraphAPI
from .models import SocialUser as User


class SocialBinderMiddleware():

    def process_request(self, request):
        user = request.session.get('user', None)
        # Bind user to request if user is in session
        if user:
            request.user = User.objects.get(id=user['id'])
        return

    def process_response(self, request, response):
        result = None
        # Hoo! Active cookies, verify that user cookie isn't stale
        if request.COOKIES:
            try:
                result = get_user_from_cookie(
                    cookies=request.COOKIES,
                    app_id=settings.FB_APP_ID,
                    app_secret=settings.FB_APP_SECRET
                )
            except:
                pass

        # If there is no result, we assume the user is not logged in.
        if result:
            # Check to see if this user is already in our database.
            graph = GraphAPI(result['access_token'])
            profile = dict()
            user = User.objects.filter(id=result.get('uid')).first()
            if not user:
                # User doesn't exist in datastore so get info
                profile = graph.get_object('/me')
                # Create the user and insert it into the database
                user = User(
                    id=str(profile.get('id')),
                    name=profile.get('name'),
                    profile_url=profile.get('link'),
                    access_token=result.get('access_token')
                )
            elif not user.has_valid_access_token(result.get('access_token')):
                # If an existing user, update the access token
                user.access_token = result.get('access_token')

            graph_query_result = graph.get_object('/me/picture')
            profile['link'] = graph_query_result['url']
            if user.profile_url is not profile['link']:
                user.profile_url = profile['link']

            user.active = True

            # Add the user to the current session
            request.session['user'] = dict(
                name=user.name,
                profile_url=user.profile_url,
                id=user.id,
                access_token=user.access_token,
                is_authenticated=user.is_authenticated()
            )

            user.save()
        return response
{'fraction_non_alphanumeric': 0.08333333333333333, 'fraction_numerical': 0.0, 'mean_word_length': 1.7212009803921569, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4487789', 'n_tokens_mistral': 1146, 'n_tokens_neox': 1095, 'n_words': 300}