<commit_before># -*- coding: utf-8 -*-
from functools import wraps
import urllib
from uuid import uuid1

from django import http
from django.conf import settings

from django.contrib.auth import authenticate, login
from django.http import HttpResponseRedirect


def get_auth_address(request, redirect_to, scope=''):
    state = unicode(uuid1())
    auth_requests = request.session.get('auth_requests', {})
    auth_requests[state] = {
        'method': request.method,
        'POST': request.POST,
    }
    request.session['auth_requests'] = auth_requests
    args = {
            'client_id': settings.FACEBOOK_APP_ID,
            'redirect_uri': redirect_to,
            'scope': scope,
            'state': state,
    }
    return 'https://www.facebook.com/dialog/oauth?' + urllib.urlencode(args)


def accept_login():
    def decorator(fun):
        @wraps(fun)
        def res(request, *args, **kwargs):
            state = request.GET.get('state', None)
            code = request.GET.get('code', None)
            if state and code:
                old_request = request.session.get('auth_requests', {}).get(state, None)
                if old_request:
                    request.method = old_request['method']
                    request.POST = old_request['POST']
                    del request.session['auth_requests'][state]
                    request.session.modified = True
                    user = authenticate(code=code, redirect_uri=request.build_absolute_uri(request.path))
                    if user:
                        login(request, user)
                if request.method != 'POST':
                    return HttpResponseRedirect(request.build_absolute_uri(request.path))
            return fun(request, *args, **kwargs)
        return res
    return decorator


def login_required():
    def decorator(fun):
        @wraps(fun)
        def res(request, *args, **kwargs):
            url = get_auth_address(request, request.build_absolute_uri(request.path))
            if request.user.is_authenticated():
                return fun(request, *args, **kwargs)
            else:
                return http.HttpResponse(("<html><head><title></title></head>"
                        "<body><script>window.top.location=\"%(url)s\";</script></body>"
                        "</html>") % dict(url=url))
        return res
    return decorator
<commit_msg>Refactor login_required.<commit_after># -*- coding: utf-8 -*-
from functools import wraps
import urllib
from uuid import uuid1

from django import http
from django.conf import settings

from django.contrib.auth import authenticate, login
from django.http import HttpResponseRedirect


def get_auth_address(request, redirect_to, scope=''):
    state = unicode(uuid1())
    auth_requests = request.session.get('auth_requests', {})
    auth_requests[state] = {
        'method': request.method,
        'POST': request.POST,
    }
    request.session['auth_requests'] = auth_requests
    args = {
            'client_id': settings.FACEBOOK_APP_ID,
            'redirect_uri': redirect_to,
            'scope': scope,
            'state': state,
    }
    return 'https://www.facebook.com/dialog/oauth?' + urllib.urlencode(args)


def accept_login():
    def decorator(fun):
        @wraps(fun)
        def res(request, *args, **kwargs):
            state = request.GET.get('state', None)
            code = request.GET.get('code', None)
            if state and code:
                old_request = request.session.get('auth_requests', {}).get(state, None)
                if old_request:
                    request.method = old_request['method']
                    request.POST = old_request['POST']
                    del request.session['auth_requests'][state]
                    request.session.modified = True
                    user = authenticate(code=code, redirect_uri=request.build_absolute_uri(request.path))
                    if user:
                        login(request, user)
                if request.method != 'POST':
                    return HttpResponseRedirect(request.build_absolute_uri(request.path))
            return fun(request, *args, **kwargs)
        return res
    return decorator


def login_required():
    def decorator(fun):
        @wraps(fun)
        def res(request, *args, **kwargs):
            if request.user.is_authenticated():
                return fun(request, *args, **kwargs)
            else:
                url = get_auth_address(request, request.build_absolute_uri(request.path))
                return http.HttpResponse(("<html><head><title></title></head>"
                        "<body><script>window.top.location=\"%(url)s\";</script></body>"
                        "</html>") % dict(url=url))
        return res
    return decorator

{'fraction_non_alphanumeric': 0.12063558436127954, 'fraction_numerical': 0.0012544428183148652, 'mean_word_length': 2.1411687458962576, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2615677', 'n_tokens_mistral': 1310, 'n_tokens_neox': 1280, 'n_words': 277}