Browse Source

Cleanup, removing Python 2 compat.

master
Erik Stein 3 years ago
parent
commit
6a121cd82b
  1. 4
      shared/utils/fields.py
  2. 4
      shared/utils/management/commands/fix_proxymodel_permissions.py
  3. 12
      shared/utils/models/slugs.py
  4. 40
      shared/utils/text.py

4
shared/utils/fields.py

@ -6,10 +6,6 @@ import re
from .text import slugify
# TODO Remove deprecated location
from .models.slugs import AutoSlugField
def uniquify_field_value(instance, field_name, value, max_length=None, queryset=None):
"""
Makes a char field value unique by appending an index, taking care of the

4
shared/utils/management/commands/fix_proxymodel_permissions.py

@ -1,11 +1,9 @@
# -*- coding: utf-8 -*-
"""Add permissions for proxy model.
This is needed because of the bug https://code.djangoproject.com/ticket/11154
in Django (as of 1.6, it's not fixed).
When a permission is created for a proxy model, it actually creates if for it's
When a permission is created for a proxy model, it gets actually created for the
base model app_label (eg: for "article" instead of "about", for the About proxy
model).

12
shared/utils/models/slugs.py

@ -1,6 +1,4 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import six
from functools import reduce
from django.conf import settings
from django.core import validators
@ -12,12 +10,10 @@ from django.utils.translation import gettext_lazy as _
from dirtyfields import DirtyFieldsMixin
from ..text import slugify, downgrading_slugify, django_slugify
if six.PY3:
from functools import reduce
from ..text import slugify, downgrading_slugify
# TODO: Use english
SLUG_HELP = _("Kurzfassung des Namens für die Adresszeile im Browser. Vorzugsweise englisch, keine Umlaute, nur Bindestrich als Sonderzeichen.")
@ -167,7 +163,7 @@ def slug_tree_mixin_post_save(sender, instance, **kwargs):
if kwargs.get('created'):
# Always get a new database instance before saving again
# or MPTTModel.save() will interpret the newly .save as
# not allowed tree move action
# a forbidden tree move action
# FIXME Not clear if this is a proper solution -> get rid of the slug_path stuff altogether
instance_copy = type(instance).objects.get(pk=instance.pk)
instance_copy.slug_path = instance_copy._get_slug_path()

40
shared/utils/text.py

@ -1,13 +1,10 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import codecs
import translitcodec # provides 'translit/long', used by codecs.encode()
import html
import re
import six
import translitcodec # provides 'translit/long', used by codecs.encode() # noqa
from django.conf import settings
from django.utils.encoding import force_str, smart_str
from django.utils.encoding import force_str
from django.utils.functional import keep_lazy_text
from django.utils.html import mark_safe
from django.utils.text import slugify as django_slugify
@ -43,20 +40,8 @@ SLUGIFY_FUNCTION = getattr(settings, 'SLUGIFY_FUNCTION', downgrading_slugify)
slugify = SLUGIFY_FUNCTION
if six.PY2:
import bs4
def html_entities_to_unicode(html):
# An incoming HTML or XML entity is always converted into the corresponding Unicode character in bs4
return smart_str(bs4.BeautifulSoup(html), 'lxml')
else:
import html
# Works only with Python >= 3.4
def html_entities_to_unicode(html_str):
return html.unescape(html_str)
# html_entities_to_unicode = allow_lazy(html_entities_to_unicode, six.text_type, SafeText)
def html_entities_to_unicode(html_str):
return html.unescape(html_str)
# Translators: Separator between list elements
@ -67,7 +52,7 @@ LAST_WORD_SEPARATOR = gettext_lazy(" and ")
@keep_lazy_text
def get_text_joined(list_, separator=DEFAULT_SEPARATOR, last_word=LAST_WORD_SEPARATOR):
def text_joined(list_, separator=DEFAULT_SEPARATOR, last_word=LAST_WORD_SEPARATOR):
list_ = list(list_)
if len(list_) == 0:
return ''
@ -78,16 +63,19 @@ def get_text_joined(list_, separator=DEFAULT_SEPARATOR, last_word=LAST_WORD_SEPA
force_str(last_word), force_str(list_[-1]))
# TODO Don't match escaped stars (like \*)
b_pattern = re.compile(r"(\*\*)(.*?)\1")
i_pattern = re.compile(r"(\*)(.*?)\1")
u_pattern = re.compile(r"(__)(.*?)\1")
link_pattern = re.compile(r"\[([^\[]+)\]\(([^\)]+)\)")
@keep_lazy_text
def slimdown(text):
"""
Converts simplified markdown (`**`, `*`, `__`) to <b>, <i> und <u> tags.
"""
b_pattern = re.compile(r"(\*\*)(.*?)\1")
i_pattern = re.compile(r"(\*)(.*?)\1")
u_pattern = re.compile(r"(__)(.*?)\1")
link_pattern = re.compile(r"\[([^\[]+)\]\(([^\)]+)\)")
if text:
text, n = re.subn(b_pattern, "<b>\\2</b>", text)
text, n = re.subn(i_pattern, "<i>\\2</i>", text)

Loading…
Cancel
Save