From ff95f9bf78ab533f5c36d3bcf47e4b18b1c47b98 Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Wed, 16 Oct 2019 16:14:04 +0200
Subject: [PATCH] admin: make slug editable on connector create view (#21800)

---
 passerelle/base/models.py                     |   2 +-
 passerelle/static/js/passerelle.js            |   9 ++
 passerelle/static/js/slugify.js               | 137 ++++++++++++++++++
 .../passerelle/manage/service_form.html       |   7 +-
 passerelle/views.py                           |  10 +-
 tests/test_manager.py                         |  20 +++
 6 files changed, 182 insertions(+), 3 deletions(-)
 create mode 100644 passerelle/static/js/slugify.js

diff --git a/passerelle/base/models.py b/passerelle/base/models.py
index 09263f19..6257a550 100644
--- a/passerelle/base/models.py
+++ b/passerelle/base/models.py
@@ -111,8 +111,8 @@ class InheritanceManager(ModelUtilsInheritanceManager):
 
 class BaseResource(models.Model):
     title = models.CharField(max_length=50, verbose_name=_('Title'))
-    description = models.TextField(verbose_name=_('Description'))
     slug = models.SlugField(verbose_name=_('Identifier'), unique=True)
+    description = models.TextField(verbose_name=_('Description'))
     users = models.ManyToManyField(ApiUser, blank=True, related_name='+', related_query_name='+')
     objects = InheritanceManager()
 
diff --git a/passerelle/static/js/passerelle.js b/passerelle/static/js/passerelle.js
index 7cffc7d4..f4612ef8 100644
--- a/passerelle/static/js/passerelle.js
+++ b/passerelle/static/js/passerelle.js
@@ -17,4 +17,13 @@ $(function() {
           var $dialog = $(response).dialog({modal: true, width: 'auto'});
         });
   });
+
+  /* keep title/slug in sync,
+     this code comes from wcs/qommon/static/js/qommon.admin.js
+  */
+  $('body').delegate('input[data-slug-sync]', 'input change paste',
+        function() {
+          var $slug_field = $(this).parents('form').find('[name=' + $(this).data('slug-sync') + ']');
+          $slug_field.val($.slugify($(this).val()));
+  });
 });
