From e873e87ffa8491f3b238ee8b85371003fcca549d Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Fri, 14 Jun 2019 15:13:54 +0200 Subject: [PATCH] use unicode_literals (#34008) --- mellon/adapters.py | 34 ++++++++++++++++++---------------- mellon/backends.py | 2 ++ mellon/middleware.py | 2 ++ mellon/models.py | 2 ++ mellon/urls.py | 2 ++ mellon/utils.py | 6 ++++-- mellon/views.py | 21 ++++++++++++--------- 7 files changed, 42 insertions(+), 27 deletions(-) diff --git a/mellon/adapters.py b/mellon/adapters.py index 8c869a3..0a1ba07 100644 --- a/mellon/adapters.py +++ b/mellon/adapters.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + import logging import uuid from xml.etree import ElementTree as ET @@ -56,7 +58,7 @@ class DefaultAdapter(object): response.raise_for_status() except requests.exceptions.RequestException as e: self.logger.error( - u'retrieval of metadata URL %r failed with error %s for %d-th idp', + 'retrieval of metadata URL %r failed with error %s for %d-th idp', idp['METADATA_URL'], e, i) continue idp['METADATA'] = response.text @@ -64,21 +66,21 @@ class DefaultAdapter(object): if idp['METADATA'].startswith('/'): idp['METADATA'] = open(idp['METADATA']).read() else: - self.logger.error(u'missing METADATA or METADATA_URL in %d-th idp', i) + self.logger.error('missing METADATA or METADATA_URL in %d-th idp', i) continue if 'ENTITY_ID' not in idp: try: doc = ET.fromstring(idp['METADATA']) except (TypeError, ET.ParseError): - self.logger.error(u'METADATA of %d-th idp is invalid', i) + self.logger.error('METADATA of %d-th idp is invalid', i) continue if doc.tag != '{%s}EntityDescriptor' % lasso.SAML2_METADATA_HREF: - self.logger.error(u'METADATA of %d-th idp has no EntityDescriptor root tag', i) + self.logger.error('METADATA of %d-th idp has no EntityDescriptor root tag', i) continue if not 'entityID' in doc.attrib: self.logger.error( - u'METADATA of %d-th idp has no entityID attribute on its root tag', i) + 'METADATA of %d-th idp has no entityID attribute on its root tag', i) continue idp['ENTITY_ID'] = doc.attrib['entityID'] yield idp @@ -101,12 +103,12 @@ class DefaultAdapter(object): username = force_text(username_template).format( realm=realm, attributes=saml_attributes, idp=idp)[:30] except ValueError: - self.logger.error(u'invalid username template %r', username_template) + self.logger.error('invalid username template %r', username_template) except (AttributeError, KeyError, IndexError) as e: self.logger.error( - u'invalid reference in username template %r: %s', username_template, e) + 'invalid reference in username template %r: %s', username_template, e) except Exception as e: - self.logger.exception(u'unknown error when formatting username') + self.logger.exception('unknown error when formatting username') else: return username @@ -220,16 +222,16 @@ class DefaultAdapter(object): self.logger.debug('looking for users by attribute %r and user field %r with value %r: not found', saml_attribute, user_field, value) continue - self.logger.info(u'looking for user by attribute %r and user field %r with value %r: found %s', + self.logger.info('looking for user by attribute %r and user field %r with value %r: found %s', saml_attribute, user_field, value, display_truncated_list(users_found)) users.update(users_found) if len(users) == 1: user = list(users)[0] - self.logger.info(u'looking for user by attributes %r: found user %s', + self.logger.info('looking for user by attributes %r: found user %s', lookup_by_attributes, user) return user elif len(users) > 1: - self.logger.warning(u'looking for user by attributes %r: too many users found(%d), failing', + self.logger.warning('looking for user by attributes %r: too many users found(%d), failing', lookup_by_attributes, len(users)) return None @@ -254,10 +256,10 @@ class DefaultAdapter(object): try: value = force_text(tpl).format(realm=realm, attributes=saml_attributes, idp=idp) except ValueError: - self.logger.warning(u'invalid attribute mapping template %r', tpl) + self.logger.warning('invalid attribute mapping template %r', tpl) except (AttributeError, KeyError, IndexError, ValueError) as e: self.logger.warning( - u'invalid reference in attribute mapping template %r: %s', tpl, e) + 'invalid reference in attribute mapping template %r: %s', tpl, e) else: model_field = user._meta.get_field(field) if hasattr(model_field, 'max_length'): @@ -266,7 +268,7 @@ class DefaultAdapter(object): old_value = getattr(user, field) setattr(user, field, value) attribute_set = True - self.logger.info(u'set field %s of user %s to value %r (old value %r)', field, + self.logger.info('set field %s of user %s to value %r (old value %r)', field, user, value, old_value) if attribute_set: user.save() @@ -320,11 +322,11 @@ class DefaultAdapter(object): groups.append(group) for group in Group.objects.filter(pk__in=[g.pk for g in groups]).exclude(user=user): self.logger.info( - u'adding group %s (%s) to user %s (%s)', group, group.pk, user, user.pk) + 'adding group %s (%s) to user %s (%s)', group, group.pk, user, user.pk) User.groups.through.objects.get_or_create(group=group, user=user) qs = User.groups.through.objects.exclude( group__pk__in=[g.pk for g in groups]).filter(user=user) for rel in qs: - self.logger.info(u'removing group %s (%s) from user %s (%s)', rel.group, + self.logger.info('removing group %s (%s) from user %s (%s)', rel.group, rel.group.pk, rel.user, rel.user.pk) qs.delete() diff --git a/mellon/backends.py b/mellon/backends.py index f43c462..575bb08 100644 --- a/mellon/backends.py +++ b/mellon/backends.py @@ -13,6 +13,8 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . +from __future__ import unicode_literals + from django.contrib.auth.backends import ModelBackend from . import utils diff --git a/mellon/middleware.py b/mellon/middleware.py index a0b814a..d8f00c6 100644 --- a/mellon/middleware.py +++ b/mellon/middleware.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + from django.utils.http import urlencode from django.http import HttpResponseRedirect from django.core.urlresolvers import reverse diff --git a/mellon/models.py b/mellon/models.py index 9368a1d..9387ed7 100644 --- a/mellon/models.py +++ b/mellon/models.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + from django.db import models from django.utils.translation import ugettext_lazy as _ from django.conf import settings diff --git a/mellon/urls.py b/mellon/urls.py index ad09beb..db4f58a 100644 --- a/mellon/urls.py +++ b/mellon/urls.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + from django.conf.urls import url import django diff --git a/mellon/utils.py b/mellon/utils.py index 6462f81..1f62409 100644 --- a/mellon/utils.py +++ b/mellon/utils.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + import logging import datetime import importlib @@ -83,8 +85,8 @@ def create_server(request): try: server.addProviderFromBuffer(lasso.PROVIDER_ROLE_IDP, idp['METADATA']) except lasso.Error as e: - logger.error(u'bad metadata in idp %r', idp['ENTITY_ID']) - logger.debug(u'lasso error: %s', e) + logger.error('bad metadata in idp %r', idp['ENTITY_ID']) + logger.debug('lasso error: %s', e) continue cache[root] = server settings._MELLON_SERVER_CACHE = cache diff --git a/mellon/views.py b/mellon/views.py index 5a39adf..7d1c1b8 100644 --- a/mellon/views.py +++ b/mellon/views.py @@ -1,3 +1,5 @@ +from __future__ import unicode_literals + import logging import requests import lasso @@ -99,9 +101,9 @@ class ProfileMixin(object): def show_message_status_is_not_success(self, profile, prefix): status_codes, idp_message = utils.get_status_codes_and_message(profile) - args = [u'%s: status is not success codes: %r', prefix, status_codes] + args = ['%s: status is not success codes: %r', prefix, status_codes] if idp_message: - args[0] += u' message: %s' + args[0] += ' message: %s' args.append(idp_message) self.log.warning(*args) @@ -276,8 +278,8 @@ class LoginView(ProfileMixin, LogMixin, View): try: login.initRequest(message, method) except lasso.ProfileInvalidArtifactError: - self.log.warning(u'artifact is malformed %r', artifact) - return HttpResponseBadRequest(u'artifact is malformed %r' % artifact) + self.log.warning('artifact is malformed %r', artifact) + return HttpResponseBadRequest('artifact is malformed %r' % artifact) except lasso.ServerProviderNotFoundError: self.log.warning('no entity id found for artifact %s', artifact) return HttpResponseBadRequest( @@ -406,14 +408,15 @@ class LoginView(ProfileMixin, LogMixin, View): # lasso>2.5.1 introduced a better API if hasattr(authn_request.extensions, 'any'): authn_request.extensions.any = ( - '%s' % eo_next_url,) + str('%s' % eo_next_url), + ) else: authn_request.extensions.setOriginalXmlnode( - ''' %s - ''' % eo_next_url + ''' % eo_next_url) ) self.set_next_url(next_url) self.add_login_hints(idp, authn_request, request=request, next_url=next_url) @@ -483,7 +486,7 @@ class LogoutView(ProfileMixin, LogMixin, View): self.log.warning('error validating logout request: %r' % e) issuer = request.session.get('mellon_session', {}).get('issuer') if issuer == logout.remoteProviderId: - self.log.info(u'user logged out by IdP SLO request') + self.log.info('user logged out by IdP SLO request') auth.logout(request) try: logout.buildResponseMsg() @@ -520,7 +523,7 @@ class LogoutView(ProfileMixin, LogMixin, View): # set next_url after local logout, as the session is wiped by auth.logout if logout: self.set_next_url(next_url) - self.log.info(u'user logged out, SLO request sent to IdP') + self.log.info('user logged out, SLO request sent to IdP') else: self.log.warning('logout refused referer %r is not of the same origin', referer) return HttpResponseRedirect(next_url) -- 2.20.1