-
Notifications
You must be signed in to change notification settings - Fork 14.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: Simplify views/base #25948
Merged
john-bodley
merged 2 commits into
apache:master
from
sebastianliebscher:chore/views_base_py
Nov 12, 2023
Merged
chore: Simplify views/base #25948
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,14 +14,16 @@ | |
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
from __future__ import annotations | ||
|
||
import dataclasses | ||
import functools | ||
import logging | ||
import os | ||
import traceback | ||
from datetime import datetime | ||
from importlib.resources import files | ||
from typing import Any, Callable, cast, Optional, Union | ||
from typing import Any, Callable, cast | ||
|
||
import simplejson as json | ||
import yaml | ||
|
@@ -139,15 +141,11 @@ def get_error_msg() -> str: | |
|
||
|
||
def json_error_response( | ||
msg: Optional[str] = None, | ||
msg: str | None = None, | ||
status: int = 500, | ||
payload: Optional[dict[str, Any]] = None, | ||
link: Optional[str] = None, | ||
payload: dict[str, Any] | None = None, | ||
) -> FlaskResponse: | ||
if not payload: | ||
payload = {"error": f"{msg}"} | ||
if link: | ||
payload["link"] = link | ||
payload = payload or {"error": f"{msg}"} | ||
|
||
return Response( | ||
json.dumps(payload, default=utils.json_iso_dttm_ser, ignore_nan=True), | ||
|
@@ -159,10 +157,9 @@ def json_error_response( | |
def json_errors_response( | ||
errors: list[SupersetError], | ||
status: int = 500, | ||
payload: Optional[dict[str, Any]] = None, | ||
payload: dict[str, Any] | None = None, | ||
) -> FlaskResponse: | ||
if not payload: | ||
payload = {} | ||
payload = payload or {} | ||
|
||
payload["errors"] = [dataclasses.asdict(error) for error in errors] | ||
return Response( | ||
|
@@ -182,7 +179,7 @@ def data_payload_response(payload_json: str, has_error: bool = False) -> FlaskRe | |
|
||
|
||
def generate_download_headers( | ||
extension: str, filename: Optional[str] = None | ||
extension: str, filename: str | None = None | ||
) -> dict[str, Any]: | ||
filename = filename if filename else datetime.now().strftime("%Y%m%d_%H%M%S") | ||
content_disp = f"attachment; filename={filename}.{extension}" | ||
|
@@ -192,15 +189,15 @@ def generate_download_headers( | |
|
||
def deprecated( | ||
eol_version: str = "4.0.0", | ||
new_target: Optional[str] = None, | ||
new_target: str | None = None, | ||
) -> Callable[[Callable[..., FlaskResponse]], Callable[..., FlaskResponse]]: | ||
""" | ||
A decorator to set an API endpoint from SupersetView has deprecated. | ||
Issues a log warning | ||
""" | ||
|
||
def _deprecated(f: Callable[..., FlaskResponse]) -> Callable[..., FlaskResponse]: | ||
def wraps(self: "BaseSupersetView", *args: Any, **kwargs: Any) -> FlaskResponse: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this was auto-changed by pre-commit |
||
def wraps(self: BaseSupersetView, *args: Any, **kwargs: Any) -> FlaskResponse: | ||
message = ( | ||
"%s.%s " | ||
"This API endpoint is deprecated and will be removed in version %s" | ||
|
@@ -227,7 +224,7 @@ def api(f: Callable[..., FlaskResponse]) -> Callable[..., FlaskResponse]: | |
return the response in the JSON format | ||
""" | ||
|
||
def wraps(self: "BaseSupersetView", *args: Any, **kwargs: Any) -> FlaskResponse: | ||
def wraps(self: BaseSupersetView, *args: Any, **kwargs: Any) -> FlaskResponse: | ||
try: | ||
return f(self, *args, **kwargs) | ||
except NoAuthorizationError: | ||
|
@@ -249,7 +246,7 @@ def handle_api_exception( | |
exceptions. | ||
""" | ||
|
||
def wraps(self: "BaseSupersetView", *args: Any, **kwargs: Any) -> FlaskResponse: | ||
def wraps(self: BaseSupersetView, *args: Any, **kwargs: Any) -> FlaskResponse: | ||
try: | ||
return f(self, *args, **kwargs) | ||
except SupersetSecurityException as ex: | ||
|
@@ -294,7 +291,7 @@ def json_response(obj: Any, status: int = 200) -> FlaskResponse: | |
) | ||
|
||
def render_app_template( | ||
self, extra_bootstrap_data: Optional[dict[str, Any]] = None | ||
self, extra_bootstrap_data: dict[str, Any] | None = None | ||
) -> FlaskResponse: | ||
payload = { | ||
"user": bootstrap_user_data(g.user, include_perms=True), | ||
|
@@ -335,21 +332,16 @@ def get_environment_tag() -> dict[str, Any]: | |
|
||
|
||
def menu_data(user: User) -> dict[str, Any]: | ||
menu = appbuilder.menu.get_data() | ||
languages = { | ||
lang: {**appbuilder.languages[lang], "url": appbuilder.get_url_for_locale(lang)} | ||
for lang in appbuilder.languages | ||
} | ||
|
||
languages = {} | ||
for lang in appbuilder.languages: | ||
languages[lang] = { | ||
**appbuilder.languages[lang], | ||
"url": appbuilder.get_url_for_locale(lang), | ||
} | ||
brand_text = appbuilder.app.config["LOGO_RIGHT_TEXT"] | ||
if callable(brand_text): | ||
if callable(brand_text := appbuilder.app.config["LOGO_RIGHT_TEXT"]): | ||
brand_text = brand_text() | ||
build_number = appbuilder.app.config["BUILD_NUMBER"] | ||
|
||
return { | ||
"menu": menu, | ||
"menu": appbuilder.menu.get_data(), | ||
"brand": { | ||
"path": appbuilder.app.config["LOGO_TARGET_PATH"] or "/superset/welcome/", | ||
"icon": appbuilder.app_icon, | ||
|
@@ -369,9 +361,9 @@ def menu_data(user: User) -> dict[str, Any]: | |
"documentation_text": appbuilder.app.config["DOCUMENTATION_TEXT"], | ||
"version_string": appbuilder.app.config["VERSION_STRING"], | ||
"version_sha": appbuilder.app.config["VERSION_SHA"], | ||
"build_number": build_number, | ||
"build_number": appbuilder.app.config["BUILD_NUMBER"], | ||
"languages": languages, | ||
"show_language_picker": len(languages.keys()) > 1, | ||
"show_language_picker": len(languages) > 1, | ||
"user_is_anonymous": user.is_anonymous, | ||
"user_info_url": None | ||
if is_feature_enabled("MENU_HIDE_USER_INFO") | ||
|
@@ -595,11 +587,11 @@ class YamlExportMixin: # pylint: disable=too-few-public-methods | |
Used on DatabaseView for cli compatibility | ||
""" | ||
|
||
yaml_dict_key: Optional[str] = None | ||
yaml_dict_key: str | None = None | ||
|
||
@action("yaml_export", __("Export to YAML"), __("Export to YAML?"), "fa-download") | ||
def yaml_export( | ||
self, items: Union[ImportExportMixin, list[ImportExportMixin]] | ||
self, items: ImportExportMixin | list[ImportExportMixin] | ||
) -> FlaskResponse: | ||
if not isinstance(items, list): | ||
items = [items] | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
link
parameter is not used in the codebase at allThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sebastianliebscher it seems like the
payload
argument isn't use in the codebase either.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there is a single usage here
superset/superset/views/core.py
Line 166 in d95c200