Flask-NoExtRef

Flask-NoExtRef is an extension for Flask that adds ability for hiding external URL to your Flask application.

Installation

Install the extension with one of the following commands:

$ easy_install Flask-NoExtRef

or alternatively if you have pip installed:

$ pip install Flask-NoExtRef

How to Use

Basically all you have to do is to create an NoExtRef object. After that you can use objects’s methods in your python code or can use new filters in Jinja templates with the same names for hiding external URL. Here is a complete example:

>>> from flask import Flask
>>> from flaskext.noextref import NoExtRef
>>> app = Flask(__name__)
>>> noext = NoExtRef(app)
>>> # these line of code is necessary in console only
>>> app.test_request_context().push()
>>> print noext.hide_url("http://google.com")
/ext-url/http://google.com
>>> print noext.hide_urls('test <a href="http://google.com"> anchor </a> ')
test <a href="/ext-url/http://google.com"> anchor </a>

And this is an example of using new filters in Jinja templates:

{# replace all anchors in the text #}
{{ some_text_with_refs|hide_urls }}

{# replace of the specified anchor #}
{{ some_url|hide_url }}

In the default configuration all external path will be hidden as:

/ext-url/<path:url>

How to change

If the default settings are not suitable for you then you can easily change them. For example if you want to set different format for hide external URL:

/go?url=external-url

You must do the following:

from flask import Flask, abort, request, redirect
from flaskext.noextref import NoExtRef

app = Flask(__name__)

def handle_ext_url():
    url = request.args.get('url', None)
    if not url:
        abort(405)
    return redirect(url)

noext = NoExtRef(app, rule='/go/',
            view_func=handle_ext_url)

If you do not want to hide urls from some domain do the follow:

from flask import Flask, abort, request, redirect
from flaskext.noextref import NoExtRef

app = Flask(__name__)
noext = NoExtRef(app, safe_domains=['some-domain.com'])

Api

This part of the documentation documents all the public classes and functions in Flask-NoExtRef.

class flaskext.noextref.NoExtRef(app, rule='/ext-url/<path:url>', endpoint='ext_url', view_func=None, safe_domains=[], add_jinja_filters=True)

NoExtRef object adds ability to the Flask application to hide external URL. Changes the state of the Flask application: adds Jinja filters and creates back reference (app._noextref).

Parameters:
  • app – the Flask application
  • rule – the URL rule as string
  • endpoint – the endpoint for the registered URL rule
  • view_func – the function to call when serving a request to the provided endpoint
  • safe_domains – the list of domains that are not external. URL containing one of these domains will not be hidden.
  • add_jinja_filters – if True then adds new filters for Jinja
go_to_url(url)

Redirects to the external url

hide_url(url)

Converts external url to the local URL. Also available in Jinja templates as filter.

hide_urls(text)

Finds all references (href) in the text and replaces them with local. Also available in Jinja templates as filter.

Robots.txt

Default robots.txt:

User-agent: *
Disallow: /ext-url/

Changelog

Version 0.1

  • Initial release
Fork me on GitHub