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()

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()

Add facebook error handler in view.
Add facebook error handler in view.

This assumes that there is no other backend which
can authenticate user with facebook credentials.

  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:
-             next_url = urls.Next().decode(request.GET['next'])
+             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(next_url)
+                 self.login()
+             except facepy.FacepyError as e:
+                 return self.handle_facebook_error(e)
-             response = http.HttpResponseRedirect(next_url['next'])
+             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(next_url['close'])
+             response = http.HttpResponseRedirect(self.next_url['close'])
          return response
  
-     def login(self, next_url):
+     def login(self):
          user = authenticate(
              code=self.request.GET['code'],
-             redirect_uri=urls.redirect_uri(next_url['next'],
+             redirect_uri=urls.redirect_uri(self.next_url['next'],
-                                            next_url['close']))
+                                            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()
  
Add facebook error handler in view.
## Code 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()

## Instruction:
Add facebook error handler in view.
## Code 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()
