Skip to content

Commit

Permalink
Merge 7a9c24e into ca9dbbf
Browse files Browse the repository at this point in the history
  • Loading branch information
mattlindesay committed Feb 19, 2017
2 parents ca9dbbf + 7a9c24e commit 82a4e06
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 8 deletions.
6 changes: 2 additions & 4 deletions .travis.yml
Expand Up @@ -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:
Expand Down
18 changes: 18 additions & 0 deletions docs/templatetags.md
Expand Up @@ -16,5 +16,23 @@ For instance if there are unread messages in a thread, change the CSS class acco
</div>
```

## 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 %}

<li class="{% if user|unread_threads %}unread{% endif %}">
<a href="{% url 'pinax_messages:inbox' %}"><i class="fa fa-envelope"></i> {% trans "Messages" %}
{% if user|unread_threads %}<sup>{{ user|unread_threads }}</sup>{% endif %}
</a>
</li>
```

***
[Documentation Index](./index.md)
12 changes: 12 additions & 0 deletions 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()
66 changes: 66 additions & 0 deletions pinax/messages/tests/tests.py
Expand Up @@ -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):
Expand Down
8 changes: 4 additions & 4 deletions tox.ini
Expand Up @@ -6,18 +6,18 @@ 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 =
coverage == 4.0.2
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
Expand Down

0 comments on commit 82a4e06

Please sign in to comment.