How to check the first django user account in the admin panel?

I am working on a django project and I am using the default authentication application for authentication.
I know that in the user model there is a field last_loginthat stores the time the user last logged in.

When a user user logs in for the first time in the admin panel, I want to check if there is a field last_loginand redirect it to the page with the password for changes.

Where should I put this check?


What I have tried so far:

I tried using a custom login form and overriding the default method confirm_login_allowed, but it looks like I can only raise a validation error to block a login attempt using these.

I also tried using django.contrib.auth.signals.user_logged_inSignal, but that also prevents me from returning a redirect response when last_loginthere is None.

I want to know how I can return a redirect response after user authentication.

+4
source share
3 answers

Django admin is not configurable. You have to connect to his inner representations. The input / output views are inside the Django class AdminSite(the one you usually refer to admin.site). My implementation is a bit hacky but small:

Paste at the top urls.py

from django.contrib import admin
from django.contrib.admin import AdminSite
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect

class MyAdminSite(AdminSite):
    def login(self, request, extra_context=None):
        new_user = False
        user = None

        username = request.POST.get('username')  # Hack to find user before its last_login set to now.
        if username:
            user = User.objects.filter(username=username).first()
            if user:
                new_user = user.last_login is None

        r = super(MyAdminSite, self).login(request, extra_context)

        if new_user and request.user == user and isinstance(r, HttpResponseRedirect):  # Successful logins will result in a redirect.
            return HttpResponseRedirect(reverse('admin:password_change'))
        return r

admin.site = MyAdminSite()

, , last_login, request.user request.POST.

AdminSite.login django.contrib.auth.views.login, , Django.

+3

Django AdminSite login_form, .

admin.py

class MyAdminSite(AdminSite):
    login_form = CustomAdminLoginForm

admin_site = MyAdminSite(name='myadmin')
admin_site.register(User)
admin_site.register(Group

urls.py

Django

from app.admin import admin_site

url(r'^admin/', admin_site.urls)

forms.py

AuthenticationForm confirm_login_allowed, .

class CustomAdminLoginForm(AuthenticationForm):
    def confirm_login_allowed(self, user):
        if user.last_login:
            raise ValidationError(mark_safe('Hey first time user please reset your password here... <a href="/test">test</a>'), code='inactive')

: .

  • , .? last_long None. date_joined . last_login == date_joined
  • , ?

Edit:

config_login_allowed ...?

from django.contrib.auth.signals import user_logged_in


def change_password_first_time(sender, user, request, **kwargs):
    # Your business logic here...

user_logged_in.connect(change_password_first_time)
+3

. . , , , .

:

if user.last_login is None:
   auth.login(request,user)
   return redirect(alertPassword) 

# when user is login for the first time it will pass a value

else:
   auth.login(request,user)
   return redirect(moveToLogin)

def moveToLogin(request):
   return render(request,"home.html")

def alertPassword(request):
   first_login=True
   return render(request,"home.html",{"first_login":first_login})
#it will pass a value to the template called first login is True

:

{% if first_login==True%}
# Put anything you want to be done 
# In my case I wanted to open a new window by using script.
<script>

    window.open("popUpWindow")

</script>
{%endif%}
0
source

Source: https://habr.com/ru/post/1690832/


All Articles