From b222722e253953b0067cfd0e6bccfa278cfd3218 Mon Sep 17 00:00:00 2001 From: Nathan Shafer Date: Tue, 2 Aug 2016 15:51:49 -0700 Subject: [PATCH] Added support for custom delimiters between tags. --- README.md | 8 +++++++- example_app/example_app/settings.py | 3 +++ taggit_selectize/conf.py | 1 + taggit_selectize/utils.py | 27 +++++++++++++++++++++++++-- taggit_selectize/widgets.py | 2 ++ 5 files changed, 38 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a5a13e0..29033b2 100644 --- a/README.md +++ b/README.md @@ -48,9 +48,10 @@ Usage ``` 2. Configured Taggit in your Django settings to use a custom string-to-tag parser that doesn't parse on spaces to match the functionality of -Selectize.js +Selectize.js, and a custom tag joiner that supports configurable delimiters. ``` TAGGIT_TAGS_FROM_STRING = 'taggit_selectize.utils.parse_tags' + TAGGIT_STRING_FROM_TAGS = 'taggit_selectize.utils.join_tags' ``` 3. Update urls.py. @@ -96,6 +97,7 @@ TAGGIT_SELECTIZE = { 'REMOVE_BUTTON': False, 'RESTORE_ON_BACKSPACE': False, 'DRAG_DROP': False, + 'DELIMITER': ',' } ``` @@ -133,6 +135,10 @@ Adds the 'restore_on_backspace' plugin to selectize.js. Adds the 'drag_drop' plugin to selectize.js. WARNING: This requires JQuery UI (Sortable) to be installed. If it's not, then selectize.js will throw an error in the console and refuse to run. +### DELIMITER + +Set the delimiter between tags, for example, ';' or '|'. Make sure you have set up the custom TAGGIT_STRING_FROM_TAGS for this to work properly +with Taggit. Default is comma, ','. Demo app -------- diff --git a/example_app/example_app/settings.py b/example_app/example_app/settings.py index 7988282..9c830dd 100644 --- a/example_app/example_app/settings.py +++ b/example_app/example_app/settings.py @@ -123,3 +123,6 @@ # Configure Taggit to use our own parser that prevents tags from getting split on spaces. We only want comma separated # tags to properly work with Selectize.js TAGGIT_TAGS_FROM_STRING = 'taggit_selectize.utils.parse_tags' + +# Configure Taggit to use our tag joiner so that custom DELIMITERs are supported +TAGGIT_STRING_FROM_TAGS = 'taggit_selectize.utils.join_tags' diff --git a/taggit_selectize/conf.py b/taggit_selectize/conf.py index 974adb9..789f39c 100644 --- a/taggit_selectize/conf.py +++ b/taggit_selectize/conf.py @@ -18,6 +18,7 @@ 'REMOVE_BUTTON': False, 'RESTORE_ON_BACKSPACE': False, 'DRAG_DROP': False, + 'DELIMITER': ',', } user_settings = getattr(settings, 'TAGGIT_SELECTIZE', {}) diff --git a/taggit_selectize/utils.py b/taggit_selectize/utils.py index cf1271d..633ab27 100644 --- a/taggit_selectize/utils.py +++ b/taggit_selectize/utils.py @@ -2,7 +2,7 @@ from django.utils import six from django.utils.encoding import force_text from taggit.utils import split_strip - +from .conf import settings def parse_tags(tagstring): """ @@ -53,9 +53,32 @@ def parse_tags(tagstring): to_be_split.append(''.join(buffer)) if to_be_split: for chunk in to_be_split: - words.extend(split_strip(chunk, ',')) + words.extend(split_strip(chunk, settings.TAGGIT_SELECTIZE['DELIMITER'])) words = list(set(words)) words.sort() return words +def join_tags(tags): + """ + Given list of ``Tag`` instances, creates a string representation of + the list suitable for editing by the user, such that submitting the + given string representation back without changing it will give the + same list of tags. + + Tag names which contain DELIMITER will be double quoted. + + Adapted from Taggit's _edit_string_for_tags() + + Ported from Jonathan Buchanan's `django-tagging + `_ + """ + names = [] + delimiter = settings.TAGGIT_SELECTIZE['DELIMITER'] + for tag in tags: + name = tag.name + if delimiter in name or ' ' in name: + names.append('"%s"' % name) + else: + names.append(name) + return delimiter.join(sorted(names)) diff --git a/taggit_selectize/widgets.py b/taggit_selectize/widgets.py index 6070739..28d6bfe 100644 --- a/taggit_selectize/widgets.py +++ b/taggit_selectize/widgets.py @@ -39,6 +39,7 @@ def render(self, name, value, attrs=None): preload: %(preload)s, addPrecedence: %(add_precedence)s, selectOnTab: %(select_on_tab)s, + delimiter: '%(delimiter)s', plugins: [%(plugins)s], render: { option: function(item, escape) { @@ -84,6 +85,7 @@ def render(self, name, value, attrs=None): 'preload': "true" if settings.TAGGIT_SELECTIZE['PRELOAD'] else "false", 'add_precedence': "true" if settings.TAGGIT_SELECTIZE['ADD_PRECEDENCE'] else "false", 'select_on_tab': "true" if settings.TAGGIT_SELECTIZE['SELECT_ON_TAB'] else "false", + 'delimiter': settings.TAGGIT_SELECTIZE['DELIMITER'], 'plugins': ",".join(["\"{}\"".format(plugin) for plugin in js_plugins]), 'remote_url': reverse('tags_recommendation') }