Application Layout with Groups of single items in Python
Question: I am coming from the PHP world, worked a lot with Joomla. Now I am building a
Python web application and I my first step is to create a user management when
I faced a very basic question: How do you manage all users in one class, when
your model represents only a single database row.

An example will make this more clear. Here's what Joomla did:

    
    
    JUser::getInstance($userid)
    

What this did was there is the class `JUser` and it has a static method
`getInstance()` that operated on a static variable that was valid among all
instances. In this way, `JUser` managed all Users while only representing
_one_ user as an instance of the class.

Now on Python I use SQLAlchemy and have a class like this:

    
    
    class User(Base):
    

... and so on. Before I now start doing crappy stuff coming over from a crappy
PHP world, I wanted to know: What is the correct and clean approach here?
Build a new `Users` class that holds all users? Or is this static-method
approach a good concept?

I have been googling for some time but I can't seem to find something on this
topic: Building larger applications in Python with the MV(C) model. Anyone
have any good links on this I could read?

In a more specifc way: How do I solve this problem?


Answer: You should create one model class, just like Joomla did:

    
    
    class User(Base):
        ...
    

And then you can use generic functionality of SQLAlchemy, and query for user
you need,

using primary key:

    
    
    user = session.query(User).get(user_id)
    

or using unique username/email:

    
    
    user = session.query(User).filter(User.username==my_username).first()
    

Speaking of user which is logged in for current HTTP request, web frameworks
usually give you an instance automatically, passing it to HTTP request handler
you're implementing. For example, Django framework gives you user instance
from it's own ORM out of the box, as it has all authentication and session
functionality in it. I also use Flask microframework with Flask-login module
and SQLAlchemy, and there you need to implement a callback, which loads user
instance by user Id, like I said before:

    
    
    @login_manager.user_loader
    def load_user(userid):
        # just query for user
        return User.query.get(userid) # User.query is Flask-SQLAlchemy extension, it's same to session.query(User).get(userid)
    

Then you can get your user instance from a thread-local variable:

    
    
    from flask.ext.login import login_required, current_user
    
    @app.route('/hello/')
    @login_required
    def my_request_handler():
        return "<html><body>Current user is %s</body></html>" % current_user.username
    

Of course you can implement a static method, if you need a shortcut for
getting user instance by Id:

    
    
    class User(Base):
        ...
    
        @classmethod
        def by_id(cls, id):
            return session.query(cls).get(id)
    

(where SQLAlchemy's session is somehow defined globally)

For Python/web apps architecture, you can look at
[Django](https://djangoproject.com) — it's very popular and easy to use all-
in-one framework, and it has MVC-like architecture in it. Just [read the
tutorial](https://docs.djangoproject.com/en/1.4/intro/tutorial01/).

(But note that Django's own ORM's functionality is very limited in comparison
to SQLAlchemy, if you'll wish to use Django).

