From 19e7af7e583158bf82d9da406d61401ae498e49a Mon Sep 17 00:00:00 2001 From: Tim Van Baak Date: Fri, 1 Oct 2021 16:40:27 -0700 Subject: [PATCH] Show unread count in post sidebar link --- amanuensis/backend/post.py | 13 +++++++++++-- amanuensis/db/models.py | 2 +- amanuensis/server/__init__.py | 1 + amanuensis/server/lexicon.jinja | 2 +- 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/amanuensis/backend/post.py b/amanuensis/backend/post.py index 38dbea4..a7c38e8 100644 --- a/amanuensis/backend/post.py +++ b/amanuensis/backend/post.py @@ -4,8 +4,7 @@ Post query interface from typing import Optional, Sequence, Tuple -from sqlalchemy import select, update, func -from sqlalchemy.sql.sqltypes import DateTime +from sqlalchemy import select, update, func, or_, DateTime from amanuensis.db import DbContext, Post from amanuensis.db.models import Lexicon, Membership @@ -87,3 +86,13 @@ def get_posts_for_membership( ).scalars() return new_posts, old_posts + + +def get_unread_count(db: DbContext, membership_id: int) -> int: + """Get the number of posts that the member has not seen""" + return db( + select(func.count(Post.id)) + .join(Membership, Membership.lexicon_id == Post.lexicon_id) + .where(or_(Membership.last_post_seen.is_(None), Post.created > Membership.last_post_seen)) + .where(Membership.id == membership_id) + ).scalar() diff --git a/amanuensis/db/models.py b/amanuensis/db/models.py index 4633e83..0f7aead 100644 --- a/amanuensis/db/models.py +++ b/amanuensis/db/models.py @@ -656,7 +656,7 @@ class Post(ModelBase): ################ # The timestamp the post was created - created = Column(DateTime, nullable=False, server_default=func.utcnow()) + created = Column(DateTime, nullable=False, server_default=func.now()) # The body of the post body = Column(Text, nullable=False) diff --git a/amanuensis/server/__init__.py b/amanuensis/server/__init__.py index c8a9827..4f70c90 100644 --- a/amanuensis/server/__init__.py +++ b/amanuensis/server/__init__.py @@ -76,6 +76,7 @@ def get_app( "memq": memq, "charq": charq, "indq": indq, + "postq": postq, "current_lexicon": current_lexicon, "current_membership": current_membership } diff --git a/amanuensis/server/lexicon.jinja b/amanuensis/server/lexicon.jinja index 7593601..1c332bc 100644 --- a/amanuensis/server/lexicon.jinja +++ b/amanuensis/server/lexicon.jinja @@ -18,7 +18,7 @@ {% block sb_posts %}Posts{% endblock %} + {% endif %}>Posts{% set unread_count = postq.get_unread_count(g.db, current_membership.id) if current_membership else None %}{% if unread_count %} ({{ unread_count }}){% endif %}{% endblock %} {% block sb_rules %}