use six types in string type tests (#38923)

This commit is contained in:
Emmanuel Cazenave 2020-01-07 14:22:16 +01:00
parent 5c6ebaaa6a
commit efa52618c1
7 changed files with 19 additions and 13 deletions

View File

@ -20,6 +20,7 @@ from lxml import etree, objectify as xobject
from django.core.files.storage import default_storage
from django.core.files.base import ContentFile
from django.utils import six
from django.utils.dateparse import parse_date
from passerelle.utils.jsonresponse import APIError
@ -93,7 +94,7 @@ class BaseType(object):
attr = getattr(self, subelt, None)
if not attr:
continue
if isinstance(attr, (str, unicode)):
if isinstance(attr, six.string_types):
tag.append(self.make_element(subelt, attr, namespace=self.namespace))
else:
xml = attr.xml

View File

@ -27,7 +27,7 @@ from cmislib.exceptions import PermissionDeniedException
from cmislib.exceptions import UpdateConflictException
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.utils.six import StringIO
from django.utils.six import StringIO, text_type
from django.utils.six.moves.urllib import error as urllib2
from passerelle.base.models import BaseResource
@ -86,7 +86,7 @@ class CmisConnector(BaseResource):
return True, '"path" is required', None
if not isinstance(data['file'], dict):
return True, '"file" must be a dict', None
if not isinstance(data['path'], unicode):
if not isinstance(data['path'], text_type):
return True, '"path" must be string', None
if not RE_FILE_PATH.match(data['path']):
return True, '"path" must be valid path', None
@ -95,14 +95,14 @@ class CmisConnector(BaseResource):
if 'filename' not in file_:
return True, '"file[\'filename\']" is required', None
if not isinstance(file_['filename'], unicode):
if not isinstance(file_['filename'], text_type):
return True, '"file[\'filename\']" must be string', None
if not RE_FILE_NAME.match(file_['filename']):
return True, '"file[\'filename\']" must be valid file name', None
if 'content' not in file_:
return True, '"file[\'content\']" is required', None
if not isinstance(file_['content'], unicode):
if not isinstance(file_['content'], text_type):
return True, '"file[\'content\']" must be string', None
try:
data['file_byte_content'] = base64.b64decode(file_['content'])

View File

@ -5,6 +5,7 @@ import hashlib
import urllib
import random
from django.utils import six
from django.utils.six.moves.urllib import parse as urlparse
'''Simple signature scheme for query strings'''
@ -34,7 +35,7 @@ def sign_query(query, key, algo='sha256', timestamp=None, nonce=None):
def sign_string(s, key, algo='sha256', timedelta=30):
digestmod = getattr(hashlib, algo)
if isinstance(key, unicode):
if isinstance(key, six.text_type):
key = key.encode('utf-8')
hash = hmac.HMAC(key, digestmod=digestmod, msg=s)
return hash.digest()

View File

@ -21,7 +21,7 @@ from decimal import Decimal
import logging
# handle years before 1900
from django.utils import datetime_safe
from django.utils import datetime_safe, six
# NNNN@mailprov.no emails are not real ones: we will ignore them.
# (They are used by Agora+ to create "fake" login for each user id=NNNN)
@ -39,7 +39,7 @@ logger = logging.getLogger('passerelle.contrib.agoraplus')
def slugify(s, space='-'):
if s is None:
return ''
if not isinstance(s, unicode):
if not isinstance(s, six.text_type):
s = s.decode('utf-8', errors='ignore')
s = unicodedata.normalize('NFKD', s).encode('ascii', 'ignore')
s = re.sub(r'[^\w\s\'%s]' % space, '', s).strip().lower()

View File

@ -30,6 +30,7 @@ from suds.transport.http import HttpAuthenticated
import suds.sudsobject
from django.db import models
from django.utils import six
from django.utils.translation import ugettext_lazy as _
from django.core.cache import cache
@ -124,7 +125,7 @@ class Greco(BaseResource):
soap_headers = []
for num, attachment in enumerate(self.attachments):
filename = attachment.get('filename') or 'file%s.bin' % num
if isinstance(filename, unicode):
if isinstance(filename, six.text_type):
filename = filename.encode('utf-8', 'ignore')
soap_headers.append('<filename%s>%s</filename%s>' % (num, filename, num))
xml_payload = xml_payload.replace('<SOAP-ENV:Header/>',

View File

@ -17,6 +17,8 @@
from datetime import datetime
import calendar
from django.utils import six
ENCODE_TOKENS = {
'integer': 16,
@ -230,7 +232,7 @@ class MSTEEncoder(object):
if ref is not None:
self._push_token_type('ref')
self._push(ref)
elif isinstance(obj, unicode) or isinstance(obj, str):
elif isinstance(obj, six.string_types):
self._encode_string(obj)
elif obj is None:
self._encode_nil()

View File

@ -2,6 +2,7 @@ import json
import logging
import re
from django.utils import six
from django.utils.translation import ugettext_lazy as _
from passerelle.utils.api import endpoint
from passerelle.utils.jsonresponse import APIError
@ -44,9 +45,9 @@ class SMSGatewayMixin(object):
assert 'message' in data, 'missing "message" in JSON payload'
assert 'from' in data, 'missing "from" in JSON payload'
assert 'to' in data, 'missing "to" in JSON payload'
assert isinstance(data['message'], unicode), 'message is not a string'
assert isinstance(data['from'], unicode), 'from is not a string'
assert all(map(lambda x: isinstance(x, unicode), data['to'])), \
assert isinstance(data['message'], six.text_type), 'message is not a string'
assert isinstance(data['from'], six.text_type), 'from is not a string'
assert all(map(lambda x: isinstance(x, six.text_type), data['to'])), \
'to is not a list of strings'
except (ValueError, AssertionError) as e:
raise APIError('Payload error: %s' % e)