Projet

Général

Profil

0044-misc-fix-arguments-renamed-pylint-error-56982.patch

Valentin Deniaud, 21 septembre 2021 17:09

Télécharger (14,3 ko)

Voir les différences:

Subject: [PATCH 44/59] misc: fix arguments-renamed pylint error (#56982)

 src/authentic2/a2_rbac/managers.py            |  4 +-
 src/authentic2/api_views.py                   | 78 +++++++++----------
 src/authentic2/custom_user/managers.py        |  4 +-
 src/authentic2/journal_event_types.py         |  4 +-
 src/authentic2/manager/journal_event_types.py |  2 +
 src/authentic2/manager/journal_views.py       |  4 +-
 src/authentic2/manager/resources.py           |  2 +-
 src/authentic2/saml/saml2utils.py             |  4 +-
 8 files changed, 52 insertions(+), 50 deletions(-)
src/authentic2/a2_rbac/managers.py
24 24

  
25 25

  
26 26
class OrganizationalUnitManager(AbstractBaseManager):
27
    def get_by_natural_key(self, slug):
28
        return self.get(slug=slug)
27
    def get_by_natural_key(self, uuid):
28
        return self.get(slug=uuid)
29 29

  
30 30

  
31 31
class RoleManager(BaseRoleManager):
src/authentic2/api_views.py
142 142
    no_email_validation = serializers.BooleanField(required=False)
143 143
    return_url = serializers.URLField(required=False, allow_blank=True)
144 144

  
145
    def validate(self, data):
145
    def validate(self, attrs):
146 146
        request = self.context.get('request')
147
        ou = data.get('ou')
147
        ou = attrs.get('ou')
148 148
        if request:
149 149
            perm = 'custom_user.add_user'
150 150
            if ou:
151
                authorized = request.user.has_ou_perm(perm, data['ou'])
151
                authorized = request.user.has_ou_perm(perm, attrs['ou'])
152 152
            else:
153 153
                authorized = request.user.has_perm(perm)
154 154
            if not authorized:
......
156 156
        User = get_user_model()
157 157
        if ou:
158 158
            if app_settings.A2_EMAIL_IS_UNIQUE or app_settings.A2_REGISTRATION_EMAIL_IS_UNIQUE:
159
                if 'email' not in data:
159
                if 'email' not in attrs:
160 160
                    raise serializers.ValidationError(_('Email is required'))
161
                if User.objects.filter(email__iexact=data['email']).exists():
161
                if User.objects.filter(email__iexact=attrs['email']).exists():
162 162
                    raise serializers.ValidationError(_('Account already exists'))
163 163

  
164 164
            if ou.email_is_unique:
165
                if 'email' not in data:
165
                if 'email' not in attrs:
166 166
                    raise serializers.ValidationError(_('Email is required in this ou'))
167
                if User.objects.filter(ou=ou, email__iexact=data['email']).exists():
167
                if User.objects.filter(ou=ou, email__iexact=attrs['email']).exists():
168 168
                    raise serializers.ValidationError(_('Account already exists in this ou'))
169 169

  
170 170
            if app_settings.A2_USERNAME_IS_UNIQUE or app_settings.A2_REGISTRATION_USERNAME_IS_UNIQUE:
171
                if 'username' not in data:
171
                if 'username' not in attrs:
172 172
                    raise serializers.ValidationError(_('Username is required'))
173
                if User.objects.filter(username=data['username']).exists():
173
                if User.objects.filter(username=attrs['username']).exists():
174 174
                    raise serializers.ValidationError(_('Account already exists'))
175 175

  
176 176
            if ou.username_is_unique:
177
                if 'username' not in data:
177
                if 'username' not in attrs:
178 178
                    raise serializers.ValidationError(_('Username is required in this ou'))
179
                if User.objects.filter(ou=ou, username=data['username']).exists():
179
                if User.objects.filter(ou=ou, username=attrs['username']).exists():
180 180
                    raise serializers.ValidationError(_('Account already exists in this ou'))
181
        return data
181
        return attrs
182 182

  
183 183

  
184 184
class RpcMixin:
......
319 319
    old_password = serializers.CharField(required=True, allow_null=True)
320 320
    new_password = serializers.CharField(required=True, allow_null=True)
321 321

  
322
    def validate(self, data):
322
    def validate(self, attrs):
323 323
        User = get_user_model()
324
        qs = User.objects.filter(email__iexact=data['email'])
325
        if data['ou']:
326
            qs = qs.filter(ou=data['ou'])
324
        qs = User.objects.filter(email__iexact=attrs['email'])
325
        if attrs['ou']:
326
            qs = qs.filter(ou=attrs['ou'])
327 327
        try:
328 328
            self.user = qs.get()
329 329
        except User.DoesNotExist:
330 330
            raise serializers.ValidationError('no user found')
331 331
        except MultipleObjectsReturned:
332 332
            raise serializers.ValidationError('more than one user have this email')
333
        if not self.user.check_password(data['old_password']):
333
        if not self.user.check_password(attrs['old_password']):
334 334
            raise serializers.ValidationError('old_password is invalid')
335
        return data
335
        return attrs
336 336

  
337 337

  
338 338
class PasswordChange(BaseRpcView):
......
491 491
            instance.save()
492 492
        return instance
493 493

  
494
    def validate(self, data):
494
    def validate(self, attrs):
495 495
        User = get_user_model()
496 496
        qs = User.objects.all()
497 497

  
......
499 499

  
500 500
        if self.instance:
501 501
            ou = self.instance.ou
502
        if 'ou' in data and not ou:
503
            ou = data['ou']
502
        if 'ou' in attrs and not ou:
503
            ou = attrs['ou']
504 504

  
505 505
        get_or_create_fields = self.context['view'].request.GET.getlist('get_or_create')
506 506
        update_or_create_fields = self.context['view'].request.GET.getlist('update_or_create')
......
509 509
        if (
510 510
            'email' not in get_or_create_fields
511 511
            and 'email' not in update_or_create_fields
512
            and data.get('email')
513
            and (not self.instance or data.get('email') != self.instance.email)
512
            and attrs.get('email')
513
            and (not self.instance or attrs.get('email') != self.instance.email)
514 514
        ):
515
            if app_settings.A2_EMAIL_IS_UNIQUE and qs.filter(email__iexact=data['email']).exists():
515
            if app_settings.A2_EMAIL_IS_UNIQUE and qs.filter(email__iexact=attrs['email']).exists():
516 516
                already_used = True
517
            if ou and ou.email_is_unique and qs.filter(ou=ou, email__iexact=data['email']).exists():
517
            if ou and ou.email_is_unique and qs.filter(ou=ou, email__iexact=attrs['email']).exists():
518 518
                already_used = True
519 519

  
520 520
        errors = {}
521 521
        if already_used:
522 522
            errors['email'] = 'email already used'
523
        if data.get('password') and data.get('hashed_password'):
523
        if attrs.get('password') and attrs.get('hashed_password'):
524 524
            errors['password'] = 'conflict with provided hashed_password'
525
        if data.get('hashed_password'):
525
        if attrs.get('hashed_password'):
526 526
            try:
527
                hasher = identify_hasher(data.get('hashed_password'))
527
                hasher = identify_hasher(attrs.get('hashed_password'))
528 528
            except ValueError:
529 529
                errors['hashed_password'] = "unknown hash format"
530 530
            else:
531 531
                try:
532
                    hasher.safe_summary(data.get('hashed_password'))
532
                    hasher.safe_summary(attrs.get('hashed_password'))
533 533
                except Exception:
534 534
                    errors['hashed_password'] = "hash format error"
535 535
        if errors:
536 536
            raise serializers.ValidationError(errors)
537
        return data
537
        return attrs
538 538

  
539 539
    class Meta:
540 540
        model = get_user_model()
......
746 746
            return new_qs
747 747
        return qs
748 748

  
749
    def filter_queryset(self, qs):
750
        qs = super().filter_queryset(qs)
751
        qs = self.request.user.filter_by_perm(['custom_user.view_user'], qs)
749
    def filter_queryset(self, queryset):
750
        queryset = super().filter_queryset(queryset)
751
        queryset = self.request.user.filter_by_perm(['custom_user.view_user'], queryset)
752 752
        # filter users authorized for a specified service
753 753
        if 'service-slug' in self.request.GET:
754 754
            service_slug = self.request.GET['service-slug']
......
760 760
            )
