Skip to content

Commit

Permalink
made function more generic
Browse files Browse the repository at this point in the history
  • Loading branch information
AAfghahi committed Jan 26, 2022
1 parent 565e2c1 commit 89d7e58
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 21 deletions.
6 changes: 4 additions & 2 deletions superset/reports/notifications/email.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +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 get_screenshot_explorelink
from superset.utils.urls import modify_url_query

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -95,7 +95,9 @@ def _get_content(self) -> EmailContent:
html_table = ""

call_to_action = __("Explore in Superset")
url = get_screenshot_explorelink(self._content.url)
url = ""
if self._content.url is not None:
url = modify_url_query(self._content.url, standalone="0")
img_tags = []
for msgid in images.keys():
img_tags.append(
Expand Down
22 changes: 14 additions & 8 deletions superset/utils/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# specific language governing permissions and limitations
# under the License.
import urllib
from typing import Any, Optional
from typing import Any

from flask import current_app, url_for

Expand All @@ -35,10 +35,16 @@ def get_url_path(view: str, user_friendly: bool = False, **kwargs: Any) -> str:
return headless_url(url_for(view, **kwargs), user_friendly=user_friendly)


def get_screenshot_explorelink(url: Optional[str]) -> Optional[str]:
data = list(urllib.parse.urlsplit(url))
params = urllib.parse.parse_qs(data[3])
params["standalone"] = ["0"]
data[3] = "&".join(f"{k}={urllib.parse.quote(v[0])}" for k, v in params.items())
url = urllib.parse.urlunsplit(data)
return url
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
6 changes: 3 additions & 3 deletions tests/unit_tests/utils/urls_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from superset.utils.urls import get_screenshot_explorelink
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 = get_screenshot_explorelink(EXPLORE_CHART_LINK)
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 = get_screenshot_explorelink(EXPLORE_DASHBOARD_LINK)
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 89d7e58

Please sign in to comment.