diff --git a/ursh/indico_ursh/blueprint.py b/ursh/indico_ursh/blueprint.py index 67d40ae..8ce992c 100644 --- a/ursh/indico_ursh/blueprint.py +++ b/ursh/indico_ursh/blueprint.py @@ -18,8 +18,9 @@ from __future__ import unicode_literals from indico.core.plugins import IndicoPluginBlueprint -from indico_ursh.controllers import RHShortenURL +from indico_ursh.controllers import RHDisplayShortenURLPage, RHGetShortURL blueprint = IndicoPluginBlueprint('ursh', 'indico_ursh') -blueprint.add_url_rule('/ursh', 'get_short_url', RHShortenURL, methods=('POST',)) +blueprint.add_url_rule('/ursh', 'get_short_url', RHGetShortURL, methods=('POST',)) +blueprint.add_url_rule('/url-shortener', 'shorten_url', RHDisplayShortenURLPage) diff --git a/ursh/indico_ursh/client/index.js b/ursh/indico_ursh/client/index.js index 4eceab1..8be6c54 100644 --- a/ursh/indico_ursh/client/index.js +++ b/ursh/indico_ursh/client/index.js @@ -35,9 +35,66 @@ async function _makeUrshRequest(originalURL, triggerElement) { $(triggerElement).copyURLTooltip(data.url).show(); } +function _patchURL(url) { + if (url.startsWith(window.location.hostname)) { + return `${window.location.protocol}//${url}`; + } else { + return url; + } +} -$(document).on('click', '.ursh-get', (event) => { - event.preventDefault(); - const originalURL = $(event.target).attr('data-original-url'); - _makeUrshRequest(originalURL, event.target); -}); +function _validateURL(url) { + const re = RegExp(`^(${window.location.protocol}//)?${window.location.hostname}/`); + return re.test(url); +} + +function _getUrshInput() { + const $t = $T.domain('ursh'); + const input = $('#ursh-shorten-input'); + const originalURL = input.val().trim(); + + const tip = ((msg) => { + input.qtip({ + content: { + text: msg + }, + hide: { + event: 'mouseleave', + fixed: true, + delay: 700 + }, + show: { + event: false, + ready: true + } + }); + }); + + if (!originalURL) { + tip($t.gettext('Please fill in a URL to shorten')); + input.focus(); + return null; + } else if (!_validateURL(originalURL)) { + tip($t.gettext('This does not look like a valid URL')); + input.focus(); + return null; + } else { + const patchedURL = _patchURL(originalURL); + input.val(patchedURL); + return patchedURL; + } +} + +$(document) + .on('click', '#ursh-shorten-button', (evt) => { + evt.preventDefault(); + const originalURL = _getUrshInput(); + if (originalURL) { + _makeUrshRequest(originalURL, evt.target); + } + }) + .on('click', '.ursh-get', (evt) => { + evt.preventDefault(); + const originalURL = $(evt.target).attr('data-original-url'); + _makeUrshRequest(originalURL, evt.target); + }); diff --git a/ursh/indico_ursh/controllers.py b/ursh/indico_ursh/controllers.py index 2bb50eb..254f7b2 100644 --- a/ursh/indico_ursh/controllers.py +++ b/ursh/indico_ursh/controllers.py @@ -26,14 +26,15 @@ from indico.core.config import config from indico.web.rh import RH from indico_ursh.util import request_short_url +from indico_ursh.views import WPShortenURLPage -class RHShortenURL(RH): +class RHGetShortURL(RH): """Make a request to the URL shortening service""" @staticmethod def _resolve_full_url(original_url): - if url_parse(original_url).host is not None: + if url_parse(original_url).host: return original_url original_url = original_url.lstrip('/') return posixpath.join(config.BASE_URL, original_url) @@ -49,3 +50,10 @@ class RHShortenURL(RH): self._check_host(full_url) short_url = request_short_url(full_url) return jsonify(url=short_url) + + +class RHDisplayShortenURLPage(RH): + """Provide a simple page, where users can submit a URL to be shortened""" + + def _process(self): + return WPShortenURLPage.render_template('url_shortener.html') diff --git a/ursh/indico_ursh/templates/url_shortener.html b/ursh/indico_ursh/templates/url_shortener.html new file mode 100644 index 0000000..4843c21 --- /dev/null +++ b/ursh/indico_ursh/templates/url_shortener.html @@ -0,0 +1,24 @@ +{% extends 'layout/base.html' %} + +{% block page_class %}fixed-width-standalone-text-page{% endblock %} + +{% block title -%} + {% trans %}URL Shortener{% endtrans %} +{%- endblock %} + +{% block content -%} +
+

+ {% trans -%} + In this page, you can generate short links for any indico URL.
+ Tip: you can also generate short links by using the quick-links in various places within indico! + {%- endtrans %} +

+
+ + +
+
+{%- endblock %} diff --git a/ursh/indico_ursh/util.py b/ursh/indico_ursh/util.py index b9e3a10..105e13a 100644 --- a/ursh/indico_ursh/util.py +++ b/ursh/indico_ursh/util.py @@ -16,9 +16,10 @@ from __future__ import unicode_literals -import requests from werkzeug.exceptions import ServiceUnavailable +import requests + def request_short_url(original_url): from indico_ursh.plugin import UrshPlugin diff --git a/ursh/indico_ursh/views.py b/ursh/indico_ursh/views.py new file mode 100644 index 0000000..7287625 --- /dev/null +++ b/ursh/indico_ursh/views.py @@ -0,0 +1,25 @@ +# This file is part of Indico. +# Copyright (C) 2002 - 2018 European Organization for Nuclear Research (CERN). +# +# Indico is free software; you can redistribute it and/or +# modify it under the terms of the GNU General Public License as +# published by the Free Software Foundation; either version 3 of the +# License, or (at your option) any later version. +# +# Indico is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Indico; if not, see . + +from __future__ import unicode_literals + +from indico.core.plugins import WPJinjaMixinPlugin +from indico.web.views import WPDecorated + + +class WPShortenURLPage(WPJinjaMixinPlugin, WPDecorated): + def _getBody(self, params): + return self._getPageContent(params)