From fdec448e745fb7495da5daf36bcb6d549ff00564 Mon Sep 17 00:00:00 2001 From: Xevion Date: Wed, 30 Mar 2022 01:20:01 -0500 Subject: [PATCH] Show up to 2 usernames, calculate number unshown on likes status text - Switched to username for likes display text instead of name - Reminder: Strict usernames, no spaces, a-Z + 0-9 --- models.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/models.py b/models.py index 472da85..3761ed9 100644 --- a/models.py +++ b/models.py @@ -101,18 +101,20 @@ class Post(db.Model): def get_like_text(self) -> str: like_count = self.get_like_count() - top_likes = PostLike.query.filter_by(post_id=self.id).order_by(PostLike.timestamp.asc()).limit(3) + top_likes = PostLike.query.filter_by(post_id=self.id).order_by(PostLike.timestamp.asc()).limit(2) users = [like.user for like in top_likes] - names = [f'{user.name}' for user in users] + names = [f'{user.username}' for user in users] - if like_count >= 3: format_string = '{0}, {1} and {2} has liked this post.' + if like_count >= 3: format_string = '{0}, {1} and {other_text} have liked this post.' elif like_count == 2: format_string = '{0} and {1} has liked this post.' elif like_count == 1: format_string = '{0} has liked this post.' else: format_string = '0 likes' + others: int = like_count - top_likes.count() + if others > 0: + return format_string.format(*names, other_text=f'{others} other{"s" if others != 1 else ""}') return format_string.format(*names) - class PostLike(db.Model): id = db.Column(db.Integer, primary_key=True) timestamp = db.Column(db.DateTime, server_default=func.now())