(get_response)
return middleware
Tasks run for authenticated users.
Tasks run for authenticated users.
Tasks run for authenticated users .
def user_tasks(get_response):
    """
    Tasks run for authenticated users.
    """

    def middleware(request):

        user, session = request.user, request.session

        # Views for anonymous users are not analzed further.
        if user.is_anonymous:
            return get_response(request)

        # Banned and suspended will be logged out.
        if auth.is_suspended(user=user):
            messages.error(request, f"Account is {user.profile.get_state_display()}")
            logout(request)

        update_status(user=user)

        # Find out the time since the last visit.
        elapsed = (now() - user.profile.last_login).total_seconds()

        # Update information since the last visit.
        if elapsed > settings.SESSION_UPDATE_SECONDS:

            # Detect user location if not set in the profile.
            ip = helpers.get_ip(request)

            # Detect user location if not set in the profile.
            if not user.profile.location:
                detect_location.spool(ip=ip, user_id=user.id)

            # Set the last login time.
            Profile.objects.filter(user=user).update(last_login=now())

            # Compute latest counts.
            counts = auth.get_counts(user=user)

            # Set the session.
            request.session[settings.SESSION_COUNT_KEY] = counts

            # Trigger award generation.
            tasks.create_user_awards.spool(user_id=user.id)

        # Can process response here after its been handled by the view
        response = get_response(request)

        return response

    return middleware
def user_tasks ( get_response ) : def middleware ( request ) : user , session = request . user , request . session # Views for anonymous users are not analzed further. if user . is_anonymous : return get_response ( request ) # Banned and suspended will be logged out. if auth . is_suspended ( user = user ) : messages . error ( request , f"Account is {user.profile.get_state_display()}" ) logout ( request ) update_status ( user = user ) # Find out the time since the last visit. elapsed = ( now ( ) - user . profile . last_login ) . total_seconds ( ) # Update information since the last visit. if elapsed > settings . SESSION_UPDATE_SECONDS : # Detect user location if not set in the profile. ip = helpers . get_ip ( request ) # Detect user location if not set in the profile. if not user . profile . location : detect_location . spool ( ip = ip , user_id = user . id ) # Set the last login time. Profile . objects . filter ( user = user ) . update ( last_login = now ( ) ) # Compute latest counts. counts = auth . get_counts ( user = user ) # Set the session. request . session [ settings . SESSION_COUNT_KEY ] = counts # Trigger award generation. tasks . create_user_awards . spool ( user_id = user . id ) # Can process response here after its been handled by the view response = get_response ( request ) return response return middleware