[Django] #31367: ImportError error while importing class from auth/mixin to auth/view

79 views
Skip to first unread message

Django

unread,
Mar 16, 2020, 9:07:23 AM3/16/20
to django-...@googlegroups.com
#31367: ImportError error while importing class from auth/mixin to auth/view
------------------------------------------------+------------------------
Reporter: Mehmet İnce | Owner: nobody
Type: Cleanup/optimization | Status: new
Component: contrib.auth | Version: 3.0
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
------------------------------------------------+------------------------
While I was doing some tests about discussion related to the
[https://groups.google.com/forum/#!topic/django-developers/PUQQUHIxEXQ] ,
I've realized that it's not possible to import class from
django.contrib.auth.mixins to the django.contrib.auth.views .

**Reproduce**

Add following mixin to the auth/mixins.py
{{{
class LoginRequiredExemptMixin(object):
login_required_exempt = True
}}}

And import it at auth/views.py and use it on one of the CBV. For instance
LoginView.

{{{
from django.contrib.auth.mixins import LoginRequiredExemptMixin

class LoginView(SuccessURLAllowedHostsMixin, FormView,
LoginRequiredExemptMixin):
"""
Display the login form and handle the login action.
"""
}}}

In that case, you will be seeing following ImportError


{{{
File "/Users/xxx/PycharmProjects/django/django/contrib/auth/mixins.py",
line 4, in <module>
from django.contrib.auth.views import redirect_to_login
File "/Users/xxx/PycharmProjects/django/django/contrib/auth/views.py",
line 13, in <module>
from django.contrib.auth.mixins import LoginRequiredExemptMixin
ImportError: cannot import name 'LoginRequiredExemptMixin' from
'django.contrib.auth.mixins'
(/Users/mince/PycharmProjects/django/django/contrib/auth/mixins.py)
}}}

**How To Fix**

The reason of the error is mixins.py file have "django.contrib.auth.views
import redirect_to_login" on line 4. Since the redirect_to_login is only
used one place (handle_no_permission function) in mixin.py we can remove
the import from top of the file and call it right before usage of the
imported function. For example as follow.

{{{
def handle_no_permission(self):
if self.raise_exception or self.request.user.is_authenticated:
raise PermissionDenied(self.get_permission_denied_message())
from django.contrib.auth.views import redirect_to_login
return redirect_to_login(self.request.get_full_path(),
self.get_login_url(), self.get_redirect_field_name())


}}}

On the other hand, I think why we have **SuccessURLAllowedHostsMixin**
class in auth/views.py file even though it's been called Mixin was related
that import code in mixin.py too.

By doing this changes we can even move **SuccessURLAllowedHostsMixin**
class to the mixins.py file since it's only being used in auth/views.py.
That little changes gives us flexibility to import mixings in the feature.

--
Ticket URL: <https://code.djangoproject.com/ticket/31367>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 16, 2020, 9:34:46 AM3/16/20
to django-...@googlegroups.com
#31367: ImportError error while importing class from auth/mixin to auth/view
-------------------------------------+-------------------------------------

Reporter: Mehmet İnce | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: contrib.auth | Version: master
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mehmet İnce):

* has_patch: 0 => 1
* version: 3.0 => master


Comment:

https://github.com/django/django/pull/12577/

--
Ticket URL: <https://code.djangoproject.com/ticket/31367#comment:1>

Django

unread,
Mar 16, 2020, 9:53:02 AM3/16/20
to django-...@googlegroups.com
#31367: ImportError error while importing class from auth/mixin to auth/view.
-------------------------------------+-------------------------------------

Reporter: Mehmet İnce | Owner: nobody
Type: | Status: closed

Cleanup/optimization |
Component: contrib.auth | Version: master
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* status: new => closed
* resolution: => wontfix


Comment:

Thanks for this ticket, however `django.contrib.auth.mixins` contains
[https://docs.djangoproject.com/en/3.0/topics/auth/default/ documented]
mixins that can be used with CBVs. `SuccessURLAllowedHostsMixin` is not
one of them, it's an internal mixin specific for login/logout views, so
currently there is no need to import anything from
`django.contrib.auth.mixins` in the `django.contrib.auth.views`.

--
Ticket URL: <https://code.djangoproject.com/ticket/31367#comment:2>

Reply all
Reply to author
Forward
0 new messages