Search - User list
Full Version: Quick way of figuring out if there are unread messages
Root » The Other » Quick way of figuring out if there are unread messages
1
BrianHV
I have a low-traffic forum as part of a larger django project. I want my base page to have an indicator showing if there are unread messages. Is there an easy way to do that? Bonus points for providing the number of unread messages.

On a related note, is there a reason for using the JSONField to track unread messages rather than using a tracking table?
slav0nic
reason of course exist, or u have batter solution (excluded coma separated fields, etc)?
M2M?) it really sux, as u planing marked all readed topics etc + with m2m u got tons of entries, in django bb max len((entries in tracing tables) <= len(users)

but, it's not brilliant i planing rewrite part with marking read/unread for forum

what about first question - interesting question:
https://bitbucket.org/slav0nic/djangobb/changeset/ddfffe696aa8 [bitbucket.org] maybe this will help for understand as this work

u can show last posts and shown if its readed via has_unreads tag [bitbucket.org]
BrianHV
I forgot to mention that I knew about has_unreads. I'm looking for a version of that that doesn't require a topic as an argument; I need to know if the user has any unread posts in the entire forum.

As for the unread tracking, I'd keep it simple:

    class PostsRead(models.Model):
user = models.ForeignKey(User)
post = models.ForeignKey(Post)

class Meta:
unique_together = ('user', 'post')
Doing it this way would allow for querying the unread posts directly without having to decode a JSON string first.

(Edit: playing around with formatting. Can't figure out how to get the code to indent.)

Edited again: think I figured out formatting. But I also think that may have been what you mean when you mentioned m2m fields. Did you already try this? Did you hit performance issues or something?
slav0nic
u used very old forum version maybe with trouble at deleting posts (then can droping topic / forum if u tried deleting last post/topic)

yes, m2m is slow and limited in some case.

for detecting is forum has unreaded posts u can check is last_posts at forum readed by user (exist at m2m), no? - forum unreaded

BrianHV
If anyone else needs this, here's the filter I wrote. Most of this code is cribbed from the search view in djangobb_forum/views.py. It's fast enough for my current needs, but no guarantees that it will scale.

from django import template
from django.db.models import Q
from djangobb_forum import settings as forum_settings
from djangobb_forum.models import PostTracking, Topic
from djangobb_forum.templatetags import forum_extras

register = template.Library()

@register.filter
def unread_topics(user):
groups = user.groups.all() or [] #removed after django > 1.2.3 release
topics = Topic.objects.filter(
Q(forum__category__groups__in=groups) | \
Q(forum__category__groups__isnull=True))
try:
last_read = PostTracking.objects.get(user=user).last_read
if last_read:
topics = topics.filter(last_post__updated__gte=last_read).all()
else:
#searching more than forum_settings.SEARCH_PAGE_SIZE in this way - not good idea :]
topics = [topic for topic in topics[:forum_settings.SEARCH_PAGE_SIZE] if forum_extras.has_unreads(topic, user)]
except PostTracking.DoesNotExist:
pass
return len(topics)
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Powered by DjangoBB