diff --git a/passerelle/static/js/slugify.js b/passerelle/static/js/slugify.js
new file mode 100644
index 00000000..f077be16
--- /dev/null
+++ b/passerelle/static/js/slugify.js
@@ -0,0 +1,137 @@
+$(function() {
+/* the slugify code is adapted from the urlify code of django,
+ * django/contrib/admin/static/admin/js/urlify.js.
+ */
+
+var LATIN_MAP = {
+    'À': 'A', 'Á': 'A', 'Â': 'A', 'Ã': 'A', 'Ä': 'A', 'Å': 'A', 'Æ': 'AE', 'Ç':
+    'C', 'È': 'E', 'É': 'E', 'Ê': 'E', 'Ë': 'E', 'Ì': 'I', 'Í': 'I', 'Î': 'I',
+    'Ï': 'I', 'Ð': 'D', 'Ñ': 'N', 'Ò': 'O', 'Ó': 'O', 'Ô': 'O', 'Õ': 'O', 'Ö':
+    'O', 'Ő': 'O', 'Ø': 'O', 'Ù': 'U', 'Ú': 'U', 'Û': 'U', 'Ü': 'U', 'Ű': 'U',
+    'Ý': 'Y', 'Þ': 'TH', 'Ÿ': 'Y', 'ß': 'ss', 'à':'a', 'á':'a', 'â': 'a', 'ã':
+    'a', 'ä': 'a', 'å': 'a', 'æ': 'ae', 'ç': 'c', 'è': 'e', 'é': 'e', 'ê': 'e',
+    'ë': 'e', 'ì': 'i', 'í': 'i', 'î': 'i', 'ï': 'i', 'ð': 'd', 'ñ': 'n', 'ò':
+    'o', 'ó': 'o', 'ô': 'o', 'õ': 'o', 'ö': 'o', 'ő': 'o', 'ø': 'o', 'ù': 'u',
+    'ú': 'u', 'û': 'u', 'ü': 'u', 'ű': 'u', 'ý': 'y', 'þ': 'th', 'ÿ': 'y'
+};
+var LATIN_SYMBOLS_MAP = {
+    '©':'(c)'
+};
+var GREEK_MAP = {
+    'α':'a', 'β':'b', 'γ':'g', 'δ':'d', 'ε':'e', 'ζ':'z', 'η':'h', 'θ':'8',
+    'ι':'i', 'κ':'k', 'λ':'l', 'μ':'m', 'ν':'n', 'ξ':'3', 'ο':'o', 'π':'p',
+    'ρ':'r', 'σ':'s', 'τ':'t', 'υ':'y', 'φ':'f', 'χ':'x', 'ψ':'ps', 'ω':'w',
+    'ά':'a', 'έ':'e', 'ί':'i', 'ό':'o', 'ύ':'y', 'ή':'h', 'ώ':'w', 'ς':'s',
+    'ϊ':'i', 'ΰ':'y', 'ϋ':'y', 'ΐ':'i',
+    'Α':'A', 'Β':'B', 'Γ':'G', 'Δ':'D', 'Ε':'E', 'Ζ':'Z', 'Η':'H', 'Θ':'8',
+    'Ι':'I', 'Κ':'K', 'Λ':'L', 'Μ':'M', 'Ν':'N', 'Ξ':'3', 'Ο':'O', 'Π':'P',
+    'Ρ':'R', 'Σ':'S', 'Τ':'T', 'Υ':'Y', 'Φ':'F', 'Χ':'X', 'Ψ':'PS', 'Ω':'W',
+    'Ά':'A', 'Έ':'E', 'Ί':'I', 'Ό':'O', 'Ύ':'Y', 'Ή':'H', 'Ώ':'W', 'Ϊ':'I',
+    'Ϋ':'Y'
+};
+var TURKISH_MAP = {
+    'ş':'s', 'Ş':'S', 'ı':'i', 'İ':'I', 'ç':'c', 'Ç':'C', 'ü':'u', 'Ü':'U',
+    'ö':'o', 'Ö':'O', 'ğ':'g', 'Ğ':'G'
+};
+var RUSSIAN_MAP = {
+    'а':'a', 'б':'b', 'в':'v', 'г':'g', 'д':'d', 'е':'e', 'ё':'yo', 'ж':'zh',
+    'з':'z', 'и':'i', 'й':'j', 'к':'k', 'л':'l', 'м':'m', 'н':'n', 'о':'o',
+    'п':'p', 'р':'r', 'с':'s', 'т':'t', 'у':'u', 'ф':'f', 'х':'h', 'ц':'c',
+    'ч':'ch', 'ш':'sh', 'щ':'sh', 'ъ':'', 'ы':'y', 'ь':'', 'э':'e', 'ю':'yu',
+    'я':'ya',
+    'А':'A', 'Б':'B', 'В':'V', 'Г':'G', 'Д':'D', 'Е':'E', 'Ё':'Yo', 'Ж':'Zh',
+    'З':'Z', 'И':'I', 'Й':'J', 'К':'K', 'Л':'L', 'М':'M', 'Н':'N', 'О':'O',
+    'П':'P', 'Р':'R', 'С':'S', 'Т':'T', 'У':'U', 'Ф':'F', 'Х':'H', 'Ц':'C',
+    'Ч':'Ch', 'Ш':'Sh', 'Щ':'Sh', 'Ъ':'', 'Ы':'Y', 'Ь':'', 'Э':'E', 'Ю':'Yu',
+    'Я':'Ya'
+};
+var UKRAINIAN_MAP = {
+    'Є':'Ye', 'І':'I', 'Ї':'Yi', 'Ґ':'G', 'є':'ye', 'і':'i', 'ї':'yi', 'ґ':'g'
+};
+var CZECH_MAP = {
+    'č':'c', 'ď':'d', 'ě':'e', 'ň': 'n', 'ř':'r', 'š':'s', 'ť':'t', 'ů':'u',
+    'ž':'z', 'Č':'C', 'Ď':'D', 'Ě':'E', 'Ň': 'N', 'Ř':'R', 'Š':'S', 'Ť':'T',
+    'Ů':'U', 'Ž':'Z'
+};
+var POLISH_MAP = {
+    'ą':'a', 'ć':'c', 'ę':'e', 'ł':'l', 'ń':'n', 'ó':'o', 'ś':'s', 'ź':'z',
+    'ż':'z', 'Ą':'A', 'Ć':'C', 'Ę':'E', 'Ł':'L', 'Ń':'N', 'Ó':'O', 'Ś':'S',
+    'Ź':'Z', 'Ż':'Z'
+};
+var LATVIAN_MAP = {
+    'ā':'a', 'č':'c', 'ē':'e', 'ģ':'g', 'ī':'i', 'ķ':'k', 'ļ':'l', 'ņ':'n',
+    'š':'s', 'ū':'u', 'ž':'z', 'Ā':'A', 'Č':'C', 'Ē':'E', 'Ģ':'G', 'Ī':'I',
+    'Ķ':'K', 'Ļ':'L', 'Ņ':'N', 'Š':'S', 'Ū':'U', 'Ž':'Z'
+};
+var ARABIC_MAP = {
+    'أ':'a', 'ب':'b', 'ت':'t', 'ث': 'th', 'ج':'g', 'ح':'h', 'خ':'kh', 'د':'d',
+    'ذ':'th', 'ر':'r', 'ز':'z', 'س':'s', 'ش':'sh', 'ص':'s', 'ض':'d', 'ط':'t',
+    'ظ':'th', 'ع':'aa', 'غ':'gh', 'ف':'f', 'ق':'k', 'ك':'k', 'ل':'l', 'م':'m',
+    'ن':'n', 'ه':'h', 'و':'o', 'ي':'y'
+};
+var LITHUANIAN_MAP = {
+    'ą':'a', 'č':'c', 'ę':'e', 'ė':'e', 'į':'i', 'š':'s', 'ų':'u', 'ū':'u',
+    'ž':'z',
+    'Ą':'A', 'Č':'C', 'Ę':'E', 'Ė':'E', 'Į':'I', 'Š':'S', 'Ų':'U', 'Ū':'U',
+    'Ž':'Z'
+};
+var SERBIAN_MAP = {
+    'ђ':'dj', 'ј':'j', 'љ':'lj', 'њ':'nj', 'ћ':'c', 'џ':'dz', 'đ':'dj',
+    'Ђ':'Dj', 'Ј':'j', 'Љ':'Lj', 'Њ':'Nj', 'Ћ':'C', 'Џ':'Dz', 'Đ':'Dj'
+};
+var AZERBAIJANI_MAP = {
+    'ç':'c', 'ə':'e', 'ğ':'g', 'ı':'i', 'ö':'o', 'ş':'s', 'ü':'u',
+    'Ç':'C', 'Ə':'E', 'Ğ':'G', 'İ':'I', 'Ö':'O', 'Ş':'S', 'Ü':'U'
+};
+
+var ALL_DOWNCODE_MAPS = [
+    LATIN_MAP,
+    LATIN_SYMBOLS_MAP,
+    GREEK_MAP,
+    TURKISH_MAP,
+    RUSSIAN_MAP,
+    UKRAINIAN_MAP,
+    CZECH_MAP,
+    POLISH_MAP,
+    LATVIAN_MAP,
+    ARABIC_MAP,
+    LITHUANIAN_MAP,
+    SERBIAN_MAP,
+    AZERBAIJANI_MAP
+];
+
+var Downcoder = {
+    'Initialize': function() {
+        if (Downcoder.map) {  // already made
+            return;
+        }
+        Downcoder.map = {};
+        Downcoder.chars = [];
+        for (var i=0; i<ALL_DOWNCODE_MAPS.length; i++) {
+            var lookup = ALL_DOWNCODE_MAPS[i];
+            for (var c in lookup) {
+                if (lookup.hasOwnProperty(c)) {
+                    Downcoder.map[c] = lookup[c];
+                }
+            }
+        }
+        for (var k in Downcoder.map) {
+            if (Downcoder.map.hasOwnProperty(k)) {
+                Downcoder.chars.push(k);
+            }
+        }
+        Downcoder.regex = new RegExp(Downcoder.chars.join('|'), 'g');
+    }
+};
+
+$.slugify = function(value) {
+    Downcoder.Initialize();
+    s = value.replace(Downcoder.regex, function(m) {return Downcoder.map[m]; });
+    s = s.replace(/\'/g, ' ');       // keep quotes as spaces
+    s = s.replace(/[^-\w\s]/g, '');  // remove unneeded chars
+    s = s.replace(/^\s+|\s+$/g, ''); // trim leading/trailing spaces
+    s = s.replace(/[-\s]+/g, '-');   // convert spaces to hyphens
+    s = s.toLowerCase();             // convert to lowercase
+    return s;
+}
+});
diff --git a/passerelle/templates/passerelle/manage/service_form.html b/passerelle/templates/passerelle/manage/service_form.html
index 7072bbaf..c7eb154e 100644
--- a/passerelle/templates/passerelle/manage/service_form.html
+++ b/passerelle/templates/passerelle/manage/service_form.html
@@ -1,5 +1,10 @@
 {% extends "passerelle/manage.html" %}
-{% load i18n gadjo %}
+{% load i18n gadjo static %}
+
+{% block extrascripts %}
+<script src="{% static "js/slugify.js" %}"></script>
+{{ block.super }}
+{% endblock %}
 
 {% block breadcrumb %}
 {{ block.super }}
diff --git a/passerelle/views.py b/passerelle/views.py
index 7295afeb..8df56397 100644
--- a/passerelle/views.py
+++ b/passerelle/views.py
@@ -129,6 +129,8 @@ class ManageAddView(TemplateView):
 
 
 class GenericConnectorMixin(object):
+    exclude_fields = ('slug', 'users')
+
     def get_connector(self, **kwargs):
         return kwargs.get('connector')
 
@@ -149,7 +151,7 @@ class GenericConnectorMixin(object):
             self.form_class = modelform_factory(
                 self.model,
                 form=GenericConnectorForm,
-                exclude=('slug', 'users'))
+                exclude=self.exclude_fields)
             for field in self.form_class.base_fields.values():
                 if isinstance(field.widget, ClearableFileInput):
                     field.widget.template_with_initial = ''\
