From 564f5b38f1b042d57da33c257002b75c47ebab97 Mon Sep 17 00:00:00 2001 From: TLSM Date: Wed, 15 Jun 2022 01:43:34 -0400 Subject: [PATCH] Clarify notif color logic, extend to modaction. Previously, notification bell linking & color logic was interspersed between templates and the user model. It doesn't _really_ belong in the user model, but it's been moved out of the templates to there to at least centralize it. This was also used as an opportunity to make the modactions inbox color the bell appropriately. --- files/classes/user.py | 40 +++++++++++++++++++++++++----- files/templates/header.html | 4 +-- files/templates/notifications.html | 4 +-- 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/files/classes/user.py b/files/classes/user.py index f420712e6..ecaa5fc17 100644 --- a/files/classes/user.py +++ b/files/classes/user.py @@ -441,13 +441,23 @@ class User(Base): @property @lazy def notifications_count(self): - notifs = g.db.query(Notification.user_id).join(Comment).filter(Notification.user_id == self.id, Notification.read == False, Comment.is_banned == False, Comment.deleted_utc == 0) + notifs = g.db.query(Notification.user_id).join(Comment).filter( + Notification.user_id == self.id, Notification.read == False, + Comment.is_banned == False, Comment.deleted_utc == 0) if not self.shadowbanned and self.admin_level < 3: notifs = notifs.join(User, User.id == Comment.author_id).filter(User.shadowbanned == None) return notifs.count() + @property + @lazy + def normal_notifications_count(self): + return self.notifications_count \ + - self.post_notifications_count \ + - self.modaction_notifications_count \ + - self.reddit_notifications_count + @property @lazy def post_notifications_count(self): @@ -475,11 +485,29 @@ class User(Base): @property @lazy - def normal_count(self): - return self.notifications_count \ - - self.post_notifications_count \ - - self.modaction_notifications_count \ - - self.reddit_notifications_count + def notifications_do(self): + # only meaningful when notifications_count > 0; otherwise falsely '' ~ normal + if self.normal_notifications_count > 0: + return '' + elif self.post_notifications_count > 0: + return 'posts' + elif self.modaction_notifications_count > 0: + return 'modactions' + elif self.reddit_notifications_count > 0: + return 'reddit' + return '' + + @property + @lazy + def notifications_color(self): + colors = { + '': '#dc3545', + 'posts': '#0000ff', + 'modactions': '#e5990d', + 'reddit': '#805ad5', + } + return colors[self.notifications_do] if self.notifications_do \ + else colors[''] @property @lazy diff --git a/files/templates/header.html b/files/templates/header.html index 2d2dacf47..af842c3b7 100644 --- a/files/templates/header.html +++ b/files/templates/header.html @@ -68,7 +68,7 @@ {% if v %} {% if v.notifications_count %} - {{v.notifications_count}} + {{v.notifications_count}} {% else %} {% endif %} @@ -108,7 +108,7 @@ {% if v.notifications_count %} {% else %}