From d578133bd1d539e229f25efedbec1efb85245686 Mon Sep 17 00:00:00 2001 From: Miguel Grinberg Date: Tue, 18 Jul 2017 07:55:26 -0700 Subject: [PATCH] Chapter 10: User profiles (10a) --- app/auth/views.py | 13 +++---- app/main/views.py | 9 ++++- app/models.py | 10 ++++++ app/templates/base.html | 3 ++ app/templates/user.html | 22 ++++++++++++ .../versions/d66f086b258_user_information.py | 34 +++++++++++++++++++ tests/test_user_model.py | 19 +++++++++++ 7 files changed, 103 insertions(+), 7 deletions(-) create mode 100644 app/templates/user.html create mode 100644 migrations/versions/d66f086b258_user_information.py diff --git a/app/auth/views.py b/app/auth/views.py index f4a8aa487..7ddd75ea2 100644 --- a/app/auth/views.py +++ b/app/auth/views.py @@ -11,12 +11,13 @@ @auth.before_app_request def before_request(): - if current_user.is_authenticated \ - and not current_user.confirmed \ - and request.endpoint \ - and request.blueprint != 'auth' \ - and request.endpoint != 'static': - return redirect(url_for('auth.unconfirmed')) + if current_user.is_authenticated: + current_user.ping() + if not current_user.confirmed \ + and request.endpoint \ + and request.blueprint != 'auth' \ + and request.endpoint != 'static': + return redirect(url_for('auth.unconfirmed')) @auth.route('/unconfirmed') diff --git a/app/main/views.py b/app/main/views.py index c8520dea6..607def452 100644 --- a/app/main/views.py +++ b/app/main/views.py @@ -1,7 +1,14 @@ -from flask import render_template +from flask import render_template, abort from . import main +from ..models import User @main.route('/') def index(): return render_template('index.html') + + +@main.route('/user/') +def user(username): + user = User.query.filter_by(username=username).first_or_404() + return render_template('user.html', user=user) diff --git a/app/models.py b/app/models.py index c00a213eb..58b719286 100644 --- a/app/models.py +++ b/app/models.py @@ -1,3 +1,4 @@ +from datetime import datetime from werkzeug.security import generate_password_hash, check_password_hash from itsdangerous import TimedJSONWebSignatureSerializer as Serializer from flask import current_app @@ -74,6 +75,11 @@ class User(UserMixin, db.Model): role_id = db.Column(db.Integer, db.ForeignKey('roles.id')) password_hash = db.Column(db.String(128)) confirmed = db.Column(db.Boolean, default=False) + name = db.Column(db.String(64)) + location = db.Column(db.String(64)) + about_me = db.Column(db.Text()) + member_since = db.Column(db.DateTime(), default=datetime.utcnow) + last_seen = db.Column(db.DateTime(), default=datetime.utcnow) def __init__(self, **kwargs): super(User, self).__init__(**kwargs) @@ -156,6 +162,10 @@ def can(self, perm): def is_administrator(self): return self.can(Permission.ADMIN) + def ping(self): + self.last_seen = datetime.utcnow() + db.session.add(self) + def __repr__(self): return '' % self.username diff --git a/app/templates/base.html b/app/templates/base.html index 1ab3e54cd..3d32dabc6 100644 --- a/app/templates/base.html +++ b/app/templates/base.html @@ -23,6 +23,9 @@