Opened 7 years ago

Closed 7 years ago

#28706 closed Cleanup/optimization (fixed)

Allow easier reuse of AuthenticationForm invalid_login ValidationError

Reported by: Jon Dufresne Owned by: nobody
Component: contrib.auth Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

The AuthenticationForm contains the snippet in the clean method:

    def clean(self):
        username = self.cleaned_data.get('username')
        password = self.cleaned_data.get('password')

        if ...:
            ...
            if ... is None:
                raise forms.ValidationError(
                    self.error_messages['invalid_login'],
                    code='invalid_login',
                    params={'username': self.username_field.verbose_name},
                )
        ...

In my project, I have overridden AuthenticationForm with customizations. Among these customizations, I have other reasons for raising an invalid_login. Rather than copy round the:

                raise forms.ValidationError(
                    self.error_messages['invalid_login'],
                    code='invalid_login',
                    params={'username': self.username_field.verbose_name},
                )

I suggest the form wrap this in a small function for reuse.

I'm not sure if this requires documentation or can remain an internal implementation detail. I'm happy to also add documentation if it is a good idea.

Change History (3)

comment:1 by Jon Dufresne, 7 years ago

Has patch: set

comment:2 by Claude Paroz, 7 years ago

Triage Stage: UnreviewedReady for checkin
Type: New featureCleanup/optimization

Looks good, I don't think documentation is required here.

comment:3 by Tim Graham <timograham@…>, 7 years ago

Resolution: fixed
Status: newclosed

In 6ed347d:

Fixed #28706 -- Moved AuthenticationFormn invalid login ValidationError to a method for reuse.

Note: See TracTickets for help on using tickets.
Back to Top