761 761
            if service:
762 762
                if service.authorized_roles.all():
763
                    qs = qs.filter(roles__in=service.authorized_roles.children())
764
                    qs = qs.distinct()
763
                    queryset = queryset.filter(roles__in=service.authorized_roles.children())
764
                    queryset = queryset.distinct()
765 765
            else:
766
                qs = qs.none()
767
        return qs
766
                queryset = queryset.none()
767
        return queryset
768 768

  
769 769
    def update(self, request, *args, **kwargs):
770 770
        kwargs['partial'] = True
......
1147 1147

  
1148 1148

  
1149 1149
class ServiceOUField(serializers.ListField):
1150
    def to_internal_value(self, data_list):
1151
        data = data_list[0].split(' ')
1150
    def to_internal_value(self, data):
1151
        data = data[0].split(' ')
1152 1152
        if not len(data) == 2:
1153 1153
            raise ValidationError('This field should be a service slug and an OU slug separated by space.')
1154 1154
        return super().to_internal_value(data)
src/authentic2/custom_user/managers.py
186 186
    def create_superuser(self, username, email, password, **extra_fields):
187 187
        return self._create_user(username, email, password, True, True, **extra_fields)
188 188

  
189
    def get_by_natural_key(self, uuid):
190
        return self.get(uuid=uuid)
