Commit 42dd7983 by Őry Máté

dashboard: get notifications with ajax

parent e62d7a9b
...@@ -212,19 +212,8 @@ $(function () { ...@@ -212,19 +212,8 @@ $(function () {
return false; return false;
}); });
/* notification read */
$("#notification-button").click(function() { $("#notification-button").click(function() {
$.ajax({ $('.notification-messages').load("/dashboard/notifications/");
type: "POST",
url: "/dashboard/notifications/",
headers: {"X-CSRFToken": getCookie('csrftoken')},
success: function(re, textStatus, xhr) {
//console.log("success");
},
error: function(xhr, textStatus, error) {
addMessage('Uh oh :(', 'danger')
}
});
}); });
}); });
......
{% load i18n %}
{% for n in notifications %} {% for n in notifications %}
<li class="notification-message"> <li class="notification-message">
<span class="notification-message-subject"> <span class="notification-message-subject">
...@@ -12,4 +13,8 @@ ...@@ -12,4 +13,8 @@
{{ n.message }} {{ n.message }}
</div> </div>
</li> </li>
{% empty %}
<li class="notification-message">
{% trans "You have no notifications." %}
</li>
{% endfor %} {% endfor %}
...@@ -6,10 +6,10 @@ ...@@ -6,10 +6,10 @@
{% block navbar-ul %} {% block navbar-ul %}
<li class="dropdown" id="notification-button"> <li class="dropdown" id="notification-button">
<a href="{% url "dashboard.views.notifications" %}" style="color: white; font-size: 12px;" class="dropdown-toggle" data-toggle="dropdown"> <a href="{% url "dashboard.views.notifications" %}" style="color: white; font-size: 12px;" class="dropdown-toggle" data-toggle="dropdown">
Notifications{% if new_notifications > 0 %} <span class="badge">{{ new_notifications }}</span>{% endif %} Notifications{% if new_notifications %} <span class="badge">{{ new_notifications }}</span>{% endif %}
</a> </a>
<ul class="dropdown-menu notification-messages"> <ul class="dropdown-menu notification-messages">
{% include "dashboard/_notifications-timeline.html" %} <li>{% trans "Loading..." %}</li>
</ul> </ul>
</li> </li>
{% endblock %} {% endblock %}
......
...@@ -82,7 +82,6 @@ class IndexView(LoginRequiredMixin, TemplateView): ...@@ -82,7 +82,6 @@ class IndexView(LoginRequiredMixin, TemplateView):
}) })
if user is not None: if user is not None:
context['notifications'] = user.notification_set.all()[:10]
context['new_notifications'] = user.notification_set.filter( context['new_notifications'] = user.notification_set.filter(
status="new").count() status="new").count()
...@@ -1660,7 +1659,12 @@ class NodeGraphView(SuperuserRequiredMixin, GraphViewBase): ...@@ -1660,7 +1659,12 @@ class NodeGraphView(SuperuserRequiredMixin, GraphViewBase):
class NotificationView(LoginRequiredMixin, TemplateView): class NotificationView(LoginRequiredMixin, TemplateView):
template_name = "dashboard/notifications.html"
def get_template_names(self):
if self.request.is_ajax():
return ['dashboard/_notifications-timeline.html']
else:
return ['dashboard/notifications.html']
def get_context_data(self, *args, **kwargs): def get_context_data(self, *args, **kwargs):
context = super(NotificationView, self).get_context_data( context = super(NotificationView, self).get_context_data(
...@@ -1668,8 +1672,9 @@ class NotificationView(LoginRequiredMixin, TemplateView): ...@@ -1668,8 +1672,9 @@ class NotificationView(LoginRequiredMixin, TemplateView):
# we need to convert it to list, otherwise it's gonna be # we need to convert it to list, otherwise it's gonna be
# similar to a QuerySet and update everything to # similar to a QuerySet and update everything to
# read status after get # read status after get
n = 10 if self.request.is_ajax() else 1000
context['notifications'] = list( context['notifications'] = list(
self.request.user.notification_set.values()) self.request.user.notification_set.values()[:n])
return context return context
def get(self, *args, **kwargs): def get(self, *args, **kwargs):
...@@ -1679,15 +1684,3 @@ class NotificationView(LoginRequiredMixin, TemplateView): ...@@ -1679,15 +1684,3 @@ class NotificationView(LoginRequiredMixin, TemplateView):
u.status = "read" u.status = "read"
u.save() u.save()
return response return response
def post(self, request, *args, **kwargs):
# unread notifications
un = request.user.notification_set.filter(status="new")
for u in un:
u.status = "read"
u.save()
if request.is_ajax():
return HttpResponse({'result': 'ok'})
else:
pass
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment