mirror of
https://github.com/lucaspalomodevelop/indico-plugins.git
synced 2026-03-13 07:29:39 +00:00
URSH: Add URL shortener page
This commit is contained in:
parent
09539b83b2
commit
eda2d0b43f
@ -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)
|
||||
|
||||
@ -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);
|
||||
});
|
||||
|
||||
@ -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')
|
||||
|
||||
24
ursh/indico_ursh/templates/url_shortener.html
Normal file
24
ursh/indico_ursh/templates/url_shortener.html
Normal file
@ -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 -%}
|
||||
<div class="page-content">
|
||||
<p>
|
||||
{% trans -%}
|
||||
In this page, you can generate short links for any indico URL.<br>
|
||||
<b>Tip:</b> you can also generate short links by using the quick-links in various places within indico!
|
||||
{%- endtrans %}
|
||||
</p>
|
||||
<div class="i-has-action">
|
||||
<input id="ursh-shorten-input" name="ursh-shorten-original-url" type="url" style="font-family:monospace;"
|
||||
spellcheck="false" autocomplete="off" autocapitalize="off"
|
||||
placeholder="{% trans %}Enter an Indico URL to shorten{% endtrans %}">
|
||||
<button id="ursh-shorten-button" type="button" class="i-button">{% trans %}Shorten{% endtrans %}</button>
|
||||
</div>
|
||||
</div>
|
||||
{%- endblock %}
|
||||
@ -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
|
||||
|
||||
25
ursh/indico_ursh/views.py
Normal file
25
ursh/indico_ursh/views.py
Normal file
@ -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 <http://www.gnu.org/licenses/>.
|
||||
|
||||
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)
|
||||
Loading…
x
Reference in New Issue
Block a user