<commit_before>import logging

from django.contrib.auth import authenticate
from django.contrib.auth import login
from django import http
from django.views import generic
from facebook_auth import urls


logger = logging.getLogger(__name__)


class Handler(generic.View):
    def get(self, request):
        try:
            next_url = urls.Next().decode(request.GET['next'])
        except urls.InvalidNextUrl:
            logger.warning('Invalid facebook handler next.',
                           extra={'request': request})
            return http.HttpResponseBadRequest()
        if 'code' in request.GET:
            self.login(next_url)
            response = http.HttpResponseRedirect(next_url['next'])
            response["P3P"] = ('CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi'
                               ' IVDi CONi HIS OUR IND CNT"')
        else:
            response = http.HttpResponseRedirect(next_url['close'])
        return response

    def login(self, next_url):
        user = authenticate(
            code=self.request.GET['code'],
            redirect_uri=urls.redirect_uri(next_url['next'],
                                           next_url['close']))
        if user:
            login(self.request, user)

handler = Handler.as_view()
<commit_msg>Add facebook error handler in view.<commit_after>import logging

from django.contrib.auth import authenticate
from django.contrib.auth import login
from django import http
from django.views import generic

import facepy

from facebook_auth import urls


logger = logging.getLogger(__name__)


class Handler(generic.View):
    def get(self, request):
        try:
            self.next_url = urls.Next().decode(request.GET['next'])
        except urls.InvalidNextUrl:
            logger.warning('Invalid facebook handler next.',
                           extra={'request': request})
            return http.HttpResponseBadRequest()
        if 'code' in request.GET:
            try:
                self.login()
            except facepy.FacepyError as e:
                return self.handle_facebook_error(e)
            response = http.HttpResponseRedirect(self.next_url['next'])
            response["P3P"] = ('CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi'
                               ' IVDi CONi HIS OUR IND CNT"')
        else:
            response = http.HttpResponseRedirect(self.next_url['close'])
        return response

    def login(self):
        user = authenticate(
            code=self.request.GET['code'],
            redirect_uri=urls.redirect_uri(self.next_url['next'],
                                           self.next_url['close']))
        if user:
            login(self.request, user)

    def handle_facebook_error(self, e):
        return http.HttpResponseRedirect(self.next_url['next'])

handler = Handler.as_view()

{'fraction_non_alphanumeric': 0.097310686482661, 'fraction_numerical': 0.0007077140835102619, 'mean_word_length': 2.1341463414634148, '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': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '732885', 'n_tokens_mistral': 765, 'n_tokens_neox': 735, 'n_words': 186}