Skip to content

Commit

Permalink
Chapter 12: Self-followers (12e)
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Nov 11, 2022
1 parent 9407002 commit b2a8f93
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
9 changes: 9 additions & 0 deletions app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ class User(UserMixin, db.Model):
lazy='dynamic',
cascade='all, delete-orphan')

@staticmethod
def add_self_follows():
for user in User.query.all():
if not user.is_following(user):
user.follow(user)
db.session.add(user)
db.session.commit()

def __init__(self, **kwargs):
super(User, self).__init__(**kwargs)
if self.role is None:
Expand All @@ -114,6 +122,7 @@ def __init__(self, **kwargs):
self.role = Role.query.filter_by(default=True).first()
if self.email is not None and self.avatar_hash is None:
self.avatar_hash = self.gravatar_hash()
self.follow(self)

@property
def password(self):
Expand Down
2 changes: 2 additions & 0 deletions app/templates/followers.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ <h1>{{ title }} {{ user.username }}</h1>
<table class="table table-hover followers">
<thead><tr><th>User</th><th>Since</th></tr></thead>
{% for follow in follows %}
{% if follow.user != user %}
<tr>
<td>
<a href="{{ url_for('.user', username = follow.user.username) }}">
Expand All @@ -19,6 +20,7 @@ <h1>{{ title }} {{ user.username }}</h1>
</td>
<td>{{ moment(follow.timestamp).format('L') }}</td>
</tr>
{% endif %}
{% endfor %}
</table>
<div class="pagination">
Expand Down
4 changes: 2 additions & 2 deletions app/templates/user.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ <h1>{{ user.username }}</h1>
<a href="{{ url_for('.unfollow', username=user.username) }}" class="btn btn-default">Unfollow</a>
{% endif %}
{% endif %}
<a href="{{ url_for('.followers', username=user.username) }}">Followers: <span class="badge">{{ user.followers.count() }}</span></a>
<a href="{{ url_for('.followed_by', username=user.username) }}">Following: <span class="badge">{{ user.followed.count() }}</span></a>
<a href="{{ url_for('.followers', username=user.username) }}">Followers: <span class="badge">{{ user.followers.count() - 1 }}</span></a>
<a href="{{ url_for('.followed_by', username=user.username) }}">Following: <span class="badge">{{ user.followed.count() - 1 }}</span></a>
{% if current_user.is_authenticated and user != current_user and user.is_following(current_user) %}
| <span class="label label-default">Follows you</span>
{% endif %}
Expand Down
12 changes: 6 additions & 6 deletions tests/test_user_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ def test_follows(self):
self.assertTrue(u1.is_following(u2))
self.assertFalse(u1.is_followed_by(u2))
self.assertTrue(u2.is_followed_by(u1))
self.assertTrue(u1.followed.count() == 1)
self.assertTrue(u2.followers.count() == 1)
self.assertTrue(u1.followed.count() == 2)
self.assertTrue(u2.followers.count() == 2)
f = u1.followed.all()[-1]
self.assertTrue(f.followed == u2)
self.assertTrue(timestamp_before <= f.timestamp <= timestamp_after)
Expand All @@ -196,13 +196,13 @@ def test_follows(self):
u1.unfollow(u2)
db.session.add(u1)
db.session.commit()
self.assertTrue(u1.followed.count() == 0)
self.assertTrue(u2.followers.count() == 0)
self.assertTrue(Follow.query.count() == 0)
self.assertTrue(u1.followed.count() == 1)
self.assertTrue(u2.followers.count() == 1)
self.assertTrue(Follow.query.count() == 2)
u2.follow(u1)
db.session.add(u1)
db.session.add(u2)
db.session.commit()
db.session.delete(u2)
db.session.commit()
self.assertTrue(Follow.query.count() == 0)
self.assertTrue(Follow.query.count() == 1)

0 comments on commit b2a8f93

Please sign in to comment.