From f396177de88c6d379a22dd629168700d4c39c308 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Lafr=C3=A9choux?= Date: Tue, 6 Sep 2022 00:18:47 +0200 Subject: [PATCH] Add Blueprint._decorate_view_func_or_method_view --- flask_smorest/blueprint.py | 14 ++++++++++++++ flask_smorest/etag.py | 12 +----------- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/flask_smorest/blueprint.py b/flask_smorest/blueprint.py index 06161b35..c8c001aa 100644 --- a/flask_smorest/blueprint.py +++ b/flask_smorest/blueprint.py @@ -305,3 +305,17 @@ def wrapper(*f_args, **f_kwargs): return wrapper return decorator + + def _decorate_view_func_or_method_view(self, decorator, obj): + """Apply decorator to view func or MethodView HTTP methods""" + + # Decorating a MethodView decorates all HTTP methods + if isinstance(obj, type(MethodView)): + for method in self.HTTP_METHODS: + if method in obj.methods: + method_l = method.lower() + func = getattr(obj, method_l) + setattr(obj, method_l, decorator(func)) + return obj + + return decorator(obj) diff --git a/flask_smorest/etag.py b/flask_smorest/etag.py index e3bc3088..84ffe037 100644 --- a/flask_smorest/etag.py +++ b/flask_smorest/etag.py @@ -9,7 +9,6 @@ import hashlib from flask import request, current_app -from flask.views import MethodView from .exceptions import PreconditionRequired, PreconditionFailed, NotModified from .utils import deepupdate, resolve_schema_instance, get_appcontext @@ -98,16 +97,7 @@ def wrapper(*args, **kwargs): return wrapper - # Decorating a MethodView decorates all HTTP methods - if isinstance(obj, type(MethodView)): - for method in self.HTTP_METHODS: - if method in obj.methods: - method_l = method.lower() - func = getattr(obj, method_l) - setattr(obj, method_l, decorator(func)) - return obj - - return decorator(obj) + return self._decorate_view_func_or_method_view(decorator, obj) @staticmethod def _generate_etag(etag_data, extra_data=None):