Skip to content

Commit

Permalink
Add notifications to menus
Browse files Browse the repository at this point in the history
  • Loading branch information
teemulehtinen committed Sep 7, 2015
1 parent 6972ff4 commit a65893b
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 11 deletions.
5 changes: 5 additions & 0 deletions assets/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ html, body {
height: 20px;
border-left: 1px solid black;
}
.badge-danger, .panel-primary>.panel-heading .badge-danger {
background-color: #d9534f;
color: #fff;
}
.badge-warning, .panel-primary>.panel-heading .badge-warning {
background-color: #f0ad4e;
color: #fff;
Expand All @@ -196,6 +200,7 @@ html, body {
background-color: #5cb85c;
color: #fff;
}
.panel-primary>.panel-heading .badge-danger,
.panel-primary>.panel-heading .badge-warning,
.panel-primary>.panel-heading .badge-info,
.panel-primary>.panel-heading .badge-success {
Expand Down
5 changes: 5 additions & 0 deletions course/templates/course/_course_menu.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{% load i18n %}
{% load course %}
{% load notification %}
{% load external_services %}

<li role="presentation" class="header"><h4>{% trans "Course" %}</h4></li>
Expand All @@ -19,6 +20,10 @@
<a href="{{ instance|url:'notifications' }}">
<span class="glyphicon glyphicon-envelope" aria-hidden="true"></span>
{% trans "Notifications" %}
{% notification_count as count %}
{% if count > 0 %}
<span class="badge badge-danger" title="{% blocktrans with count=count %}{{ count }} unread{% endblocktrans %}">{{ count }}</span>
{% endif %}
</a>
</li>

Expand Down
5 changes: 5 additions & 0 deletions notification/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ def get_course(cls, course_instance, user, per_page=30, page=1):
course_instance=course_instance)[skip:(skip + per_page)]
return NotificationSet(qs)

@classmethod
def get_course_new_count(cls, course_instance, user):
return user.userprofile.received_notifications.filter(
course_instance=course_instance, seen=False).count()

def __init__(self, queryset):
self.notifications = list(queryset)

Expand Down
21 changes: 21 additions & 0 deletions notification/templates/notification/_notification_menu.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{% load i18n %}
{% load course %}
{% if unread.count > 0 %}
<li role="presentation" class="menu-notification">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
<span class="glyphicon glyphicon-envelope pull-left" aria-hidden="true"></span>

{% blocktrans with count=unread.count count_p=unread.count|pluralize %}
<span class="badge badge-danger">{{ count }}</span> new notification{{ count_p }}
<span class="caret" aria-hidden="true"></span>
{% endblocktrans %}
<ul class="dropdown-menu">
{% for notification in unread.notifications %}
<li><a href="{{ notification.course_instance|url:'notifications' }}" class="alert-link">
{{ notification.course_instance.course }}: {{ notification.subject }}
</a></li>
{% endfor %}
</ul>
</a>
</li>
{% endif %}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{% load i18n %}
{% load course %}
{% if unread.count > 0 %}
<div class="alert alert-info">
<div class="alert alert-danger visible-xs">
<span class="glyphicon glyphicon-envelope pull-left" aria-hidden="true"></span>
<p>
{% blocktrans with count=unread.count count_p=unread.count|pluralize %}
Expand Down
35 changes: 25 additions & 10 deletions notification/templatetags/notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,37 @@
register = template.Library()


@register.inclusion_tag("notification/_notification_messages.html", takes_context=True)
def notification_messages(context):
unread = []
def _context_user(context):
if "request" in context:
user = context["request"].user
if user.is_authenticated():
unread = NotificationSet.get_unread(user)
return user
return None


def _unread_messages(context):
unread = []
user = _context_user(context)
if user:
unread = NotificationSet.get_unread(user)
return {
"unread": unread,
}

@register.assignment_tag
def new_course_notifications(course_instance, user):
return NotificationSet.get_course_unread_and_mark(course_instance, user)

@register.inclusion_tag("notification/_notification_messages.html", takes_context=True)
def notification_messages(context):
return _unread_messages(context)


@register.inclusion_tag("notification/_notification_menu.html", takes_context=True)
def notification_menu(context):
return _unread_messages(context)


@register.assignment_tag
def old_course_notifications(course_instance, user):
return NotificationSet.get_course_read(course_instance, user)
@register.assignment_tag(takes_context=True)
def notification_count(context):
user = _context_user(context)
if user and "instance" in context:
return NotificationSet.get_course_new_count(context["instance"], user)
return 0

0 comments on commit a65893b

Please sign in to comment.