Skip to content

Commit

Permalink
Added support for custom delimiters between tags.
Browse files Browse the repository at this point in the history
  • Loading branch information
nshafer committed Aug 2, 2016
1 parent adc5a30 commit b222722
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 3 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -96,6 +97,7 @@ TAGGIT_SELECTIZE = {
'REMOVE_BUTTON': False,
'RESTORE_ON_BACKSPACE': False,
'DRAG_DROP': False,
'DELIMITER': ','
}
```
Expand Down Expand Up @@ -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
--------
Expand Down
3 changes: 3 additions & 0 deletions example_app/example_app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
1 change: 1 addition & 0 deletions taggit_selectize/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
'REMOVE_BUTTON': False,
'RESTORE_ON_BACKSPACE': False,
'DRAG_DROP': False,
'DELIMITER': ',',
}

user_settings = getattr(settings, 'TAGGIT_SELECTIZE', {})
Expand Down
27 changes: 25 additions & 2 deletions taggit_selectize/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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
<http://django-tagging.googlecode.com/>`_
"""
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))
2 changes: 2 additions & 0 deletions taggit_selectize/widgets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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')
}
Expand Down

0 comments on commit b222722

Please sign in to comment.