diff --git a/.travis.yml b/.travis.yml index 4497fcd..d85b3ff 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,15 +8,13 @@ python: env: - DJANGO=1.8 - DJANGO=1.9 - - DJANGO=master + - DJANGO=1.10 matrix: exclude: - python: "3.3" env: DJANGO=1.9 - python: "3.3" - env: DJANGO=master - - python: "3.5" - env: DJANGO=1.7 + env: DJANGO=1.10 install: - pip install tox coveralls script: diff --git a/docs/templatetags.md b/docs/templatetags.md index 333de7d..6db0a5f 100644 --- a/docs/templatetags.md +++ b/docs/templatetags.md @@ -16,5 +16,23 @@ For instance if there are unread messages in a thread, change the CSS class acco ``` +## unread_threads + +Returns the number of unread threads for the user. Use for notifying a user of new messages, for example in _account_bar.html + +**Argument**: `user` + +For instance if there are unread messages in a thread, change the CSS class accordingly: + +```html +{% load pinax_messages_tags %} + +
  • + {% trans "Messages" %} + {% if user|unread_threads %}{{ user|unread_threads }}{% endif %} + +
  • +``` + *** [Documentation Index](./index.md) diff --git a/pinax/messages/templatetags/pinax_messages_tags.py b/pinax/messages/templatetags/pinax_messages_tags.py index 279a13e..f70cd89 100644 --- a/pinax/messages/templatetags/pinax_messages_tags.py +++ b/pinax/messages/templatetags/pinax_messages_tags.py @@ -1,9 +1,21 @@ from django import template +from pinax.messages.models import Thread register = template.Library() @register.filter def unread(thread, user): + ''' + Check whether there are any unread messages for a particular thread for a user. + ''' return bool(thread.userthread_set.filter(user=user, unread=True)) + + +@register.filter +def unread_threads(user): + ''' + Return the number of Threads with unread messages for this user, useful for highlighting on an account bar for example. + ''' + return Thread.unread(user).count() diff --git a/pinax/messages/tests/tests.py b/pinax/messages/tests/tests.py index e33af13..4cf378f 100644 --- a/pinax/messages/tests/tests.py +++ b/pinax/messages/tests/tests.py @@ -233,6 +233,72 @@ def test_unread(self): "READ", ) + def test_unread_threads_one_unread(self): + """ + Ensure `unread_threads` template_tag produces correct results for one unread message + """ + thread = Message.new_message( + self.brosner, + [self.jtauber], + "Why did you break the internet?", "I demand to know.").thread + + tmpl = """ + {% load pinax_messages_tags %} + {% if user|unread_threads %}{{ user|unread_threads }}{% endif %} + """ + self.assert_renders( + tmpl, + Context({"thread": thread, "user": self.jtauber}), + '1' + ) + + def test_unread_threads_two_messages_incl_reply(self): + """ + Ensure `unread_threads` template_tag produces correct results. + """ + thread = Message.new_message( + self.brosner, + [self.jtauber], + "Why did you break the internet?", "I demand to know.").thread + + Message.new_reply(thread, self.jtauber, "Replying to the first message") + Message.new_reply(thread, self.brosner, "Replying again, so that there are two unread messages on one thread") + + thread = Message.new_message( + self.brosner, + [self.jtauber], + "Second message", "So there are two.").thread + tmpl = """ + {% load pinax_messages_tags %} + {% if user|unread_threads %}{{ user|unread_threads }}{% endif %} + """ + self.assert_renders( + tmpl, + Context({"thread": thread, "user": self.jtauber}), + '2' + ) + + def test_unread_threads_with_all_messages_read(self): + """ + Ensure `unread_threads` template_tag produces correct results. + """ + thread = Message.new_message( + self.brosner, + [self.jtauber], + "Why did you break the internet?", "I demand to know.").thread + + Message.new_reply(thread, self.jtauber, "Replying to the message so that I have no unread Threads") + + tmpl = """ + {% load pinax_messages_tags %} + {{ user|unread_threads }} + """ + self.assert_renders( + tmpl, + Context({"thread": thread, "user": self.jtauber}), + '0' + ) + class TestHookSet(BaseTest): def test_get_user_choices(self): diff --git a/tox.ini b/tox.ini index 6ff14f4..2178956 100644 --- a/tox.ini +++ b/tox.ini @@ -6,10 +6,10 @@ exclude = migrations/*,docs/* [tox] envlist = - py27-{1.8,1.9,master}, + py27-{1.8,1.9,1.10}, py33-{1.8}, - py34-{1.8,1.9,master}, - py35-{1.8,1.9,master} + py34-{1.8,1.9,1.10}, + py35-{1.8,1.9,1.10} [testenv] deps = @@ -17,7 +17,7 @@ deps = flake8 == 2.5.0 1.8: Django>=1.8,<1.9 1.9: Django>=1.9,<1.10 - master: https://github.com/django/django/tarball/master + 1.10: Django>=1.10,<1.11 usedevelop = True setenv = LANG=en_US.UTF-8