From 1f7f3e7860f44dad5afe8e304cd37d22355488fc Mon Sep 17 00:00:00 2001 From: Valentin Deniaud Date: Mon, 20 Sep 2021 14:31:10 +0200 Subject: [PATCH 09/59] misc: fix consider-using-dict-items pylint error (#56982) --- src/authentic2/a2_rbac/management.py | 8 ++++---- src/authentic2/backends/ldap_backend.py | 20 +++++++++---------- src/authentic2/custom_user/apps.py | 4 ++-- .../management/commands/check-and-repair.py | 4 ++-- src/authentic2/utils/misc.py | 12 +++++------ src/authentic2_auth_fc/utils.py | 2 +- 6 files changed, 25 insertions(+), 25 deletions(-) diff --git a/src/authentic2/a2_rbac/management.py b/src/authentic2/a2_rbac/management.py index eaf8ccd1..3686307c 100644 --- a/src/authentic2/a2_rbac/management.py +++ b/src/authentic2/a2_rbac/management.py @@ -33,16 +33,16 @@ def update_ou_admin_roles(ou): else: ou_admin_role = ou.get_admin_role() - for key in MANAGED_CT: + for key, info in MANAGED_CT.items(): ct = ContentType.objects.get_by_natural_key(key[0], key[1]) model_class = ct.model_class() ou_model = get_fk_model(model_class, 'ou') # do not create scoped admin roles if the model is not scopable if not ou_model: continue - name = str(MANAGED_CT[key]['name']) + name = str(info['name']) slug = '_a2-' + slugify(name) - scoped_name = str(MANAGED_CT[key]['scoped_name']) + scoped_name = str(info['scoped_name']) name = scoped_name.format(ou=ou) ou_slug = slug + '-' + ou.slug if app_settings.MANAGED_CONTENT_TYPES == (): @@ -56,7 +56,7 @@ def update_ou_admin_roles(ou): ou_ct_admin_role.add_child(ou_admin_role) else: ou_ct_admin_role.remove_child(ou_admin_role) - if MANAGED_CT[key].get('must_view_user'): + if info.get('must_view_user'): ou_ct_admin_role.permissions.add(utils.get_view_user_perm(ou)) ou_ct_admin_role.permissions.add(utils.get_search_ou_perm(ou)) diff --git a/src/authentic2/backends/ldap_backend.py b/src/authentic2/backends/ldap_backend.py index a396f335..efc8c043 100644 --- a/src/authentic2/backends/ldap_backend.py +++ b/src/authentic2/backends/ldap_backend.py @@ -925,12 +925,12 @@ class LDAPBackend: user_attributes[to_user] = '' else: user_attributes[to_user] = attributes[from_ldap][0] - for name in user_attributes: + for name, new_value in user_attributes.items(): value = getattr(user.attributes, name, None) - if value != user_attributes[name]: + if value != new_value: if not user.pk: user.save() - setattr(user.attributes, name, user_attributes[name]) + setattr(user.attributes, name, new_value) user._changed = True def populate_admin_flags_by_group(self, user, block, group_dns): @@ -1825,28 +1825,28 @@ class LDAPBackend: if i in block and isinstance(block[i], str): block[i] = (block[i],) - for d in cls._DEFAULTS: + for d, value in cls._DEFAULTS.items(): if d not in block: - block[d] = cls._DEFAULTS[d] + block[d] = value if d == 'user_filter' and app_settings.A2_ACCEPT_EMAIL_AUTHENTICATION: block[d] = '(|(mail=%s)(uid=%s))' else: - if isinstance(cls._DEFAULTS[d], str): + if isinstance(value, str): if not isinstance(block[d], str): raise ImproperlyConfigured('LDAP_AUTH_SETTINGS: attribute %r must be a string' % d) try: block[d] = force_text(block[d]) except UnicodeEncodeError: raise ImproperlyConfigured('LDAP_AUTH_SETTINGS: attribute %r must be a string' % d) - if isinstance(cls._DEFAULTS[d], bool) and not isinstance(block[d], bool): + if isinstance(value, bool) and not isinstance(block[d], bool): raise ImproperlyConfigured('LDAP_AUTH_SETTINGS: attribute %r must be a boolean' % d) - if isinstance(cls._DEFAULTS[d], (list, tuple)) and not isinstance(block[d], (list, tuple)): + if isinstance(value, (list, tuple)) and not isinstance(block[d], (list, tuple)): raise ImproperlyConfigured( 'LDAP_AUTH_SETTINGS: attribute %r must be a list or a tuple' % d ) - if isinstance(cls._DEFAULTS[d], dict) and not isinstance(block[d], dict): + if isinstance(value, dict) and not isinstance(block[d], dict): raise ImproperlyConfigured('LDAP_AUTH_SETTINGS: attribute %r must be a dictionary' % d) - if not isinstance(cls._DEFAULTS[d], bool) and d in cls._REQUIRED and not block[d]: + if not isinstance(value, bool) and d in cls._REQUIRED and not block[d]: raise ImproperlyConfigured('LDAP_AUTH_SETTINGS: attribute %r is required but is empty') # force_bytes all strings in iterable or dict if isinstance(block[d], (list, tuple, dict)): diff --git a/src/authentic2/custom_user/apps.py b/src/authentic2/custom_user/apps.py index b2d44522..e025c4da 100644 --- a/src/authentic2/custom_user/apps.py +++ b/src/authentic2/custom_user/apps.py @@ -75,11 +75,11 @@ class CustomUserConfig(AppConfig): serialize = get_kind('string').get('serialize') for user in User.objects.all(): - for attr_name in attrs: + for attr_name, value in attrs.items(): av, created = AttributeValue.objects.get_or_create( content_type=content_type, object_id=user.id, - attribute=attrs[attr_name], + attribute=value, defaults={ 'multiple': False, 'verified': False, diff --git a/src/authentic2/management/commands/check-and-repair.py b/src/authentic2/management/commands/check-and-repair.py index c8d6fede..6e84c4e3 100644 --- a/src/authentic2/management/commands/check-and-repair.py +++ b/src/authentic2/management/commands/check-and-repair.py @@ -337,8 +337,8 @@ class Command(BaseCommand): roles_to_fix[ou] = wrong_ou_roles if roles_to_fix and self.do_repair(): self.notice('Changing ou of admin roles...', ending=' ') - for ou in roles_to_fix: - roles_to_fix[ou].update(ou=ou) + for ou, role in roles_to_fix.items(): + role.update(ou=ou) self.success('DONE!') def check_manager_of_roles(self): diff --git a/src/authentic2/utils/misc.py b/src/authentic2/utils/misc.py index 294137b2..0c270f4d 100644 --- a/src/authentic2/utils/misc.py +++ b/src/authentic2/utils/misc.py @@ -76,12 +76,12 @@ class MWT: def collect(self): """Clear cache of results which have timed out""" - for func in self._caches: - cache = {} - for key in self._caches[func]: - if (time.time() - self._caches[func][key][1]) < self._timeouts[func]: - cache[key] = self._caches[func][key] - self._caches[func] = cache + for func, cache in self._caches.items(): + updated_cache = {} + for key in cache: + if (time.time() - cache[key][1]) < self._timeouts[func]: + updated_cache[key] = cache[key] + cache = updated_cache def __call__(self, f): self.cache = self._caches[f] = {} diff --git a/src/authentic2_auth_fc/utils.py b/src/authentic2_auth_fc/utils.py index 1a71dd6b..b795a313 100644 --- a/src/authentic2_auth_fc/utils.py +++ b/src/authentic2_auth_fc/utils.py @@ -195,7 +195,7 @@ class RequestError(Exception): s = super().__str__() if self.details: s += ' (' - s += ' '.join('%s=%r' % (key, self.details[key]) for key in self.details) + s += ' '.join('%s=%r' % (k, v) for k, v in self.details.items()) s += ')' return s -- 2.30.2