forked from apluslms/a-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
746ee3b
commit 51a666d
Showing
17 changed files
with
170 additions
and
311 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
from django.db.models.signals import post_save, post_delete | ||
|
||
from lib.cached import CachedAbstract | ||
from .models import Notification | ||
|
||
|
||
class CachedNotifications(CachedAbstract): | ||
KEY_PREFIX = "notifications" | ||
|
||
def __init__(self, user): | ||
super().__init__(user) | ||
|
||
def _generate_data(self, user, data=None): | ||
if not user or not user.is_authenticated(): | ||
return { | ||
'count': 0, | ||
'notifications': [], | ||
} | ||
|
||
def notification_entry(n): | ||
exercise = n.submission.exercise if n.submission else None | ||
return { | ||
'id': n.id, | ||
'submission_id': n.submission.id if n.submission else 0, | ||
'name': "{} {}, {}".format( | ||
n.course_instance.course.code, | ||
(str(exercise.parent) | ||
if exercise and exercise.parent else | ||
n.course_instance.instance_name), | ||
(str(exercise) | ||
if exercise else | ||
n.subject), | ||
), | ||
'link': n.get_display_url(), | ||
} | ||
|
||
notifications = list( | ||
user.userprofile.received_notifications\ | ||
.filter(seen=False)\ | ||
.select_related( | ||
'submission', | ||
'submission__exercise', | ||
'course_instance', | ||
'course_instance__course', | ||
) | ||
) | ||
return { | ||
'count': len(notifications), | ||
'notifications': [notification_entry(n) for n in notifications], | ||
} | ||
|
||
def count(self): | ||
return self.data['count'] | ||
|
||
def notifications(self): | ||
return self.data['notifications'] | ||
|
||
|
||
def invalidate_notifications(sender, instance, **kwargs): | ||
CachedNotifications.invalidate(instance.recipient) | ||
|
||
|
||
# Automatically invalidate cache when notifications change. | ||
post_save.connect(invalidate_notifications, sender=Notification) | ||
post_delete.connect(invalidate_notifications, sender=Notification) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 14 additions & 21 deletions
35
notification/templates/notification/_notification_menu.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,18 @@ | ||
{% load i18n %} | ||
{% load course %} | ||
{% if unread.count > 0 %} | ||
{% if count > 0 %} | ||
<li role="presentation" class="menu-notification dropdown"> | ||
<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> | ||
<span class="badge badge-danger">{{ unread.count }}</span> {{ unread_message }} | ||
<span class="caret" aria-hidden="true"></span> | ||
<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> | ||
<span class="badge badge-danger">{{ count }}</span> {{ unread_message }} | ||
<span class="caret" aria-hidden="true"></span> | ||
</a> | ||
<ul class="dropdown-menu"> | ||
{% for entry in notifications %} | ||
<li> | ||
<a href="{{ entry.link }}" class="alert-link"> | ||
{{ entry.name }} | ||
</a> | ||
<ul class="dropdown-menu"> | ||
{% for notification in unread.notifications %} | ||
<li> | ||
<a href="{{ notification|url }}" class="alert-link"> | ||
{{ notification.course_instance.course }}: | ||
{% if notification.submission %} | ||
{{ notification.submission.exercise }} | ||
{% else %} | ||
{{ notification.subject }} | ||
{% endif %} | ||
</a> | ||
</li> | ||
{% endfor %} | ||
</ul> | ||
</li> | ||
{% endfor %} | ||
</ul> | ||
</li> | ||
{% endif %} |
11 changes: 5 additions & 6 deletions
11
notification/templates/notification/_notification_messages.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.