<s> import re <EOL> from django . http import HttpResponsePermanentRedirect <EOL> from . models import ShortenedURL <EOL> class URLShortenerMiddleware ( object ) : <EOL> url_re = re . compile ( r"<STR_LIT>" ) <EOL> def process_response ( self , request , response ) : <EOL> if response . status_code == <NUM_LIT> and self . url_re . match ( request . path ) : <EOL> try : <EOL> shortened_url = ShortenedURL . objects . get ( slug = request . path [ <NUM_LIT:1> : ] ) <EOL> return HttpResponsePermanentRedirect ( shortened_url . path ) <EOL> except : <EOL> pass <EOL> return response </s>
