You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@superset.apache.org by GitBox <gi...@apache.org> on 2018/06/02 18:08:45 UTC

[GitHub] hughhhh closed pull request #4760: URL shortner for dashboards

hughhhh closed pull request #4760: URL shortner for dashboards
URL: https://github.com/apache/incubator-superset/pull/4760
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/superset/assets/spec/javascripts/explore/components/URLShortLinkButton_spec.jsx b/superset/assets/spec/javascripts/components/URLShortLinkButton_spec.jsx
similarity index 76%
rename from superset/assets/spec/javascripts/explore/components/URLShortLinkButton_spec.jsx
rename to superset/assets/spec/javascripts/components/URLShortLinkButton_spec.jsx
index 986daaa434..98e6e2cf68 100644
--- a/superset/assets/spec/javascripts/explore/components/URLShortLinkButton_spec.jsx
+++ b/superset/assets/spec/javascripts/components/URLShortLinkButton_spec.jsx
@@ -4,13 +4,13 @@ import { describe, it } from 'mocha';
 import { shallow } from 'enzyme';
 
 import { OverlayTrigger } from 'react-bootstrap';
-import URLShortLinkButton from '../../../../src/explore/components/URLShortLinkButton';
+import URLShortLinkButton from '../../../src/components/URLShortLinkButton';
 
 describe('URLShortLinkButton', () => {
   const defaultProps = {
-    slice: {
-      querystring: () => 'query string',
-    },
+    url: 'mockURL',
+    emailSubject: 'Mock Subject',
+    emailContent: 'mock content',
   };
 
   it('renders', () => {
diff --git a/superset/assets/src/explore/components/URLShortLinkButton.jsx b/superset/assets/src/components/URLShortLinkButton.jsx
similarity index 65%
rename from superset/assets/src/explore/components/URLShortLinkButton.jsx
rename to superset/assets/src/components/URLShortLinkButton.jsx
index 223852477e..aa9ae96ef5 100644
--- a/superset/assets/src/explore/components/URLShortLinkButton.jsx
+++ b/superset/assets/src/components/URLShortLinkButton.jsx
@@ -1,13 +1,14 @@
 import React from 'react';
 import PropTypes from 'prop-types';
 import { Popover, OverlayTrigger } from 'react-bootstrap';
-import CopyToClipboard from './../../components/CopyToClipboard';
-import { getShortUrl } from '../../utils/common';
-import { getExploreLongUrl } from '../exploreUtils';
-import { t } from '../../locales';
+import CopyToClipboard from './CopyToClipboard';
+import { getShortUrl } from '../utils/common';
+import { t } from '../locales';
 
 const propTypes = {
-  latestQueryFormData: PropTypes.object.isRequired,
+  url: PropTypes.string,
+  emailSubject: PropTypes.string,
+  emailContent: PropTypes.string,
 };
 
 export default class URLShortLinkButton extends React.Component {
@@ -25,12 +26,11 @@ export default class URLShortLinkButton extends React.Component {
   }
 
   getCopyUrl() {
-    const longUrl = getExploreLongUrl(this.props.latestQueryFormData);
-    getShortUrl(longUrl, this.onShortUrlSuccess.bind(this));
+    getShortUrl(this.props.url, this.onShortUrlSuccess.bind(this));
   }
 
   renderPopover() {
-    const emailBody = t('Check out this chart: %s', this.state.shortUrl);
+    const emailBody = t('%s%s', this.props.emailContent, this.state.shortUrl);
     return (
       <Popover id="shorturl-popover">
         <CopyToClipboard
@@ -38,7 +38,7 @@ export default class URLShortLinkButton extends React.Component {
           copyNode={<i className="fa fa-clipboard" title={t('Copy to clipboard')} />}
         />
         &nbsp;&nbsp;
-        <a href={`mailto:?Subject=Superset%20Slice%20&Body=${emailBody}`}>
+        <a href={`mailto:?Subject=${this.props.emailSubject}%20&Body=${emailBody}`}>
           <i className="fa fa-envelope" />
         </a>
       </Popover>
@@ -62,4 +62,10 @@ export default class URLShortLinkButton extends React.Component {
   }
 }
 
+URLShortLinkButton.defaultProps = {
+  url: window.location.href.substring(window.location.origin.length),
+  emailSubject: '',
+  emailContent: '',
+};
+
 URLShortLinkButton.propTypes = propTypes;
diff --git a/superset/assets/src/dashboard/components/Header.jsx b/superset/assets/src/dashboard/components/Header.jsx
index 52d3024ff9..eabd3f4c07 100644
--- a/superset/assets/src/dashboard/components/Header.jsx
+++ b/superset/assets/src/dashboard/components/Header.jsx
@@ -5,6 +5,7 @@ import Controls from './Controls';
 import EditableTitle from '../../components/EditableTitle';
 import Button from '../../components/Button';
 import FaveStar from '../../components/FaveStar';
+import URLShortLinkButton from '../../components/URLShortLinkButton';
 import InfoTooltipWithTrigger from '../../components/InfoTooltipWithTrigger';
 import { t } from '../../locales';
 
@@ -92,6 +93,12 @@ class Header extends React.PureComponent {
           </h1>
         </div>
         <div className="pull-right" style={{ marginTop: '35px' }}>
+          <span className="m-r-5">
+            <URLShortLinkButton
+              emailSubject="Superset Dashboard"
+              emailContent="Check out this dashboard: "
+            />
+          </span>
           {this.renderEditButton()}
           <Controls
             dashboard={dashboard}
diff --git a/superset/assets/src/explore/components/ExploreActionButtons.jsx b/superset/assets/src/explore/components/ExploreActionButtons.jsx
index ec9d214c07..f383d66b88 100644
--- a/superset/assets/src/explore/components/ExploreActionButtons.jsx
+++ b/superset/assets/src/explore/components/ExploreActionButtons.jsx
@@ -1,11 +1,11 @@
 import React from 'react';
 import PropTypes from 'prop-types';
 import cx from 'classnames';
-import URLShortLinkButton from './URLShortLinkButton';
+import URLShortLinkButton from '../../components/URLShortLinkButton';
 import EmbedCodeButton from './EmbedCodeButton';
 import DisplayQueryButton from './DisplayQueryButton';
 import { t } from '../../locales';
-import { exportChart } from '../exploreUtils';
+import { exportChart, getExploreLongUrl } from '../exploreUtils';
 
 const propTypes = {
   canDownload: PropTypes.oneOfType([PropTypes.string, PropTypes.bool]).isRequired,
@@ -25,7 +25,12 @@ export default function ExploreActionButtons({
   return (
     <div className="btn-group results" role="group">
       {latestQueryFormData &&
-        <URLShortLinkButton latestQueryFormData={latestQueryFormData} />}
+        <URLShortLinkButton
+          url={getExploreLongUrl(latestQueryFormData)}
+          emailSubject="Superset Chart"
+          emailContent="Check out this chart: "
+        />
+      }
 
       {latestQueryFormData &&
         <EmbedCodeButton latestQueryFormData={latestQueryFormData} />}
diff --git a/superset/views/core.py b/superset/views/core.py
index 40d24b268a..e0c4c6a924 100755
--- a/superset/views/core.py
+++ b/superset/views/core.py
@@ -745,13 +745,12 @@ def index(self, url_id):
     @expose('/shortner/', methods=['POST', 'GET'])
     def shortner(self):
         url = request.form.get('data')
-        directory = url.split('?')[0][2:]
         obj = models.Url(url=url)
         db.session.add(obj)
         db.session.commit()
         return Response(
-            '{scheme}://{request.headers[Host]}/{directory}?r={obj.id}'.format(
-                scheme=request.scheme, request=request, directory=directory, obj=obj),
+            '{scheme}://{request.headers[Host]}/r/{obj.id}'.format(
+                scheme=request.scheme, request=request, obj=obj),
             mimetype='text/plain')
 
     @expose('/msg/')
diff --git a/tests/core_tests.py b/tests/core_tests.py
index 34d8a64677..dd6e3d891d 100644
--- a/tests/core_tests.py
+++ b/tests/core_tests.py
@@ -13,6 +13,7 @@
 import logging
 import os
 import random
+import re
 import string
 import unittest
 
@@ -377,7 +378,7 @@ def test_shortner(self):
             'previous_viz_type=sankey'
         )
         resp = self.client.post('/r/shortner/', data=dict(data=data))
-        assert '?r=' in resp.data.decode('utf-8')
+        assert re.search(r'\/r\/[0-9]+', resp.data.decode('utf-8'))
 
     def test_kv(self):
         self.logout()


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@superset.apache.org
For additional commands, e-mail: notifications-help@superset.apache.org