diff --git a/bitpoll/poll/models.py b/bitpoll/poll/models.py index 4b94ed03..8d90bc7f 100644 --- a/bitpoll/poll/models.py +++ b/bitpoll/poll/models.py @@ -16,7 +16,7 @@ from bitpoll.base.models import BitpollUser from bitpoll.base.validators import validate_timezone -from bitpoll.poll.util import DateTimePart, PartialDateTime +from bitpoll.poll.util import DateTimePart, PartialDateTime, split_unescape from django.utils import translation POLL_TYPES = ( @@ -239,7 +239,7 @@ def get_hierarchy(self, tz): if self.date: return [PartialDateTime(self.date, DateTimePart.date, tz), PartialDateTime(self.date, DateTimePart.time, tz)] - return [part.strip() for part in self.text.split("/") if part] + return [part.strip() for part in split_unescape(self.text, "/") if part] def votechoice_count(self): return self.votechoice_set.filter(value__isnull=False).count() diff --git a/bitpoll/poll/templates/poll/universal_choice_creation.html b/bitpoll/poll/templates/poll/universal_choice_creation.html index d2448a7c..6ae59281 100644 --- a/bitpoll/poll/templates/poll/universal_choice_creation.html +++ b/bitpoll/poll/templates/poll/universal_choice_creation.html @@ -115,6 +115,7 @@
{% blocktrans %}If you want to have a '/' in the choice escape it with '\': '\/'.{% endblocktrans %}
diff --git a/bitpoll/poll/util.py b/bitpoll/poll/util.py index 03c23f12..1bcb77b2 100644 --- a/bitpoll/poll/util.py +++ b/bitpoll/poll/util.py @@ -28,3 +28,40 @@ def format(self): return date_format(self.datetime, format='H:i') elif self.part == DateTimePart.datetime: return date_format(self.datetime, format='D, j. N Y H:i') + + +#From: https://stackoverflow.com/questions/18092354/python-split-string-without-splitting-escaped-character +def split_unescape(s, delim, escape='\\', unescape=True): + """ + >>> split_unescape('foo,bar', ',') + ['foo', 'bar'] + >>> split_unescape('foo$,bar', ',', '$') + ['foo,bar'] + >>> split_unescape('foo$$,bar', ',', '$', unescape=True) + ['foo$', 'bar'] + >>> split_unescape('foo$$,bar', ',', '$', unescape=False) + ['foo$$', 'bar'] + >>> split_unescape('foo$', ',', '$', unescape=True) + ['foo$'] + """ + ret = [] + current = [] + itr = iter(s) + for ch in itr: + if ch == escape: + try: + # skip the next character; it has been escaped! + if not unescape: + current.append(escape) + current.append(next(itr)) + except StopIteration: + if unescape: + current.append(escape) + elif ch == delim: + # split! (add current to the list and reset it) + ret.append(''.join(current)) + current = [] + else: + current.append(ch) + ret.append(''.join(current)) + return ret