Browse Source

Chapter 12: Self-followers (12e)

T. Meissner 4 months 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):
115 115
                 self.role = Role.query.filter_by(default=True).first()
116 116
         if self.email is not None and self.avatar_hash is None:
117 117
             self.avatar_hash = self.gravatar_hash()
118
+        self.follow(self)
118 119
 
119 120
     @property
120 121
     def password(self):
@@ -231,6 +232,14 @@ class User(UserMixin, db.Model):
231 232
         return Post.query.join(Follow, Follow.followed_id == Post.author_id) \
232 233
                .filter(Follow.follower_id == self.id)
233 234
 
235
+    @staticmethod
236
+    def add_self_follows():
237
+        for user in User.query.all():
238
+            if not user.is_following(user):
239
+                user.follow(user)
240
+                db.session.add(user)
241
+                db.session.commit()
242
+
234 243
     def __repr__(self):
235 244
         return '<User %r>' % self.username
236 245
 

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

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

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

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

+ 6
- 6
tests/test_user_model.py View File

@@ -186,8 +186,8 @@ class UserModelTestCase(unittest.TestCase):
186 186
         self.assertTrue(u1.is_following(u2))
187 187
         self.assertFalse(u1.is_followed_by(u2))
188 188
         self.assertTrue(u2.is_followed_by(u1))
189
-        self.assertTrue(u1.followed.count() == 1)
190
-        self.assertTrue(u2.followers.count() == 1)
189
+        self.assertTrue(u1.followed.count() == 2)
190
+        self.assertTrue(u2.followers.count() == 2)
191 191
         f = u1.followed.all()[-1]
192 192
         self.assertTrue(f.followed == u2)
193 193
         self.assertTrue(timestamp_before <= f.timestamp <= timestamp_after)
@@ -196,13 +196,13 @@ class UserModelTestCase(unittest.TestCase):
196 196
         u1.unfollow(u2)
197 197
         db.session.add(u1)
198 198
         db.session.commit()
199
-        self.assertTrue(u1.followed.count() == 0)
200
-        self.assertTrue(u2.followers.count() == 0)
201
-        self.assertTrue(Follow.query.count() == 0)
199
+        self.assertTrue(u1.followed.count() == 1)
200
+        self.assertTrue(u2.followers.count() == 1)
201
+        self.assertTrue(Follow.query.count() == 2)
202 202
         u2.follow(u1)
203 203
         db.session.add(u1)
204 204
         db.session.add(u2)
205 205
         db.session.commit()
206 206
         db.session.delete(u2)
207 207
         db.session.commit()
208
-        self.assertTrue(Follow.query.count() == 0)
208
+        self.assertTrue(Follow.query.count() == 1)