Compare commits

...

7 Commits

  1. 10
      shared/utils/dateformat.py
  2. 5
      shared/utils/fields.py
  3. 10
      shared/utils/text.py

10
shared/utils/dateformat.py

@ -115,9 +115,9 @@ def format_date_range(from_date, to_date, variant=DEFAULT_VARIANT):
f = t = "" f = t = ""
if from_date: if from_date:
f = date_format(from_date, get_format(from_format), lang=get_language()) f = date_format(from_date, get_format(from_format, lang=get_language()))
if to_date: if to_date:
t = date_format(to_date, get_format(to_format), lang=get_language()) t = date_format(to_date, get_format(to_format, lang=get_language()))
separator = get_format('DATE_RANGE_SEPARATOR', lang=get_language()) or " - " separator = get_format('DATE_RANGE_SEPARATOR', lang=get_language()) or " - "
return separator.join((f, t)) return separator.join((f, t))
@ -152,13 +152,13 @@ def format_time_range(from_time, to_time, variant=DEFAULT_VARIANT):
from_format = to_format = "q" # get_format(variant + 'TIME_FORMAT', lang=get_language()) from_format = to_format = "q" # get_format(variant + 'TIME_FORMAT', lang=get_language())
if from_time == to_time or not to_time: if from_time == to_time or not to_time:
return time_format(from_time, get_format(from_format), lang=get_language()) return time_format(from_time, get_format(from_format, lang=get_language()))
else: else:
f = t = "" f = t = ""
if from_time: if from_time:
f = time_format(from_time, get_format(from_format), lang=get_language()) f = time_format(from_time, get_format(from_format, lang=get_language()))
if to_time: if to_time:
t = time_format(to_time, get_format(to_format), lang=get_language()) t = time_format(to_time, get_format(to_format, lang=get_language()))
separator = get_format('DATE_RANGE_SEPARATOR', lang=get_language()) or "" separator = get_format('DATE_RANGE_SEPARATOR', lang=get_language()) or ""
return separator.join((f, t)) return separator.join((f, t))

5
shared/utils/fields.py

@ -5,6 +5,7 @@ from __future__ import unicode_literals
import re import re
from django.db.models import fields from django.db.models import fields
from django.utils import six from django.utils import six
from django.utils.encoding import force_text
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
if six.PY3: if six.PY3:
from functools import reduce from functools import reduce
@ -13,7 +14,7 @@ from .text import slugify_long as slugify
from . import SLUG_HELP from . import SLUG_HELP
DEFAULT_SLUG = _("item") DEFAULT_SLUG = "item"
def unique_slug(instance, slug_field, slug_value, max_length=50, queryset=None): def unique_slug(instance, slug_field, slug_value, max_length=50, queryset=None):
@ -22,7 +23,7 @@ def unique_slug(instance, slug_field, slug_value, max_length=50, queryset=None):
""" """
if not slug_value: if not slug_value:
raise ValueError("Cannot uniquify empty slug") raise ValueError("Cannot uniquify empty slug")
orig_slug = slug = slugify(slug_value) orig_slug = slug = slugify(force_text(slug_value))
index = 0 index = 0
if not queryset: if not queryset:
queryset = instance.__class__._default_manager.get_queryset() queryset = instance.__class__._default_manager.get_queryset()

10
shared/utils/text.py

@ -9,23 +9,23 @@ from django.utils.safestring import SafeText
from django.utils.text import slugify from django.utils.text import slugify
from django.utils.translation import ugettext as _, ugettext_lazy from django.utils.translation import ugettext as _, ugettext_lazy
from bs4 import BeautifulStoneSoup from HTMLParser import HTMLParser
import translitcodec # provides 'translit/long', used by codecs.encode() import translitcodec # provides 'translit/long', used by codecs.encode()
import codecs import codecs
@keep_lazy_text
def downgrade(value): def downgrade(value):
""" """
Downgrade unicode to ascii, transliterating accented characters. Downgrade unicode to ascii, transliterating accented characters.
""" """
value = force_text(value) value = force_text(value)
return codecs.encode(value, 'translit/long') return codecs.encode(value, 'translit/long')
downgrade = allow_lazy(downgrade, six.text_type, SafeText)
@keep_lazy_text
def slugify_long(value): def slugify_long(value):
return slugify(downgrade(value)) return slugify(downgrade(value))
slugify_long = allow_lazy(slugify_long, six.text_type, SafeText)
# Spreading umlauts is included in the translit/long codec. # Spreading umlauts is included in the translit/long codec.
@ -33,8 +33,8 @@ slugify_german = slugify_long
def html_entities_to_unicode(html): def html_entities_to_unicode(html):
text = smart_text(BeautifulStoneSoup(html, convertEntities=BeautifulStoneSoup.ALL_ENTITIES)) parser = HTMLParser()
return text return parser.unescape(html)
html_entities_to_unicode = allow_lazy(html_entities_to_unicode, six.text_type, SafeText) html_entities_to_unicode = allow_lazy(html_entities_to_unicode, six.text_type, SafeText)

Loading…
Cancel
Save