Best way to keep a list of custom objects unique for every user in a small Flask app?

时间:2017-08-04 12:15:21

标签: python flask web-applications

I'm writing a app in flask where users fill in a form and based on that the number of objects (of a custom "Event" class) are created and stored in a list. After that, some operations on the list can be performed. Right now my code looks approximately like this:

from flask import Flask, request, render_template
app = Flask(__name__)
eventsList = []
@app.route('/')
def my_form():

...

if __name__ == '__main__':
    app.run(host= '0.0.0.0', port="8080")

However, I want the eventsList=[] to be unique for every guest on the site, so e.g. when two people launch my app on a different browsers or computers, they can use it simultaneously and their lists won't mix up. Right now, when I add a Event to it, I can see it on any other device as well. It's meant to be a small application, and there is no need to store any info once a guest closes their browser, so using a database for this problem feels like too much of a trouble, also it's unnecessary to have any logging sessions here. Storing the whole eventsList in client's cookie is also not an option, since it can be just to big and surpass the size limit. What would be the easiest and most effective way of doing such a thing?

1 个答案:

答案 0 :(得分:0)

I would store a unique user identifier (generated randomly, probably) in a cookie, keep a dict mapping the identifier to a User class instance, and make the event list a variable of each User instance.