Browse Source

Chapter 12: Self-followers (12e)

master
T. Meissner 6 years ago
parent
commit
639c61fc77
4 changed files with 19 additions and 8 deletions
  1. +9
    -0
      app/models.py
  2. +2
    -0
      app/templates/followers.html
  3. +2
    -2
      app/templates/user.html
  4. +6
    -6
      tests/test_user_model.py

+ 9
- 0
app/models.py View File

@ -115,6 +115,7 @@ class User(UserMixin, db.Model):
self.role = Role.query.filter_by(default=True).first() self.role = Role.query.filter_by(default=True).first()
if self.email is not None and self.avatar_hash is None: if self.email is not None and self.avatar_hash is None:
self.avatar_hash = self.gravatar_hash() self.avatar_hash = self.gravatar_hash()
self.follow(self)
@property @property
def password(self): def password(self):
@ -231,6 +232,14 @@ class User(UserMixin, db.Model):
return Post.query.join(Follow, Follow.followed_id == Post.author_id) \ return Post.query.join(Follow, Follow.followed_id == Post.author_id) \
.filter(Follow.follower_id == self.id) .filter(Follow.follower_id == self.id)
@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 __repr__(self): def __repr__(self):
return '<User %r>' % self.username return '<User %r>' % self.username


+ 2
- 0
app/templates/followers.html View File

@ -10,6 +10,7 @@
<table class="table table-hover followers"> <table class="table table-hover followers">
<thead><tr><th>User</th><th>Since</th></tr></thead> <thead><tr><th>User</th><th>Since</th></tr></thead>
{% for follow in follows %} {% for follow in follows %}
{% if follow.user != user %}
<tr> <tr>
<td> <td>
<a href="{{ url_for('.user', username = follow.user.username) }}"> <a href="{{ url_for('.user', username = follow.user.username) }}">
@ -19,6 +20,7 @@
</td> </td>
<td>{{ moment(follow.timestamp).format('L') }}</td> <td>{{ moment(follow.timestamp).format('L') }}</td>
</tr> </tr>
{% endif %}
{% endfor %} {% endfor %}
</table> </table>
<div class="pagination"> <div class="pagination">


+ 2
- 2
app/templates/user.html View File

@ -40,10 +40,10 @@
{% endif %} {% endif %}
{% endif %} {% endif %}
<a href="{{ url_for('.followers', username=user.username) }}"> <a href="{{ url_for('.followers', username=user.username) }}">
Followers: <span class="badge">{{ user.followers.count() }}</span>
Followers: <span class="badge">{{ user.followers.count() - 1 }}</span>
</a> </a>
<a href="{{ url_for('.followed_by', username=user.username) }}"> <a href="{{ url_for('.followed_by', username=user.username) }}">
Following: <span class="badge">{{ user.followed.count() }}</span>
Following: <span class="badge">{{ user.followed.count() - 1 }}</span>
</a> </a>
{% if current_user.is_authenticated and user != current_user and {% if current_user.is_authenticated and user != current_user and
user.is_following(current_user) %} user.is_following(current_user) %}


+ 6
- 6
tests/test_user_model.py View File

@ -186,8 +186,8 @@ class UserModelTestCase(unittest.TestCase):
self.assertTrue(u1.is_following(u2)) self.assertTrue(u1.is_following(u2))
self.assertFalse(u1.is_followed_by(u2)) self.assertFalse(u1.is_followed_by(u2))
self.assertTrue(u2.is_followed_by(u1)) 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] f = u1.followed.all()[-1]
self.assertTrue(f.followed == u2) self.assertTrue(f.followed == u2)
self.assertTrue(timestamp_before <= f.timestamp <= timestamp_after) self.assertTrue(timestamp_before <= f.timestamp <= timestamp_after)
@ -196,13 +196,13 @@ class UserModelTestCase(unittest.TestCase):
u1.unfollow(u2) u1.unfollow(u2)
db.session.add(u1) db.session.add(u1)
db.session.commit() 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) u2.follow(u1)
db.session.add(u1) db.session.add(u1)
db.session.add(u2) db.session.add(u2)
db.session.commit() db.session.commit()
db.session.delete(u2) db.session.delete(u2)
db.session.commit() db.session.commit()
self.assertTrue(Follow.query.count() == 0)
self.assertTrue(Follow.query.count() == 1)

Loading…
Cancel
Save