'Error in the report on whether the user is online or offline via session in flask

I would need advice on reporting on users whether it is online or offline, it will mainly be used by the admin on the page for such a quick overview. I'm doing it in vannila flask so jinja2. I just came across a problem with all the selected users in the table showing me that they are offline when they are logged in, would anyone advise me?

Flask Code:

@app.route("/users_for_teacher")
@isLoggedIn
def users_for_teacher():
    if session["group_id"] != Group.UCITEL:
        abort(401, description="Nemáš dostatečné oprávnění")

    connection = mysql.connection
    cursor = connection.cursor(prepared=True)
    
    cursor.execute("SELECT * FROM useraccounts WHERE school=%s AND class=%s;", (session["school"], session["class"],))
    users = [
        dict(id=row[0], username=row[1], classname=row[7], year=row[9], full_name=row[11])
        for row in cursor.fetchall()
    ]

    for user in users:
        if user in session["logged"] == True:
            user=session["logged"] = "True"
        else:
            user=session["logged"] = "False"

    cursor.close()
    connection.close()

    return render_template("profile/users_for_teacher.html", users=users, classname=session["class"], user=user)

user_for_teacher.html code:

<table id="usersTable" class="react-to-dark">
        <thead>
            <tr class="react-to-dark myprofile-tr">
                <th class="react-to-dark myprofile-th">Uživatelské Jméno</th>
                <th class="react-to-dark myprofile-th">Jméno</th>
                <th class="react-to-dark myprofile-th">Třída</th>
                <th class="react-to-dark myprofile-th">Ročník</th>
                <th class="react-to-dark myprofile-th">Heslo</th>
                <th class="react-to-dark myprofile-th">Aktivita</th>
            </tr>
        </thead>
        <tbody id="users">
            {% for user in users %}
            <tr class="react-to-dark myprofile-tr users-tr">
                <td class="react-to-dark myprofile-td"><span class="searchable">{{user["username"]}}</span></td>
                <td class="react-to-dark myprofile-td"><span class="searchable">{{user["full_name"]}}</span></td>
                <td class="react-to-dark myprofile-td"><span class="searchable">{{user["classname"]}}</span></td>
                <td class="react-to-dark myprofile-td"><span class="searchable">{{user["year"]}}</span></td>
                <td class="react-to-dark myprofile-td"><a style="text-decoration: none"
                        href="reset/{{user['username']}}">Resetovat heslo</a></td>
                <td class="react-to-dark myprofile-td">
                    {% if "user" in session["logged"] == "True" %}
                    <i title="Je aktivní" style="color: green;" class="bi bi-circle-fill"></i>
                    {% else %}
                    <i title="Není aktivní" style="color: red;" class="bi bi-circle-fill"></i>
                    {% endif %}
                </td>
            </tr>
            {% endfor %}
        </tbody>
    </table>

An example of what the table looks like are the users that I sort already when selecting from the database are selected for the school they go to and for the class:

Sample of problem



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source