1

I am developing a program that uses flask_login and am trying to set attributes to the flask_login.current_user that can persist through requests that are not stored in the database that the usermixin class is mapped to. One of the attributes I intend to set to current_user is a complex Class (controller) that holds many other classes as well as a connection to my Database. Therefore, it cannot be pickled and recollected. It also takes a long time to initialize and therefore would slow down my program a lot to keep reinitializing.

I am trying to do this:

@leagues.route('/admin-attend', methods=["POST"])
@login_required
def admin_attending():
    """
    When the admin opens up its league.
    """
    json_file = request.get_json()
    current_user.controller = Controller(json_file["league_id"])
    json_return["status"] = "success"
    return redirect('/session/')

And the access the Controller class that i set here:

@session_page.route('/', methods=['GET'])
@admin_required
def session():
    """
    Renders the session page, and loads in all of the data.
    """

    com = current_user.controller
    present_players, rounds, absent_players = getval(com)
    return render_template("session.html", players=present_players, 
                           absent=absent_players, 
                           rounds=rounds)

When I do this in development on my localhost, it works perfectly fine. I am able to access the same instance of the controller class throughout the entire program entirely through current_user. However, after deployment the same functionality causes an attribute error as the current_user doesn't have the attribute "controller". I understand the user_loader supposedly loads the user up from the database every request, which makes me very confused how this was able to work before? as I kind of just blindly trusted it.

My usermixin class is as follows:

class User(UserMixin):
    """ 
    This class is mapped to the user table in the database.
    As a client navigates the browser it tracks the admin priveleges.
    The users reciever and controller properties allow a client to remain 
    using the same reciever/controller while navigating without 
    reinitializing.
    """
    def __init__(self, email, password, session):
        self.email = email
        self.password = password
        self.session = session
        self.is_admin = False
        self.current_league = None
        self.reciever = None
        self.controller = None
        
    def get_id(self):
        """ 
        Gets the ID of the player.
        """
        return self.player_id

This is then mapped to a user table in the database which has the columns: email, player_id, password:

# Map User Class to User Table.
metadata = MetaData()
user_table = Table('user', metadata, autoload=True,autoload_with=engine)
mapper(User, user_table)

Here is my user_loader:

`# Loads the current_user.
@login_manager.user_loader
def load_user(id):
    return session.query(User).get(id)

`

Server running: ubuntu latest, python 3.9.12, wsgi takes "app" and does it's thing. Development running: windows10, python 3.10.9, does app.run(debug=True).

Can anyone give me insight into the issue I am facing here, or another way to hold an instance of my Controller class across my whole program that persists through requests?

Edit: Just to reiterate, my controller class is very large and has a connection object, therefore it is too big to store in a cookie and is not JSON serializable. Making it unable to be stored using flask's "session".

0 Answers0