diff --git a/flask_admin/_compat.py b/flask_admin/_compat.py index 610d29301..5b8fa27bf 100644 --- a/flask_admin/_compat.py +++ b/flask_admin/_compat.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- # flake8: noqa """ flask_admin._compat @@ -11,58 +10,23 @@ :copyright: (c) 2013 by Armin Ronacher. :license: BSD, see LICENSE for more details. """ -import sys -PY2 = sys.version_info[0] == 2 -VER = sys.version_info +text_type = str +string_types = (str,) -if not PY2: - text_type = str - string_types = (str,) - integer_types = (int, ) +itervalues = lambda d: iter(d.values()) +iteritems = lambda d: iter(d.items()) +filter_list = lambda f, l: list(filter(f, l)) - iterkeys = lambda d: iter(d.keys()) - itervalues = lambda d: iter(d.values()) - iteritems = lambda d: iter(d.items()) - filter_list = lambda f, l: list(filter(f, l)) +def as_unicode(s): + if isinstance(s, bytes): + return s.decode('utf-8') - def as_unicode(s): - if isinstance(s, bytes): - return s.decode('utf-8') + return str(s) - return str(s) - - def csv_encode(s): - ''' Returns unicode string expected by Python 3's csv module ''' - return as_unicode(s) - - # Various tools - from functools import reduce - from urllib.parse import urljoin, urlparse, quote -else: - text_type = unicode - string_types = (str, unicode) - integer_types = (int, long) - - iterkeys = lambda d: d.iterkeys() - itervalues = lambda d: d.itervalues() - iteritems = lambda d: d.iteritems() - filter_list = filter - - def as_unicode(s): - if isinstance(s, str): - return s.decode('utf-8') - - return unicode(s) - - def csv_encode(s): - ''' Returns byte string expected by Python 2's csv module ''' - return as_unicode(s).encode('utf-8') - - # Helpers - reduce = __builtins__['reduce'] if isinstance(__builtins__, dict) else __builtins__.reduce - from urlparse import urljoin, urlparse - from urllib import quote +def csv_encode(s): + ''' Returns unicode string expected by Python 3's csv module ''' + return as_unicode(s) def with_metaclass(meta, *bases): @@ -87,11 +51,7 @@ def __new__(cls, name, this_bases, d): try: - from collections import OrderedDict -except ImportError: - from ordereddict import OrderedDict - -try: + # jinja2 3.0.0 from jinja2 import pass_context except ImportError: from jinja2 import contextfunction as pass_context diff --git a/flask_admin/contrib/fileadmin/__init__.py b/flask_admin/contrib/fileadmin/__init__.py index f5c786adb..95c42d95c 100644 --- a/flask_admin/contrib/fileadmin/__init__.py +++ b/flask_admin/contrib/fileadmin/__init__.py @@ -6,13 +6,14 @@ import re import shutil from operator import itemgetter +from urllib.parse import urljoin, quote from flask import flash, redirect, abort, request, send_file from werkzeug.utils import secure_filename from wtforms import fields, validators from flask_admin import form, helpers -from flask_admin._compat import urljoin, as_unicode, quote +from flask_admin._compat import as_unicode from flask_admin.base import BaseView, expose from flask_admin.actions import action, ActionsMixin from flask_admin.babel import gettext, lazy_gettext diff --git a/flask_admin/form/upload.py b/flask_admin/form/upload.py index 3dc7444c4..1afc42f4e 100644 --- a/flask_admin/form/upload.py +++ b/flask_admin/form/upload.py @@ -1,5 +1,6 @@ import os import os.path as op +from urllib.parse import urljoin from werkzeug.utils import secure_filename from werkzeug.datastructures import FileStorage @@ -12,7 +13,7 @@ from flask_admin.helpers import get_url from flask_admin._backwards import Markup -from flask_admin._compat import string_types, urljoin +from flask_admin._compat import string_types try: diff --git a/flask_admin/helpers.py b/flask_admin/helpers.py index da6033715..cb22e100c 100644 --- a/flask_admin/helpers.py +++ b/flask_admin/helpers.py @@ -1,8 +1,10 @@ from re import sub, compile +from urllib.parse import urljoin, urlparse + from flask import g, request, url_for, flash from wtforms.validators import DataRequired, InputRequired -from flask_admin._compat import iteritems, pass_context, urljoin, urlparse +from flask_admin._compat import iteritems, pass_context from ._compat import string_types diff --git a/flask_admin/model/base.py b/flask_admin/model/base.py index 41cccf6f0..a9fe94694 100755 --- a/flask_admin/model/base.py +++ b/flask_admin/model/base.py @@ -5,6 +5,7 @@ import time from math import ceil import inspect +from collections import OrderedDict from werkzeug.utils import secure_filename @@ -28,7 +29,7 @@ get_redirect_target, flash_errors) from flask_admin.tools import rec_getattr from flask_admin._backwards import ObsoleteAttr -from flask_admin._compat import (iteritems, itervalues, OrderedDict, +from flask_admin._compat import (iteritems, itervalues, as_unicode, csv_encode, text_type, pass_context) from .helpers import prettify_name, get_mdict_item_or_list from .ajax import AjaxModelLoader diff --git a/flask_admin/model/template.py b/flask_admin/model/template.py index 495e4199a..05132576f 100644 --- a/flask_admin/model/template.py +++ b/flask_admin/model/template.py @@ -1,4 +1,6 @@ -from flask_admin._compat import pass_context, string_types, reduce +from functools import reduce + +from flask_admin._compat import pass_context, string_types from flask_admin.babel import gettext diff --git a/flask_admin/tools.py b/flask_admin/tools.py index b0533aae2..5723104f9 100644 --- a/flask_admin/tools.py +++ b/flask_admin/tools.py @@ -1,8 +1,9 @@ +from functools import reduce import sys import traceback # Python 3 compatibility -from ._compat import reduce, as_unicode +from ._compat import as_unicode CHAR_ESCAPE = u'.' CHAR_SEPARATOR = u','