diff --git a/ursh/indico_ursh/controllers.py b/ursh/indico_ursh/controllers.py index 1ac5f4e..e1c013d 100644 --- a/ursh/indico_ursh/controllers.py +++ b/ursh/indico_ursh/controllers.py @@ -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) diff --git a/ursh/indico_ursh/plugin.py b/ursh/indico_ursh/plugin.py index d5cf08e..37da0b8 100644 --- a/ursh/indico_ursh/plugin.py +++ b/ursh/indico_ursh/plugin.py @@ -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') diff --git a/ursh/indico_ursh/templates/ursh_custom_shortener_page.html b/ursh/indico_ursh/templates/ursh_custom_shortener_page.html index 70e2870..30c4721 100644 --- a/ursh/indico_ursh/templates/ursh_custom_shortener_page.html +++ b/ursh/indico_ursh/templates/ursh_custom_shortener_page.html @@ -7,14 +7,14 @@ {%- endblock %} {% block content -%} - {% if success == False %} + {% if submitted and not success %}
{% trans %}Custom URL creation failed:{% endtrans %} {{ msg }}
- {% elif success == True %} + {% elif submitted and success %}
@@ -27,15 +27,16 @@

{% trans %}Please enter the desired shortcut below (at least 5 chars).{% endtrans %}

-
+
- + spellcheck="false" autocomplete="off" autocapitalize="off" + pattern="^[0-9a-zA-Z-]{5,}$" + {% if shortcut %}value="{{ shortcut }}"{% endif %} + placeholder="{% trans %}Enter a shortcut...{% endtrans %}"> @@ -43,16 +44,13 @@ - {% if success == True %} + {% if submitted and success and shorturl %} {% endif %} diff --git a/ursh/indico_ursh/templates/ursh_footer.html b/ursh/indico_ursh/templates/ursh_footer.html index fdd36f5..7732352 100644 --- a/ursh/indico_ursh/templates/ursh_footer.html +++ b/ursh/indico_ursh/templates/ursh_footer.html @@ -1,12 +1,8 @@ -{% if url %} - {% trans %}URL Shortener{% endtrans %} -{% else %} - - {% trans %}URL Shortener{% endtrans %} - -{% endif %} + + {% trans %}URL Shortener{% endtrans %} + diff --git a/ursh/indico_ursh/templates/ursh_shortener_page.html b/ursh/indico_ursh/templates/ursh_shortener_page.html index e6e2287..fc512b8 100644 --- a/ursh/indico_ursh/templates/ursh_shortener_page.html +++ b/ursh/indico_ursh/templates/ursh_shortener_page.html @@ -16,6 +16,7 @@
diff --git a/ursh/indico_ursh/util.py b/ursh/indico_ursh/util.py index 2280b74..8484f6a 100644 --- a/ursh/indico_ursh/util.py +++ b/ursh/indico_ursh/util.py @@ -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)] diff --git a/ursh/indico_ursh/views.py b/ursh/indico_ursh/views.py index 5e86be9..d47b840 100644 --- a/ursh/indico_ursh/views.py +++ b/ursh/indico_ursh/views.py @@ -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)