Skip to content

Commit

Permalink
fix: remove standalone (apache#18157)
Browse files Browse the repository at this point in the history
* removed standalone

* Update tests/integration_tests/reports/commands_tests.py

* changed standalone

* added tests

* made function more generic

* Update superset/reports/notifications/email.py

Co-authored-by: Beto Dealmeida <[email protected]>

Co-authored-by: Beto Dealmeida <[email protected]>
  • Loading branch information
2 people authored and ofekisr committed Feb 8, 2022
1 parent 8e3b442 commit 88cd500
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 9 deletions.
8 changes: 7 additions & 1 deletion superset/reports/notifications/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
from superset.reports.notifications.base import BaseNotification
from superset.reports.notifications.exceptions import NotificationError
from superset.utils.core import send_email_smtp
from superset.utils.urls import modify_url_query

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -94,6 +95,11 @@ def _get_content(self) -> EmailContent:
html_table = ""

call_to_action = __("Explore in Superset")
url = (
modify_url_query(self._content.url, standalone="0")
if self._content.url is not None
else ""
)
img_tags = []
for msgid in images.keys():
img_tags.append(
Expand Down Expand Up @@ -122,7 +128,7 @@ def _get_content(self) -> EmailContent:
</head>
<body>
<p>{description}</p>
<b><a href="{self._content.url}">{call_to_action}</a></b><p></p>
<b><a href="{url}">{call_to_action}</a></b><p></p>
{html_table}
{img_tag}
</body>
Expand Down
15 changes: 15 additions & 0 deletions superset/utils/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,18 @@ def headless_url(path: str, user_friendly: bool = False) -> str:
def get_url_path(view: str, user_friendly: bool = False, **kwargs: Any) -> str:
with current_app.test_request_context():
return headless_url(url_for(view, **kwargs), user_friendly=user_friendly)


def modify_url_query(url: str, **kwargs: Any) -> str:
"""
Replace or add parameters to a URL.
"""
parts = list(urllib.parse.urlsplit(url))
params = urllib.parse.parse_qs(parts[3])
for k, v in kwargs.items():
if not isinstance(v, list):
v = [v]
params[k] = v

parts[3] = "&".join(f"{k}={urllib.parse.quote(v[0])}" for k, v in params.items())
return urllib.parse.urlunsplit(parts)
16 changes: 8 additions & 8 deletions tests/integration_tests/reports/commands_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,9 +694,9 @@ def test_email_chart_report_schedule(
# assert that the link sent is correct
assert (
'<a href="http://0.0.0.0:8080/superset/explore/?'
"form_data=%7B%22slice_id%22%3A+"
"form_data=%7B%22slice_id%22%3A%20"
f"{create_report_email_chart.chart.id}%7D&"
'standalone=true&force=false">Explore in Superset</a>'
'standalone=0&force=false">Explore in Superset</a>'
in email_mock.call_args[0][2]
)
# Assert the email smtp address
Expand Down Expand Up @@ -737,9 +737,9 @@ def test_email_chart_report_schedule_force_screenshot(
# assert that the link sent is correct
assert (
'<a href="http://0.0.0.0:8080/superset/explore/?'
"form_data=%7B%22slice_id%22%3A+"
"form_data=%7B%22slice_id%22%3A%20"
f"{create_report_email_chart_force_screenshot.chart.id}%7D&"
'standalone=true&force=true">Explore in Superset</a>'
'standalone=0&force=true">Explore in Superset</a>'
in email_mock.call_args[0][2]
)
# Assert the email smtp address
Expand Down Expand Up @@ -774,9 +774,9 @@ def test_email_chart_alert_schedule(
# assert that the link sent is correct
assert (
'<a href="http://0.0.0.0:8080/superset/explore/?'
"form_data=%7B%22slice_id%22%3A+"
"form_data=%7B%22slice_id%22%3A%20"
f"{create_alert_email_chart.chart.id}%7D&"
'standalone=true&force=true">Explore in Superset</a>'
'standalone=0&force=true">Explore in Superset</a>'
in email_mock.call_args[0][2]
)
# Assert the email smtp address
Expand Down Expand Up @@ -842,9 +842,9 @@ def test_email_chart_report_schedule_with_csv(
# assert that the link sent is correct
assert (
'<a href="http://0.0.0.0:8080/superset/explore/?'
"form_data=%7B%22slice_id%22%3A+"
"form_data=%7B%22slice_id%22%3A%20"
f"{create_report_email_chart_with_csv.chart.id}%7D&"
'standalone=true&force=false">Explore in Superset</a>'
'standalone=0&force=false">Explore in Superset</a>'
in email_mock.call_args[0][2]
)
# Assert the email smtp address
Expand Down
35 changes: 35 additions & 0 deletions tests/unit_tests/utils/urls_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# -*- coding: utf-8 -*-
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from superset.utils.urls import modify_url_query

EXPLORE_CHART_LINK = "http://localhost:9000/superset/explore/?form_data=%7B%22slice_id%22%3A+76%7D&standalone=true&force=false"

EXPLORE_DASHBOARD_LINK = "http://localhost:9000/superset/dashboard/3/?standalone=3"


def test_convert_chart_link() -> None:
test_url = modify_url_query(EXPLORE_CHART_LINK, standalone="0")
assert (
test_url
== "http://localhost:9000/superset/explore/?form_data=%7B%22slice_id%22%3A%2076%7D&standalone=0&force=false"
)


def test_convert_dashboard_link() -> None:
test_url = modify_url_query(EXPLORE_DASHBOARD_LINK, standalone="0")
assert test_url == "http://localhost:9000/superset/dashboard/3/?standalone=0"

0 comments on commit 88cd500

Please sign in to comment.