Skip to content

Commit 0beb0b8

Browse files
committed
Implement created_on & last_login fields on User object.
1 parent 5d33376 commit 0beb0b8

File tree

11 files changed

+40
-931
lines changed

11 files changed

+40
-931
lines changed

flask_login_tutorial/forms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class SignupForm(FlaskForm):
2121
"Password",
2222
validators=[
2323
DataRequired(),
24-
Length(min=6, message="Select a stronger password."),
24+
Length(min=6, message="Select a stronger password (at least 6 characters)."),
2525
],
2626
)
2727
confirm = PasswordField(

flask_login_tutorial/models.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ class User(UserMixin, db.Model):
1212
__tablename__ = "flasklogin-users"
1313

1414
id = db.Column(db.Integer, primary_key=True)
15-
name = db.Column(db.String(100), nullable=False, unique=False)
16-
email = db.Column(db.String(40), unique=True, nullable=False)
17-
password = db.Column(db.String(200), primary_key=False, unique=False, nullable=False)
18-
website = db.Column(db.String(60), index=False, unique=False, nullable=True)
19-
created_on = db.Column(db.DateTime, index=False, unique=False, nullable=True)
20-
last_login = db.Column(db.DateTime, index=False, unique=False, nullable=True)
15+
name = db.Column(db.String(200), nullable=False, unique=False)
16+
email = db.Column(db.String(100), unique=True, nullable=False)
17+
password = db.Column(db.String(200), unique=False, nullable=False)
18+
website = db.Column(db.String(100), nullable=True)
19+
created_on = db.Column(db.DateTime, server_default=db.func.now())
20+
last_login = db.Column(db.DateTime)
2121

2222
def set_password(self, password):
2323
"""Create hashed password."""

flask_login_tutorial/routes.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from flask import Blueprint, redirect, render_template, url_for
44
from flask_login import current_user, login_required, logout_user
55

6+
from .models import db
7+
68
# Blueprint Configuration
79
main_blueprint = Blueprint("main_blueprint", __name__, template_folder="templates", static_folder="static")
810

@@ -11,6 +13,8 @@
1113
@login_required
1214
def dashboard():
1315
"""Logged-in User Dashboard."""
16+
current_user.last_login = db.func.now()
17+
db.session.commit()
1418
return render_template(
1519
"dashboard.jinja2",
1620
title="Flask-Login Tutorial",

flask_login_tutorial/static/src/less/account.less

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
padding: 0;
7070
background-color: transparent;
7171
border: 0;
72-
-webkit-appearance: none;
72+
appearance: none;
7373
float: right;
7474
font-size: 1.5rem;
7575
font-weight: 700;

flask_login_tutorial/static/src/less/dashboard.less

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,24 @@ body {
2626
line-height: 1;
2727
}
2828

29-
p {
30-
font-size: 1.5em;
31-
font-weight: 300;
32-
text-shadow: 0 0 3px #507b89;
29+
.user-details {
30+
width: 600px;
31+
text-align: left;
32+
33+
p {
34+
font-size: 1em;
35+
font-weight: 300;
36+
text-shadow: 0 0 3px #507b89;
37+
line-height: 1;
38+
margin: auto;
39+
40+
a {
41+
display: inline;
42+
}
43+
}
3344
}
3445

46+
3547
a {
3648
font-size: 1em;
3749
font-weight: 500;
@@ -42,7 +54,7 @@ body {
4254
opacity: .7;
4355
transition: @transition;
4456

45-
&:hover{
57+
&:hover {
4658
cursor: pointer;
4759
opacity: 1;
4860
}

flask_login_tutorial/templates/analytics.jinja2

Lines changed: 0 additions & 14 deletions
This file was deleted.

flask_login_tutorial/templates/dashboard.jinja2

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,15 @@
99
<h1>{{ body }}</h1>
1010

1111
{% if current_user.is_authenticated %}
12-
<p>Hi {{ current_user.name }}!</p>
12+
<div class="user-details">
13+
<p>Hi {{ current_user.name }}!</p>
14+
<p><b>Email:</b> {{ current_user.email }}</p>
15+
<p><b>Created on:</b> {{ current_user.created_on.strftime("%B %d, %Y") }}</p>
16+
<p><b>Last seen:</b> {{ current_user.last_login.strftime("%B %d, %Y (%r)" ) }}</p>
17+
{% if current_user.website %}
18+
<p><b>Website:</b> <a href="{{ current_user.website }}" target="_blank" rel="noopener">{{ current_user.website }}</a></p>
19+
{% endif %}
20+
</div>
1321
<a href="{{ url_for('main_blueprint.logout') }}">Log out</a>
1422
{% endif %}
1523

flask_login_tutorial/templates/layout.jinja2

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
</div>
3636
<script src="{{ url_for('static', filename='dist/js/main.min.js') }}"></script>
3737
{% block additionalscripts %}{% endblock %}
38-
{% include 'analytics.jinja2' %}
3938
</body>
4039

4140
</html>

gunicorn.conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
reload = True
2222
workers = 1
2323
threads = 1
24-
bind = ["127.0.0.1:8000"]
24+
bind = ["127.0.0.1:8001"]
2525
elif ENVIRONMENT == "production":
2626
daemon = True
2727
accesslog = "/var/log/flasklogin/access.log"

0 commit comments

Comments
 (0)