from django import http

from wagtail.wagtailredirects import models


# Originally pinched from: https://github.com/django/django/blob/master/django/contrib/redirects/middleware.py
class RedirectMiddleware(object):
    def process_response(self, request, response):
        # No need to check for a redirect for non-404 responses.
        if response.status_code != 404:
            return response

        # Get the path
        path = models.Redirect.normalise_path(request.get_full_path())

        # Find redirect
        try:
            redirect = models.Redirect.get_for_site(request.site).get(old_path=path)

            if redirect.is_permanent:
                return http.HttpResponsePermanentRedirect(redirect.link)
            else:
                return http.HttpResponseRedirect(redirect.link)
        except:
            pass

        return response

from django import http

from wagtail.wagtailredirects import models


# Originally pinched from: https://github.com/django/django/blob/master/django/contrib/redirects/middleware.py
class RedirectMiddleware(object):
    def process_response(self, request, response):
        # No need to check for a redirect for non-404 responses.
        if response.status_code != 404:
            return response

        # Get the path
        path = models.Redirect.normalise_path(request.get_full_path())

        # Find redirect
        try:
            redirect = models.Redirect.get_for_site(request.site).get(old_path=path)
        except models.Redirect.DoesNotExist:
            # No redirect found, return the 400 page
            return response

        if redirect.is_permanent:
            return http.HttpResponsePermanentRedirect(redirect.link)
        else:
            return http.HttpResponseRedirect(redirect.link)

Refactor out a bare except: statement
Refactor out a bare except: statement

It now catches `Redirect.DoesNotExist`, returning the normal 404 page if
no redirect is found. Any other exception should not be caught here.

  from django import http
  
  from wagtail.wagtailredirects import models
  
  
  # Originally pinched from: https://github.com/django/django/blob/master/django/contrib/redirects/middleware.py
  class RedirectMiddleware(object):
      def process_response(self, request, response):
          # No need to check for a redirect for non-404 responses.
          if response.status_code != 404:
              return response
  
          # Get the path
          path = models.Redirect.normalise_path(request.get_full_path())
  
          # Find redirect
          try:
              redirect = models.Redirect.get_for_site(request.site).get(old_path=path)
+         except models.Redirect.DoesNotExist:
+             # No redirect found, return the 400 page
+             return response
  
-             if redirect.is_permanent:
+         if redirect.is_permanent:
-                 return http.HttpResponsePermanentRedirect(redirect.link)
+             return http.HttpResponsePermanentRedirect(redirect.link)
-             else:
+         else:
-                 return http.HttpResponseRedirect(redirect.link)
+             return http.HttpResponseRedirect(redirect.link)
-         except:
-             pass
  
-         return response
- 
Refactor out a bare except: statement
## Code Before:
from django import http

from wagtail.wagtailredirects import models


# Originally pinched from: https://github.com/django/django/blob/master/django/contrib/redirects/middleware.py
class RedirectMiddleware(object):
    def process_response(self, request, response):
        # No need to check for a redirect for non-404 responses.
        if response.status_code != 404:
            return response

        # Get the path
        path = models.Redirect.normalise_path(request.get_full_path())

        # Find redirect
        try:
            redirect = models.Redirect.get_for_site(request.site).get(old_path=path)

            if redirect.is_permanent:
                return http.HttpResponsePermanentRedirect(redirect.link)
            else:
                return http.HttpResponseRedirect(redirect.link)
        except:
            pass

        return response

## Instruction:
Refactor out a bare except: statement
## Code After:
from django import http

from wagtail.wagtailredirects import models


# Originally pinched from: https://github.com/django/django/blob/master/django/contrib/redirects/middleware.py
class RedirectMiddleware(object):
    def process_response(self, request, response):
        # No need to check for a redirect for non-404 responses.
        if response.status_code != 404:
            return response

        # Get the path
        path = models.Redirect.normalise_path(request.get_full_path())

        # Find redirect
        try:
            redirect = models.Redirect.get_for_site(request.site).get(old_path=path)
        except models.Redirect.DoesNotExist:
            # No redirect found, return the 400 page
            return response

        if redirect.is_permanent:
            return http.HttpResponsePermanentRedirect(redirect.link)
        else:
            return http.HttpResponseRedirect(redirect.link)
