Add @throttle_scope function based view decorator#9963
Add @throttle_scope function based view decorator#9963d-ryzhykau wants to merge 3 commits intoencode:mainfrom
@throttle_scope function based view decorator#9963Conversation
f80dcb4 to
0513735
Compare
|
|
||
| Each of these decorators is equivalent to setting their respective [api policy attributes][api-policy-attributes]. | ||
|
|
||
| All decorators take a single argument. The ones that end with `_class` expect a single class while the ones ending in `_classes` expect a list or tuple of classes. |
There was a problem hiding this comment.
This line doesn't work for this new decorator... Can we rephrase or add another sentence?
There was a problem hiding this comment.
I moved the new decorator description to a separate section as it doesn't seem to fit into the "API policy decorators" group.
| @throttle_classes([OncePerDayScopedThrottle]) | ||
| @throttle_scope(scope) |
There was a problem hiding this comment.
Is @throttle_classes required every time @throttle_scope is used? I don't think so if the scopes are defined in the project settings... Can we add a test to cover that please?
Also make sure we're aligned with CBV in terms of misconfigurations (e.g specifying an unknown scope)
There was a problem hiding this comment.
@throttle_classes is only required for tests, as the alternative way to enable ScopedRateThrottle - using REST_FRAMEWORK settings - is hard to setup: APIView.throttle_classes and SimpleRateThrottle.THROTTLE_RATES are set during import, so to change these attributes for the test it'd be necessary to reload the throttling, views and decorators modules. IMO, this would make the test more complicated and more brittle under refactoring.
Here's how the test would look like:
def test_throttle_scope(self):
scope = "x"
with override_settings(
REST_FRAMEWORK={
'DEFAULT_THROTTLE_CLASSES': ['rest_framework.throttling.ScopedRateThrottle'],
'DEFAULT_THROTTLE_RATES': {scope: '1/day'}
},
):
reload_module(throttling)
reload_module(views)
reload_module(decorators)
# view definitions and checks
...
reload_module(throttling)
reload_module(views)
reload_module(decorators)I've added extra checks to verify that FBV are aligned with CBV in terms of misconfigurations.
There was a problem hiding this comment.
Fair enough, thanks for looking into it
The new decorator allows to set throttle scope for function based views. The implementation was suggested in #9942.
The change includes a test for the new decorator and mention of it in the docs.