Browse Source

Adds provide_automatic_options to Class-based Views

pull/2316/head
kaveh 8 years ago
parent
commit
75f537fb87
  1. 4
      flask/views.py
  2. 37
      tests/test_views.py

4
flask/views.py

@ -51,6 +51,9 @@ class View(object):
#: A list of methods this view can handle. #: A list of methods this view can handle.
methods = None methods = None
#: Setting this disables or force-enables the automatic options handling.
provide_automatic_options = None
#: The canonical way to decorate class-based views is to decorate the #: The canonical way to decorate class-based views is to decorate the
#: return value of as_view(). However since this moves parts of the #: return value of as_view(). However since this moves parts of the
#: logic from the class declaration to the place where it's hooked #: logic from the class declaration to the place where it's hooked
@ -99,6 +102,7 @@ class View(object):
view.__doc__ = cls.__doc__ view.__doc__ = cls.__doc__
view.__module__ = cls.__module__ view.__module__ = cls.__module__
view.methods = cls.methods view.methods = cls.methods
view.provide_automatic_options = cls.provide_automatic_options
return view return view

37
tests/test_views.py

@ -109,6 +109,43 @@ def test_view_decorators():
assert rv.headers['X-Parachute'] == 'awesome' assert rv.headers['X-Parachute'] == 'awesome'
assert rv.data == b'Awesome' assert rv.data == b'Awesome'
def test_view_provide_automatic_options_attr():
app = flask.Flask(__name__)
class Index1(flask.views.View):
provide_automatic_options = False
def dispatch_request(self):
return 'Hello World!'
app.add_url_rule('/', view_func=Index1.as_view('index'))
c = app.test_client()
rv = c.open('/', method='OPTIONS')
assert rv.status_code == 405
app = flask.Flask(__name__)
class Index2(flask.views.View):
methods = ['OPTIONS']
provide_automatic_options = True
def dispatch_request(self):
return 'Hello World!'
app.add_url_rule('/', view_func=Index2.as_view('index'))
c = app.test_client()
rv = c.open('/', method='OPTIONS')
assert sorted(rv.allow) == ['OPTIONS']
app = flask.Flask(__name__)
class Index3(flask.views.View):
def dispatch_request(self):
return 'Hello World!'
app.add_url_rule('/', view_func=Index3.as_view('index'))
c = app.test_client()
rv = c.open('/', method='OPTIONS')
assert 'OPTIONS' in rv.allow
def test_implicit_head(): def test_implicit_head():
app = flask.Flask(__name__) app = flask.Flask(__name__)

Loading…
Cancel
Save