middleware: external links open in new window

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
class ExtlinksBlankMiddleware(object):
    '''
    Makes sure all external and only exernal links open in a new window.
    '''
    def __init__(self):
        self.targets = re.compile(r'''target=.\w*.''')
        self.extlinks = re.compile(r'''<a (?P<old>[^>]*http.?://)''')
    def process_response(self, request, response):
        if ("text" in response['Content-Type']):
            response.content = self.targets.sub('', response.content)
            response.content = self.extlinks.sub('<a target="_blank" \g<old>',response.content)
            return response
        else:
            return response

More like this

  1. URLField admin widget with View Link button by ungenio41 3 years ago
  2. Remove self links middleware by svetlyak 5 years, 1 month ago
  3. admin: edit related object shortcut by christian 5 years, 7 months ago
  4. Hyperlink list filter by lifefloatsby 5 years, 4 months ago
  5. True links in the admin list by svetlyak 5 years, 8 months ago

Comments

marcob (on February 11, 2010):

Well done!

#

(Forgotten your password?)