189
    def get_by_natural_key(self, username):
190
        return self.get(uuid=username)
src/authentic2/journal_event_types.py
26 26

  
27 27
class EventTypeWithService(EventTypeDefinition):
28 28
    @classmethod
29
    def record(cls, user=None, service=None, session=None, references=None, data=None):
29
    def record(cls, user=None, session=None, references=None, data=None, api=False, service=None):
30 30
        if service:
31 31
            if not data:
32 32
                data = {}
......
34 34
            if not references:
35 35
                references = []
36 36
            references = [service] + references
37
        super().record(user=user, session=session, references=references, data=data)
37
        super().record(user=user, session=session, references=references, data=data, api=api)
38 38

  
39 39
    @classmethod
40 40
    def get_service_name(cls, event):
src/authentic2/manager/journal_event_types.py
197 197
    label = _('user activation')
198 198

  
199 199
    @classmethod
200
    # pylint: disable=arguments-renamed
200 201
    def record(cls, target_user, user=None, session=None, origin=None, reason=None):
201 202
        data = {'origin': origin, 'reason': reason}
202 203
        super().record(user=user, session=session, references=[target_user], data=data)
......
225 226
    label = _('user deactivation')
226 227

  
227 228
    @classmethod
229
    # pylint: disable=arguments-renamed
228 230
    def record(cls, target_user, user=None, session=None, origin=None, reason=None):
229 231
        data = {'reason': reason, 'origin': origin}
230 232
        super().record(user=user, session=session, references=[target_user], data=data)
src/authentic2/manager/journal_views.py
70 70
                yield from super().lexem_queries('email:' + lexem)
71 71
                yield from super().lexem_queries('username:' + lexem)
72 72

  
73
    def unmatched_lexems_query(self, lexems):
74
        fullname = ' '.join(lexem.strip() for lexem in lexems if lexem.strip())
73
    def unmatched_lexems_query(self, unmatched_lexems):
74
        fullname = ' '.join(lexem.strip() for lexem in unmatched_lexems if lexem.strip())
75 75
        if fullname:
76 76
            users = User.objects.find_duplicates(fullname=fullname)
77 77
            return self.query_for_users(users)
src/authentic2/manager/resources.py
28 28
    def clean(self, value):
29 29
        raise NotImplementedError
30 30

  
31
    def render(self, value, object):
31
    def render(self, value, obj):
32 32
        return ', '.join(str(v) for v in value.all())
33 33

  
34 34

  
src/authentic2/saml/saml2utils.py
95 95
    def popNamespace(self):
96 96
        self.__ns = self.__old_ns.pop()
97 97

  
98
    def start(self, tag, attrib):
98
    def start(self, tag, attrs):
99 99
        tag = '{%s}%s' % (self.__ns, tag)
100 100
        self.__opened.append(tag)
101
        return etree.TreeBuilder.start(self, tag, attrib)
101
        return etree.TreeBuilder.start(self, tag, attrs)
102 102

  
103 103
    def simple_content(self, tag, data):
104 104
        self.start(tag, {})
105
-