Skip to content

Commit

Permalink
refactor: extract json_required view decorator (apache#18170)
Browse files Browse the repository at this point in the history
* refactor: extract json_required view decorator

* chore: rename json_required to requires_json

* refactor: add requires_form_data decorator and use it

* fix: fix lint issue, raise InvalidPayloadFormatError for invalid payload
  • Loading branch information
ad-m authored and shcoderAlex committed Feb 7, 2022
1 parent 9ca633b commit 5a51289
Show file tree
Hide file tree
Showing 13 changed files with 106 additions and 56 deletions.
12 changes: 7 additions & 5 deletions superset/annotation_layers/annotations/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@
from superset.annotation_layers.commands.exceptions import AnnotationLayerNotFoundError
from superset.constants import MODEL_API_RW_METHOD_PERMISSION_MAP, RouteMethod
from superset.models.annotations import Annotation
from superset.views.base_api import BaseSupersetModelRestApi, statsd_metrics
from superset.views.base_api import (
BaseSupersetModelRestApi,
requires_json,
statsd_metrics,
)

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -254,6 +258,7 @@ def get( # pylint: disable=arguments-differ
@safe
@statsd_metrics
@permission_name("post")
@requires_json
def post(self, pk: int) -> Response: # pylint: disable=arguments-differ
"""Creates a new Annotation
---
Expand Down Expand Up @@ -294,8 +299,6 @@ def post(self, pk: int) -> Response: # pylint: disable=arguments-differ
500:
$ref: '#/components/responses/500'
"""
if not request.is_json:
return self.response_400(message="Request is not JSON")
try:
item = self.add_model_schema.load(request.json)
item["layer"] = pk
Expand Down Expand Up @@ -323,6 +326,7 @@ def post(self, pk: int) -> Response: # pylint: disable=arguments-differ
@safe
@statsd_metrics
@permission_name("put")
@requires_json
def put( # pylint: disable=arguments-differ
self, pk: int, annotation_id: int
) -> Response:
Expand Down Expand Up @@ -370,8 +374,6 @@ def put( # pylint: disable=arguments-differ
500:
$ref: '#/components/responses/500'
"""
if not request.is_json:
return self.response_400(message="Request is not JSON")
try:
item = self.edit_model_schema.load(request.json)
item["layer"] = pk
Expand Down
12 changes: 7 additions & 5 deletions superset/annotation_layers/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@
from superset.constants import MODEL_API_RW_METHOD_PERMISSION_MAP, RouteMethod
from superset.extensions import event_logger
from superset.models.annotations import AnnotationLayer
from superset.views.base_api import BaseSupersetModelRestApi, statsd_metrics
from superset.views.base_api import (
BaseSupersetModelRestApi,
requires_json,
statsd_metrics,
)

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -171,6 +175,7 @@ def delete(self, pk: int) -> Response:
action=lambda self, *args, **kwargs: f"{self.__class__.__name__}.post",
log_to_statsd=False,
)
@requires_json
def post(self) -> Response:
"""Creates a new Annotation Layer
---
Expand Down Expand Up @@ -205,8 +210,6 @@ def post(self) -> Response:
500:
$ref: '#/components/responses/500'
"""
if not request.is_json:
return self.response_400(message="Request is not JSON")
try:
item = self.add_model_schema.load(request.json)
# This validates custom Schema with custom validations
Expand Down Expand Up @@ -237,6 +240,7 @@ def post(self) -> Response:
action=lambda self, *args, **kwargs: f"{self.__class__.__name__}.put",
log_to_statsd=False,
)
@requires_json
def put(self, pk: int) -> Response:
"""Updates an Annotation Layer
---
Expand Down Expand Up @@ -277,8 +281,6 @@ def put(self, pk: int) -> Response:
500:
$ref: '#/components/responses/500'
"""
if not request.is_json:
return self.response_400(message="Request is not JSON")
try:
item = self.edit_model_schema.load(request.json)
item["layer"] = pk
Expand Down
9 changes: 5 additions & 4 deletions superset/charts/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@
from superset.views.base_api import (
BaseSupersetModelRestApi,
RelatedFieldFilter,
requires_form_data,
requires_json,
statsd_metrics,
)
from superset.views.filters import FilterRelatedOwners
Expand Down Expand Up @@ -239,6 +241,7 @@ def ensure_thumbnails_enabled(self) -> Optional[Response]:
action=lambda self, *args, **kwargs: f"{self.__class__.__name__}.post",
log_to_statsd=False,
)
@requires_json
def post(self) -> Response:
"""Creates a new Chart
---
Expand Down Expand Up @@ -273,8 +276,6 @@ def post(self) -> Response:
500:
$ref: '#/components/responses/500'
"""
if not request.is_json:
return self.response_400(message="Request is not JSON")
try:
item = self.add_model_schema.load(request.json)
# This validates custom Schema with custom validations
Expand Down Expand Up @@ -302,6 +303,7 @@ def post(self) -> Response:
action=lambda self, *args, **kwargs: f"{self.__class__.__name__}.put",
log_to_statsd=False,
)
@requires_json
def put(self, pk: int) -> Response:
"""Changes a Chart
---
Expand Down Expand Up @@ -345,8 +347,6 @@ def put(self, pk: int) -> Response:
500:
$ref: '#/components/responses/500'
"""
if not request.is_json:
return self.response_400(message="Request is not JSON")
try:
item = self.edit_model_schema.load(request.json)
# This validates custom Schema with custom validations
Expand Down Expand Up @@ -820,6 +820,7 @@ def favorite_status(self, **kwargs: Any) -> Response:
action=lambda self, *args, **kwargs: f"{self.__class__.__name__}.import_",
log_to_statsd=False,
)
@requires_form_data
def import_(self) -> Response:
"""Import chart(s) with associated datasets and databases
---
Expand Down
9 changes: 5 additions & 4 deletions superset/dashboards/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@
from superset.views.base_api import (
BaseSupersetModelRestApi,
RelatedFieldFilter,
requires_form_data,
requires_json,
statsd_metrics,
)
from superset.views.filters import FilterRelatedOwners
Expand Down Expand Up @@ -430,6 +432,7 @@ def get_charts(self, id_or_slug: str) -> Response:
action=lambda self, *args, **kwargs: f"{self.__class__.__name__}.post",
log_to_statsd=False,
)
@requires_json
def post(self) -> Response:
"""Creates a new Dashboard
---
Expand Down Expand Up @@ -466,8 +469,6 @@ def post(self) -> Response:
500:
$ref: '#/components/responses/500'
"""
if not request.is_json:
return self.response_400(message="Request is not JSON")
try:
item = self.add_model_schema.load(request.json)
# This validates custom Schema with custom validations
Expand Down Expand Up @@ -495,6 +496,7 @@ def post(self) -> Response:
action=lambda self, *args, **kwargs: f"{self.__class__.__name__}.put",
log_to_statsd=False,
)
@requires_json
def put(self, pk: int) -> Response:
"""Changes a Dashboard
---
Expand Down Expand Up @@ -540,8 +542,6 @@ def put(self, pk: int) -> Response:
500:
$ref: '#/components/responses/500'
"""
if not request.is_json:
return self.response_400(message="Request is not JSON")
try:
item = self.edit_model_schema.load(request.json)
# This validates custom Schema with custom validations
Expand Down Expand Up @@ -927,6 +927,7 @@ def favorite_status(self, **kwargs: Any) -> Response:
action=lambda self, *args, **kwargs: f"{self.__class__.__name__}.import_",
log_to_statsd=False,
)
@requires_form_data
def import_(self) -> Response:
"""Import dashboard(s) with associated charts/datasets/databases
---
Expand Down
12 changes: 7 additions & 5 deletions superset/dashboards/filter_sets/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@
)
from superset.extensions import event_logger
from superset.models.filter_set import FilterSet
from superset.views.base_api import BaseSupersetModelRestApi, statsd_metrics
from superset.views.base_api import (
BaseSupersetModelRestApi,
requires_json,
statsd_metrics,
)

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -193,6 +197,7 @@ def get_list(self, dashboard_id: int, **kwargs: Any) -> Response:
action=lambda self, *args, **kwargs: f"{self.__class__.__name__}.post",
log_to_statsd=False,
)
@requires_json
def post(self, dashboard_id: int) -> Response:
"""
Creates a new Dashboard's Filter Set
Expand Down Expand Up @@ -236,8 +241,6 @@ def post(self, dashboard_id: int) -> Response:
500:
$ref: '#/components/responses/500'
"""
if not request.is_json:
return self.response_400(message="Request is not JSON")
try:
item = self.add_model_schema.load(request.json)
new_model = CreateFilterSetCommand(g.user, dashboard_id, item).run()
Expand All @@ -261,6 +264,7 @@ def post(self, dashboard_id: int) -> Response:
action=lambda self, *args, **kwargs: f"{self.__class__.__name__}.put",
log_to_statsd=False,
)
@requires_json
def put(self, dashboard_id: int, pk: int) -> Response:
"""Changes a Dashboard's Filter set
---
Expand Down Expand Up @@ -308,8 +312,6 @@ def put(self, dashboard_id: int, pk: int) -> Response:
500:
$ref: '#/components/responses/500'
"""
if not request.is_json:
return self.response_400(message="Request is not JSON")
try:
item = self.edit_model_schema.load(request.json)
changed_model = UpdateFilterSetCommand(g.user, dashboard_id, pk, item).run()
Expand Down
23 changes: 11 additions & 12 deletions superset/databases/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,16 @@
from superset.databases.utils import get_table_metadata
from superset.db_engine_specs import get_available_engine_specs
from superset.errors import ErrorLevel, SupersetError, SupersetErrorType
from superset.exceptions import InvalidPayloadFormatError
from superset.extensions import security_manager
from superset.models.core import Database
from superset.typing import FlaskResponse
from superset.utils.core import error_msg_from_exception
from superset.views.base_api import BaseSupersetModelRestApi, statsd_metrics
from superset.views.base_api import (
BaseSupersetModelRestApi,
requires_form_data,
requires_json,
statsd_metrics,
)

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -201,6 +205,7 @@ class DatabaseRestApi(BaseSupersetModelRestApi):
action=lambda self, *args, **kwargs: f"{self.__class__.__name__}.post",
log_to_statsd=False,
)
@requires_json
def post(self) -> Response:
"""Creates a new Database
---
Expand Down Expand Up @@ -237,9 +242,6 @@ def post(self) -> Response:
500:
$ref: '#/components/responses/500'
"""

if not request.is_json:
return self.response_400(message="Request is not JSON")
try:
item = self.add_model_schema.load(request.json)
# This validates custom Schema with custom validations
Expand Down Expand Up @@ -277,6 +279,7 @@ def post(self) -> Response:
action=lambda self, *args, **kwargs: f"{self.__class__.__name__}.put",
log_to_statsd=False,
)
@requires_json
def put(self, pk: int) -> Response:
"""Changes a Database
---
Expand Down Expand Up @@ -320,8 +323,6 @@ def put(self, pk: int) -> Response:
500:
$ref: '#/components/responses/500'
"""
if not request.is_json:
return self.response_400(message="Request is not JSON")
try:
item = self.edit_model_schema.load(request.json)
# This validates custom Schema with custom validations
Expand Down Expand Up @@ -593,6 +594,7 @@ def select_star(
f".test_connection",
log_to_statsd=False,
)
@requires_json
def test_connection(self) -> FlaskResponse:
"""Tests a database connection
---
Expand Down Expand Up @@ -623,8 +625,6 @@ def test_connection(self) -> FlaskResponse:
500:
$ref: '#/components/responses/500'
"""
if not request.is_json:
return self.response_400(message="Request is not JSON")
try:
item = DatabaseTestConnectionSchema().load(request.json)
# This validates custom Schema with custom validations
Expand Down Expand Up @@ -774,6 +774,7 @@ def export(self, **kwargs: Any) -> Response:
action=lambda self, *args, **kwargs: f"{self.__class__.__name__}.import_",
log_to_statsd=False,
)
@requires_form_data
def import_(self) -> Response:
"""Import database(s) with associated datasets
---
Expand Down Expand Up @@ -985,6 +986,7 @@ def available(self) -> Response:
f".validate_parameters",
log_to_statsd=False,
)
@requires_json
def validate_parameters(self) -> FlaskResponse:
"""validates database connection parameters
---
Expand Down Expand Up @@ -1015,9 +1017,6 @@ def validate_parameters(self) -> FlaskResponse:
500:
$ref: '#/components/responses/500'
"""
if not request.is_json:
raise InvalidPayloadFormatError("Request is not JSON")

