From 75c2873ff331f142e3b05451e02380169967c7db Mon Sep 17 00:00:00 2001 From: Andros Fenollosa Date: Sun, 18 Sep 2016 13:03:57 +0200 Subject: [PATCH] Fix multiuser --- README.org | 4 +++- src/app.py | 11 ++++++----- src/database.py | 4 +++- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/README.org b/README.org index dfeeeb8..2861eb2 100644 --- a/README.org +++ b/README.org @@ -102,10 +102,12 @@ class Note(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100), unique=True) text = db.Column(db.Text()) + user_id = db.Column(db.Integer) - def __init__(self, title, text): + def __init__(self, title, text, user_id): self.title = title self.text = text + self.user_id = user_id def __repr__(self): return ''.format(title=self.title) diff --git a/src/app.py b/src/app.py index 38d46bc..9305550 100644 --- a/src/app.py +++ b/src/app.py @@ -6,10 +6,9 @@ from database import db, User, Note app = Flask(__name__) -# Decoration: check login in session - def login_required(f): + # Decoration: check login in session @wraps(f) def decorated_function(*args, **kwargs): if 'user_id' not in session: @@ -64,7 +63,8 @@ def dashboard(my_param_note=None): my_notes = my_param_note if not my_notes: # Nothing found - my_notes = Note.query.order_by(Note.id.desc()).all() + my_notes = Note.query.filter_by( + user_id=session['user_id']).order_by(Note.id.desc()).all() if my_notes: # Show first result my_main_note = my_notes[0] @@ -92,7 +92,7 @@ def search(): return dashboard(Note.query.filter( or_(Note.title.like('%' + q + '%'), Note.text.like('%' + q + '%') - )).all()) + )).filter_by(user_id=session['user_id']).order_by(Note.id.desc()).all()) @app.route('/new') @@ -104,7 +104,8 @@ def new(): @app.route('/new/save', methods=['POST']) @login_required def save_note(): - myNote = Note(request.form['title'], request.form['text']) + myNote = Note(request.form['title'], request.form[ + 'text'], session['user_id']) # Create db.session.add(myNote) db.session.commit() diff --git a/src/database.py b/src/database.py index fdefc5c..d2f48a3 100644 --- a/src/database.py +++ b/src/database.py @@ -23,10 +23,12 @@ class Note(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100), unique=True) text = db.Column(db.Text()) + user_id = db.Column(db.Integer) - def __init__(self, title, text): + def __init__(self, title, text, user_id): self.title = title self.text = text + self.user_id = user_id def __repr__(self): return ''.format(title=self.title)