mirror of
https://github.com/lucaspalomodevelop/indico-plugins.git
synced 2026-03-12 23:27:22 +00:00
URSH: Fix various issues
This commit is contained in:
parent
43a3fa79aa
commit
796a7fd9e3
@ -60,31 +60,37 @@ class RHCustomShortURLPage(RHManageEventBase):
|
||||
def _make_absolute_url(self, url):
|
||||
return posixpath.join(config.BASE_URL, url[1:]) if url.startswith('/') else url
|
||||
|
||||
def _get_error_msg(self, response):
|
||||
if response['status'] == 403:
|
||||
def _get_error_msg(self, result):
|
||||
if result['status'] == 409:
|
||||
return _('Shortcut already exists')
|
||||
elif result['status'] == 400:
|
||||
return _('Malformed shortcut')
|
||||
return result['error'].get('description')
|
||||
|
||||
def _process_args(self):
|
||||
from indico_ursh.plugin import UrshPlugin
|
||||
super(RHCustomShortURLPage, self)._process_args()
|
||||
api_host = url_parse(UrshPlugin.settings.get('api_host'))
|
||||
self.ursh_host = strip_end(api_host.to_url(), api_host.path[1:])
|
||||
self.ursh_host = strip_end(api_host.to_url(), api_host.path[1:]).rstrip('/') + '/'
|
||||
|
||||
def _process_GET(self):
|
||||
original_url = self._make_absolute_url(request.args['original_url'])
|
||||
return WPShortenURLPage.render_template('ursh_custom_shortener_page.html',
|
||||
event=self.event,
|
||||
ursh_host=self.ursh_host,
|
||||
original_url=original_url)
|
||||
original_url=original_url,
|
||||
submitted=False)
|
||||
|
||||
def _process_POST(self):
|
||||
original_url = self._make_absolute_url(request.args['original_url'])
|
||||
shortcut = request.form['shortcut']
|
||||
shortcut = request.form['shortcut'].strip()
|
||||
result = register_shortcut(original_url, shortcut)
|
||||
|
||||
error = result.get('error')
|
||||
kwargs = {'success': True} if not error else {'success': False, 'msg': self._get_error_msg(result)}
|
||||
if result.get('error'):
|
||||
kwargs = {'success': False, 'msg': self._get_error_msg(result)}
|
||||
else:
|
||||
kwargs = {'success': True, 'shorturl': result['short_url']}
|
||||
|
||||
return jsonify_template('ursh_custom_shortener_page.html', render_plugin_template,
|
||||
event=self.event, ursh_host=self.ursh_host, shortcut=shortcut,
|
||||
original_url=original_url, **kwargs)
|
||||
original_url=original_url, submitted=True, **kwargs)
|
||||
|
||||
@ -13,7 +13,6 @@ from wtforms.fields.html5 import URLField
|
||||
from wtforms.validators import DataRequired
|
||||
|
||||
from indico.core.plugins import IndicoPlugin
|
||||
from indico.web.flask.util import url_for
|
||||
from indico.web.forms.base import IndicoForm
|
||||
from indico.web.views import WPBase
|
||||
|
||||
@ -56,5 +55,4 @@ class UrshPlugin(IndicoPlugin):
|
||||
dropdown=dropdown, element_class=element_class, text=text, **kwargs)
|
||||
|
||||
def _inject_ursh_footer(self, **kwargs):
|
||||
url = url_for('plugin_ursh.shorten_url')
|
||||
return render_plugin_template('ursh_footer.html')
|
||||
|
||||
@ -7,14 +7,14 @@
|
||||
{%- endblock %}
|
||||
|
||||
{% block content -%}
|
||||
{% if success == False %}
|
||||
{% if submitted and not success %}
|
||||
<div class="error-message-box">
|
||||
<div class="message-box-content">
|
||||
<span class="icon"></span>
|
||||
<div class="message-text">{% trans %}Custom URL creation failed:{% endtrans %} {{ msg }}</div>
|
||||
</div>
|
||||
</div>
|
||||
{% elif success == True %}
|
||||
{% elif submitted and success %}
|
||||
<div class="success-message-box">
|
||||
<div class="message-box-content">
|
||||
<span class="icon"></span>
|
||||
@ -27,15 +27,16 @@
|
||||
<input type="url" style="font-family:monospace;" value="{{ original_url }}" disabled>
|
||||
</div>
|
||||
<p>{% trans %}Please enter the desired shortcut below (at least 5 chars).{% endtrans %}</p>
|
||||
<form id="ursh-custom-shortcut-form" method="POST">
|
||||
<form method="POST">
|
||||
<div class="i-has-action">
|
||||
<button id="ursh-host-button" type="button" class="i-button" style="font-family:monospace;" disabled>
|
||||
<button type="button" class="i-button" style="font-family:monospace;" disabled>
|
||||
{{- ursh_host -}}
|
||||
</button>
|
||||
<input id="ursh-custom-shortcut-input" name="shortcut" type="text" style="font-family:monospace;"
|
||||
spellcheck="false" autocomplete="off" autocapitalize="off"
|
||||
{% if shortcut %}value="{{ shortcut }}"{% endif %}
|
||||
placeholder="{% trans %}Enter a shortcut...{% endtrans %}">
|
||||
spellcheck="false" autocomplete="off" autocapitalize="off"
|
||||
pattern="^[0-9a-zA-Z-]{5,}$"
|
||||
{% if shortcut %}value="{{ shortcut }}"{% endif %}
|
||||
placeholder="{% trans %}Enter a shortcut...{% endtrans %}">
|
||||
<button id="ursh-custom-shortcut-submit-button" type="submit" class="i-button" disabled>
|
||||
{% trans -%}Create{%- endtrans %}
|
||||
</button>
|
||||
@ -43,16 +44,13 @@
|
||||
</form>
|
||||
<script>
|
||||
$(document).ready(() => {
|
||||
const inputField = $('#ursh-custom-shortcut-input');
|
||||
inputField.focus().select();
|
||||
$('#ursh-custom-shortcut-input').focus().select();
|
||||
});
|
||||
</script>
|
||||
{% if success == True %}
|
||||
{% if submitted and success and shorturl %}
|
||||
<script>
|
||||
$(document).ready(() => {
|
||||
const hostButton = $('#ursh-host-button');
|
||||
const inputField = $('#ursh-custom-shortcut-input');
|
||||
inputField.copyURLTooltip(`${hostButton.text()}${inputField.val()}`, 'unfocus');
|
||||
$('#ursh-custom-shortcut-input').copyURLTooltip({{ shorturl | tojson }}, 'unfocus');
|
||||
});
|
||||
</script>
|
||||
{% endif %}
|
||||
|
||||
@ -1,12 +1,8 @@
|
||||
{% if url %}
|
||||
<a href="{{ url }}">{% trans %}URL Shortener{% endtrans %}</a>
|
||||
{% else %}
|
||||
<a href="{{ url_for('plugin_ursh.shorten_url') }}"
|
||||
data-title="{{ _("URL Shortener") }}"
|
||||
data-href="{{ url_for('plugin_ursh.shorten_url') }}"
|
||||
data-ajax-dialog
|
||||
data-hide-page-header
|
||||
data-close-button>
|
||||
{% trans %}URL Shortener{% endtrans %}
|
||||
</a>
|
||||
{% endif %}
|
||||
<a href="{{ url_for('plugin_ursh.shorten_url') }}"
|
||||
data-title="{{ _("URL Shortener") }}"
|
||||
data-href="{{ url_for('plugin_ursh.shorten_url', url=request.url) }}"
|
||||
data-ajax-dialog
|
||||
data-hide-page-header
|
||||
data-close-button>
|
||||
{% trans %}URL Shortener{% endtrans %}
|
||||
</a>
|
||||
|
||||
@ -16,6 +16,7 @@
|
||||
<div id="ursh-shorten-request-form" 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"
|
||||
value="{{ request.args.url }}"
|
||||
placeholder="{% trans %}Enter an Indico URL to shorten...{% endtrans %}">
|
||||
<button id="ursh-shorten-button" type="button" class="i-button">{% trans %}Shorten{% endtrans %}</button>
|
||||
</div>
|
||||
|
||||
@ -27,31 +27,29 @@ def _get_settings():
|
||||
def request_short_url(original_url):
|
||||
api_key, api_host = _get_settings()
|
||||
headers = {'Authorization': 'Bearer {api_key}'.format(api_key=api_key)}
|
||||
url = posixpath.join(api_host, 'urls/')
|
||||
|
||||
response = requests.post(api_host, data=dict(url=original_url, allow_reuse=True), headers=headers)
|
||||
if response.status_code not in (400, ):
|
||||
response = requests.post(url, data={'url': original_url, 'allow_reuse': True}, headers=headers)
|
||||
if response.status_code not in (400,):
|
||||
response.raise_for_status()
|
||||
|
||||
data = response.json()
|
||||
return data.get('short_url')
|
||||
return data['short_url']
|
||||
|
||||
|
||||
def register_shortcut(original_url, shortcut):
|
||||
api_key, api_host = _get_settings()
|
||||
headers = {'Authorization': 'Bearer {api_key}'.format(api_key=api_key)}
|
||||
url = posixpath.join(api_host, 'urls', shortcut)
|
||||
|
||||
# ursh expects shortcut as a path argument
|
||||
api_host = posixpath.join(api_host, shortcut)
|
||||
|
||||
response = requests.put(api_host, data=dict(url=original_url), headers=headers)
|
||||
if response.status_code not in (403, ):
|
||||
response = requests.put(url, data={'url': original_url, 'allow_reuse': True}, headers=headers)
|
||||
if not (400 <= response.status_code < 500):
|
||||
response.raise_for_status()
|
||||
|
||||
data = response.json()
|
||||
return data
|
||||
return response.json()
|
||||
|
||||
|
||||
def strip_end(text, suffix):
|
||||
if not text.endswith(suffix):
|
||||
return text
|
||||
return text[:len(text)-len(suffix)]
|
||||
return text[:len(text) - len(suffix)]
|
||||
|
||||
@ -12,5 +12,5 @@ from indico.web.views import WPDecorated
|
||||
|
||||
|
||||
class WPShortenURLPage(WPJinjaMixinPlugin, WPDecorated):
|
||||
def _getBody(self, params):
|
||||
return self._getPageContent(params)
|
||||
def _get_body(self, params):
|
||||
return self._get_page_content(params)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user