Walter Mantovani
commited on
Commit
·
0e78cd8
1
Parent(s):
b2ad130
uod
Browse files
app.py
CHANGED
@@ -17,9 +17,9 @@ class Utente(db.Model):
|
|
17 |
nickname = db.Column(db.String(80), unique=True, nullable=False)
|
18 |
username = db.Column(db.String(80), unique=True, nullable=False)
|
19 |
password = db.Column(db.String(30), nullable=False)
|
20 |
-
# messaggi = db.relationship('Messaggio', backref=db.backref('
|
21 |
# --- OR ---
|
22 |
-
messaggi = db.relationship('Messaggio', back_populates='
|
23 |
|
24 |
class Messaggio(db.Model):
|
25 |
__tablename__ = 'messaggio'
|
@@ -27,16 +27,16 @@ class Messaggio(db.Model):
|
|
27 |
user_id = db.Column(db.Integer, db.ForeignKey('utente.id'), nullable=False)
|
28 |
messaggio = db.Column(db.Text, nullable=False)
|
29 |
timestamp = db.Column(db.DateTime, default=db.func.now(), nullable=False)
|
30 |
-
#
|
31 |
# --- OR ---
|
32 |
-
|
33 |
|
34 |
|
35 |
@app.route('/')
|
36 |
def home():
|
37 |
if 'user_id' in session:
|
38 |
-
|
39 |
-
return render_template('home.html', user=
|
40 |
return render_template('home.html')
|
41 |
|
42 |
|
@@ -62,7 +62,7 @@ def api_guestbook():
|
|
62 |
|
63 |
messages = Messaggio.query.order_by(Messaggio.timestamp.desc()).all()
|
64 |
response = [
|
65 |
-
{'nickname': message.
|
66 |
for message in messages
|
67 |
]
|
68 |
return jsonify(response), 200
|
@@ -94,9 +94,9 @@ def login():
|
|
94 |
if request.method == 'POST':
|
95 |
username = request.form['username']
|
96 |
password = request.form['password']
|
97 |
-
|
98 |
-
if
|
99 |
-
session['user_id'] =
|
100 |
flash('Login riuscito!')
|
101 |
return redirect(url_for('guestbook'))
|
102 |
else:
|
|
|
17 |
nickname = db.Column(db.String(80), unique=True, nullable=False)
|
18 |
username = db.Column(db.String(80), unique=True, nullable=False)
|
19 |
password = db.Column(db.String(30), nullable=False)
|
20 |
+
# messaggi = db.relationship('Messaggio', backref=db.backref('utente', lazy='dynamic'))
|
21 |
# --- OR ---
|
22 |
+
messaggi = db.relationship('Messaggio', back_populates='utente', lazy='dynamic')
|
23 |
|
24 |
class Messaggio(db.Model):
|
25 |
__tablename__ = 'messaggio'
|
|
|
27 |
user_id = db.Column(db.Integer, db.ForeignKey('utente.id'), nullable=False)
|
28 |
messaggio = db.Column(db.Text, nullable=False)
|
29 |
timestamp = db.Column(db.DateTime, default=db.func.now(), nullable=False)
|
30 |
+
# utente = db.relationship('Utente', backref=db.backref('messaggi', lazy='dynamic'))
|
31 |
# --- OR ---
|
32 |
+
utente = db.relationship('Utente', back_populates='messaggi')
|
33 |
|
34 |
|
35 |
@app.route('/')
|
36 |
def home():
|
37 |
if 'user_id' in session:
|
38 |
+
utente = db.session.query(Utente).get(session['user_id'])
|
39 |
+
return render_template('home.html', user=utente)
|
40 |
return render_template('home.html')
|
41 |
|
42 |
|
|
|
62 |
|
63 |
messages = Messaggio.query.order_by(Messaggio.timestamp.desc()).all()
|
64 |
response = [
|
65 |
+
{'nickname': message.utente.nickname, 'messaggio': message.messaggio}
|
66 |
for message in messages
|
67 |
]
|
68 |
return jsonify(response), 200
|
|
|
94 |
if request.method == 'POST':
|
95 |
username = request.form['username']
|
96 |
password = request.form['password']
|
97 |
+
utente = Utente.query.filter_by(username=username, password=password).first()
|
98 |
+
if utente:
|
99 |
+
session['user_id'] = utente.id
|
100 |
flash('Login riuscito!')
|
101 |
return redirect(url_for('guestbook'))
|
102 |
else:
|