@@ -179,6 +181,7 @@ class GenericConnectorView(GenericConnectorMixin, DetailView):
 
 class GenericCreateConnectorView(GenericConnectorMixin, CreateView):
     template_name = 'passerelle/manage/service_form.html'
+    exclude_fields = ('users',)  # slug not excluded
 
     def form_valid(self, form):
         with transaction.atomic():
@@ -186,6 +189,11 @@ class GenericCreateConnectorView(GenericConnectorMixin, CreateView):
         self.object.availability()
         return response
 
+    def init_stuff(self, request, *args, **kwargs):
+        super(GenericCreateConnectorView, self).init_stuff(request, *args, **kwargs)
+        # tell JS to prepopulate 'slug' field using the 'title' field
+        self.form_class.base_fields['title'].widget.attrs['data-slug-sync'] = 'slug'
+
 
 class GenericEditConnectorView(GenericConnectorMixin, UpdateView):
     template_name = 'passerelle/manage/service_form.html'
diff --git a/tests/test_manager.py b/tests/test_manager.py
index 280ef98c..5d839b42 100644
--- a/tests/test_manager.py
+++ b/tests/test_manager.py
@@ -66,6 +66,7 @@ def test_add_connector(app, admin_user):
     assert 'Geographic information system' in resp.body
     resp = resp.click('Base Adresse Web Service')
     resp.forms[0]['title'] = 'Test Connector'