try:
payload = DatabaseValidateParametersSchema().load(request.json)
except ValidationError as ex:
Expand Down
9 changes: 5 additions & 4 deletions superset/datasets/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@
from superset.views.base_api import (
BaseSupersetModelRestApi,
RelatedFieldFilter,
requires_form_data,
requires_json,
statsd_metrics,
)
from superset.views.filters import FilterRelatedOwners
Expand Down Expand Up @@ -206,6 +208,7 @@ class DatasetRestApi(BaseSupersetModelRestApi):
action=lambda self, *args, **kwargs: f"{self.__class__.__name__}.post",
log_to_statsd=False,
)
@requires_json
def post(self) -> Response:
"""Creates a new Dataset
---
Expand Down Expand Up @@ -240,8 +243,6 @@ def post(self) -> Response:
500:
$ref: '#/components/responses/500'
"""
if not request.is_json:
return self.response_400(message="Request is not JSON")
try:
item = self.add_model_schema.load(request.json)
# This validates custom Schema with custom validations
Expand Down Expand Up @@ -270,6 +271,7 @@ def post(self) -> Response:
action=lambda self, *args, **kwargs: f"{self.__class__.__name__}.put",
log_to_statsd=False,
)
@requires_json
def put(self, pk: int) -> Response:
"""Changes a Dataset
---
Expand Down Expand Up @@ -322,8 +324,6 @@ def put(self, pk: int) -> Response:
if "override_columns" in request.args
else False
)
if not request.is_json:
return self.response_400(message="Request is not JSON")
try:
item = self.edit_model_schema.load(request.json)
# This validates custom Schema with custom validations
Expand Down Expand Up @@ -680,6 +680,7 @@ def bulk_delete(self, **kwargs: Any) -> Response:
action=lambda self, *args, **kwargs: f"{self.__class__.__name__}.import_",
log_to_statsd=False,
)
@requires_form_data
def import_(self) -> Response:
"""Import dataset(s) with associated databases
---
Expand Down
Loading

0 comments on commit 5a51289

Please sign in to comment.