<s> from __future__ import absolute_import , unicode_literals <EOL> from django import http <EOL> from django . utils . six . moves . urllib . parse import urlparse <EOL> from wagtail . wagtailredirects import models <EOL> def get_redirect ( request , path ) : <EOL> try : <EOL> return models . Redirect . get_for_site ( request . site ) . get ( old_path = path ) <EOL> except models . Redirect . MultipleObjectsReturned : <EOL> return models . Redirect . objects . get ( site = request . site , old_path = path ) <EOL> except models . Redirect . DoesNotExist : <EOL> return None <EOL> class RedirectMiddleware ( object ) : <EOL> def process_response ( self , request , response ) : <EOL> if response . status_code != <NUM_LIT> : <EOL> return response <EOL> if not hasattr ( request , '<STR_LIT>' ) : <EOL> return response <EOL> path = models . Redirect . normalise_path ( request . get_full_path ( ) ) <EOL> redirect = get_redirect ( request , path ) <EOL> if redirect is None : <EOL> path_without_query = urlparse ( path ) . path <EOL> if path == path_without_query : <EOL> return response <EOL> redirect = get_redirect ( request , path_without_query ) <EOL> if redirect is None : <EOL> return response <EOL> if redirect . is_permanent : <EOL> return http . HttpResponsePermanentRedirect ( redirect . link ) <EOL> else : <EOL> return http . HttpResponseRedirect ( redirect . link ) </s>
