Commit 2975e175 authored by Serge S. Koval's avatar Serge S. Koval

Merge pull request #559 from iurisilvio/inaccessible_callback

Extension point to customize the behaviour of inaccessible views.
parents 5e790899 1eb349d3
......@@ -292,8 +292,8 @@ class BaseView(with_metaclass(AdminViewMeta, BaseViewClass)):
"""
This method will be executed before calling any view method.
By default, it will check if the admin class is accessible and if it is not it will
throw HTTP 404 error.
It will execute the ``inaccessible_callback`` if the view is not
accessible.
:param name:
View function name
......@@ -301,7 +301,17 @@ class BaseView(with_metaclass(AdminViewMeta, BaseViewClass)):
View function arguments
"""
if not self.is_accessible():
return abort(403)
return self.inaccessible_callback(name, **kwargs)
def inaccessible_callback(self, name, **kwargs):
"""
Handle the response to inaccessible views.
By default, it throw HTTP 403 error. Override this method to
customize the behaviour.
"""
return abort(403)
@property
def _debug(self):
......
from nose.tools import ok_, eq_, raises
from flask import Flask, request
from flask import Flask, request, abort
from flask.views import MethodView
from flask.ext.admin import base
......@@ -232,6 +232,20 @@ def test_permissions():
eq_(rv.status_code, 403)
def test_inaccessible_callback():
app = Flask(__name__)
admin = base.Admin(app)
view = MockView()
admin.add_view(view)
client = app.test_client()
view.allow_access = False
view.inaccessible_callback = lambda *args, **kwargs: abort(418)
rv = client.get('/admin/mockview/')
eq_(rv.status_code, 418)
def get_visibility():
app = Flask(__name__)
admin = base.Admin(app)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment