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)): ...@@ -292,8 +292,8 @@ class BaseView(with_metaclass(AdminViewMeta, BaseViewClass)):
""" """
This method will be executed before calling any view method. 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 It will execute the ``inaccessible_callback`` if the view is not
throw HTTP 404 error. accessible.
:param name: :param name:
View function name View function name
...@@ -301,7 +301,17 @@ class BaseView(with_metaclass(AdminViewMeta, BaseViewClass)): ...@@ -301,7 +301,17 @@ class BaseView(with_metaclass(AdminViewMeta, BaseViewClass)):
View function arguments View function arguments
""" """
if not self.is_accessible(): 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 @property
def _debug(self): def _debug(self):
......
from nose.tools import ok_, eq_, raises 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.views import MethodView
from flask.ext.admin import base from flask.ext.admin import base
...@@ -232,6 +232,20 @@ def test_permissions(): ...@@ -232,6 +232,20 @@ def test_permissions():
eq_(rv.status_code, 403) 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(): def get_visibility():
app = Flask(__name__) app = Flask(__name__)
admin = base.Admin(app) 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