+    resp.forms[0]['slug'] = 'test-connector'
     resp.forms[0]['description'] = 'Connector for a simple test'
     resp.forms[0]['service_url'] = 'https://api-adresse.data.gouv.fr/'
     resp = resp.forms[0].submit()
@@ -77,6 +78,25 @@ def test_add_connector(app, admin_user):
     resp = app.get('/manage/', status=200)
     assert 'Test Connector' in resp.body
 
+def test_add_connector_unique_slug(app, admin_user):
+    app = login(app)
+    resp = app.get('/manage/', status=200)
+    resp = resp.click('Add Connector')
+    resp = resp.click('Base Adresse Web Service')
+    resp.forms[0]['title'] = 'Test Connector'
+    resp.forms[0]['slug'] = 'test-connector'
+    resp.forms[0]['description'] = 'Connector for a simple test'
+    resp.forms[0]['service_url'] = 'https://api-adresse.data.gouv.fr/'
+    resp1 = resp.forms[0].submit()
+    assert resp1.status_int == 302
+
+    resp2 = resp.forms[0].submit()
+    assert 'There were errors processing your form.' in resp2.body
+    assert 'this Identifier already exists.' in resp2.body
+    resp.forms[0]['slug'] = 'foo'
+    resp2 = resp.forms[0].submit()
+    assert resp2.status_int == 302
+
 def test_visit_connectors(app, admin_user):
     app = login(app)
     resp = app.get('/manage/', status=200)
-- 
2.23.0

