Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Various changes to templates, storage and model that make message out…

…put more flexible
  • Loading branch information...
commit 812d205d711df2bc56d78178e784e90ebe58b91a 1 parent 12a0e1f
Samuel Luescher samluescher authored
4 persistent_messages/models.py
View
@@ -73,7 +73,7 @@ def _get_tags(self):
if (self.read):
read_tag = "read"
else:
- read_tag = "unread"
+ read_tag = "unread"
if extra_tags and label_tag:
return u' '.join([extra_tags, label_tag, read_tag])
@@ -83,4 +83,4 @@ def _get_tags(self):
return u' '.join([label_tag, read_tag])
return read_tag
tags = property(_get_tags)
-
+
21 persistent_messages/storage.py
View
@@ -1,4 +1,5 @@
from persistent_messages.models import Message
+from persistent_messages.constants import PERSISTENT_MESSAGE_LEVELS
from django.contrib import messages
from django.contrib.messages.storage.base import BaseStorage
from django.contrib.auth.models import AnonymousUser
@@ -27,6 +28,12 @@ def __init__(self, *args, **kwargs):
self.non_persistent_messages = []
self.is_anonymous = not get_user(self.request).is_authenticated()
+ def _message_queryset(self, exclude_unread=True):
+ qs = Message.objects.filter(user=get_user(self.request)).filter(Q(expires=None) | Q(expires__gt=datetime.datetime.now()))
+ if exclude_unread:
+ qs = qs.exclude(read=True)
+ return qs
+
def _get(self, *args, **kwargs):
"""
Retrieves a list of stored messages. Returns a tuple of the messages
@@ -37,11 +44,23 @@ def _get(self, *args, **kwargs):
if not get_user(self.request).is_authenticated():
return super(PersistentMessageStorage, self)._get(*args, **kwargs)
messages = []
- for message in Message.objects.filter(user=get_user(self.request)).exclude(read=True).filter(Q(expires=None) | Q(expires__gt=datetime.datetime.now())):
+ for message in self._message_queryset():
if not message.is_persistent():
self.non_persistent_messages.append(message)
messages.append(message)
return (messages, True)
+
+ def get_persistent(self):
+ return self._message_queryset(exclude_unread=False).filter(level__in=PERSISTENT_MESSAGE_LEVELS)
+
+ def get_persistent_unread(self):
+ return self._message_queryset(exclude_unread=True).filter(level__in=PERSISTENT_MESSAGE_LEVELS)
+
+ def count_unread(self):
+ return self._message_queryset(exclude_unread=True).count()
+
+ def count_persistent_unread(self):
+ return self.get_persistent_unread().count()
def _delete_non_persistent(self):
for message in self.non_persistent_messages:
14 persistent_messages/templates/persistent_messages/message/includes/message_li.html
View
@@ -0,0 +1,14 @@
+{% load i18n %}
+ <li id="message-{{ message.pk }}" {% if message.tags %} class="{{ message.tags }}"{% endif %}>
+ {% if message.subject %}<strong>{{ message.subject }}</strong><br />{% endif %}
+ {% if message.is_persistent or jquery %}
+ {% with 32 as max_words %}
+ {{ message.message|truncatewords:max_words }}{% if message.message != message.message|truncatewords:max_words %} <a class="message-more" href="{% if message.is_persistent %}{% url message_detail message.pk %}{% else %}#{% endif %}">more</a>{% endif %}<br />
+ {% endwith %}
+ {% else %}
+ {{ message }}
+ {% endif %}
+ {% if message.is_persistent or jquery %}
+ <a class="message-close icon" href="{% if message.is_persistent %}{% url message_mark_read message.pk %}{% else %}#{% endif %}"><span>{% trans "close" %}</span></a>
+ {% endif %}
+ </li>
19 persistent_messages/templates/persistent_messages/message/includes/messages.html
View
@@ -1,22 +1,13 @@
+{% load i18n %}
{% if messages %}
<ul class="messages">
{% for message in messages %}
- <li id="message-{{ message.pk }}" {% if message.tags %} class="{{ message.tags }}"{% endif %}>
- {% if message.subject %}<strong>{{ message.subject }}</strong><br />{% endif %}
- {% if message.is_persistent or jquery %}
- {% with 32 as max_words %}
- {{ message.message|truncatewords:max_words }}{% if message.message != message.message|truncatewords:max_words %} <a class="message-more" href="{% if message.is_persistent %}{% url message_detail message.pk %}{% else %}#{% endif %}">more</a>{% endif %}<br />
- {% endwith %}
- {% else %}
- {{ message }}
- {% endif %}
- {% if message.is_persistent or jquery %}
- <a class="message-close" href="{% if message.is_persistent %}{% url message_mark_read message.pk %}{% else %}#{% endif %}"><span>Close</span></a>
- {% endif %}
- </li>
+ {% if not hide_persistent or not message.is_persistent %}
+ {% include "persistent_messages/message/includes/message_li.html" %}
+ {% endif %}
{% endfor %}
</ul>
{% if close_all and messages|length > 1 %}
-<a class="message-close-all" href="{% url message_mark_all_read %}"><span>Close all</span></a>
+<a class="message-close-all icon" href="{% url message_mark_all_read %}"><span>{% trans "close all" %}</span></a>
{% endif %}
{% endif %}
0  persistent_messages/templatetags/__init__.py
View
No changes.
15 persistent_messages/templatetags/message_filters.py
View
@@ -0,0 +1,15 @@
+# coding=utf-8
+from django import template
+
+def latest(queryset, count):
+ return queryset.order_by('-created')[:count]
+
+def latest_or_unread(queryset, count):
+ count_unread = queryset.filter(read=False).count()
+ if count_unread > count:
+ count = count_unread
+ return queryset.order_by('read', '-created')[:count]
+
+register = template.Library()
+register.filter(latest)
+register.filter(latest_or_unread)
Please sign in to comment.
Something went wrong with that request. Please try again.