From d7b868105d70833d64da1d4ff5e4f4a55f183a67 Mon Sep 17 00:00:00 2001
From: Jorran de Wit <jorrandewit@outlook.com>
Date: Wed, 13 Sep 2017 12:43:02 +0200
Subject: [PATCH] Optimize

---
 notifications/models.py                           |  2 +-
 notifications/urls.py                             |  2 +-
 notifications/views.py                            | 12 ++++++------
 scipost/static/scipost/assets/js/notifications.js | 10 ++++++----
 4 files changed, 14 insertions(+), 12 deletions(-)

diff --git a/notifications/models.py b/notifications/models.py
index e3205f9c4..49c6bdd1c 100644
--- a/notifications/models.py
+++ b/notifications/models.py
@@ -85,7 +85,7 @@ class Notification(models.Model):
         current timestamp.
         """
         from django.utils.timesince import timesince as timesince_
-        return timesince_(self.timestamp, now)
+        return timesince_(self.created, now)
 
     @property
     def slug(self):
diff --git a/notifications/urls.py b/notifications/urls.py
index 7386359ef..cd551b2ad 100644
--- a/notifications/urls.py
+++ b/notifications/urls.py
@@ -13,6 +13,6 @@ urlpatterns = [
     url(r'^delete/(?P<slug>\d+)/$', views.delete, name='delete'),
     url(r'^api/unread_count/$', views.live_unread_notification_count,
         name='live_unread_notification_count'),
-    url(r'^api/unread_list/$', views.live_unread_notification_list,
+    url(r'^api/list/$', views.live_notification_list,
         name='live_unread_notification_list'),
 ]
diff --git a/notifications/views.py b/notifications/views.py
index a3b88a85a..2170da02f 100644
--- a/notifications/views.py
+++ b/notifications/views.py
@@ -102,11 +102,11 @@ def live_unread_notification_count(request):
     return JsonResponse(data)
 
 
-def live_unread_notification_list(request):
+def live_notification_list(request):
     if not request.user.is_authenticated():
         data = {
            'unread_count': 0,
-           'unread_list': []
+           'list': []
         }
         return JsonResponse(data)
 
@@ -117,9 +117,9 @@ def live_unread_notification_list(request):
     except ValueError:
         num_to_fetch = 5
 
-    unread_list = []
+    list = []
 
-    for n in request.user.notifications.unread()[:num_to_fetch]:
+    for n in request.user.notifications.all()[:num_to_fetch]:
         struct = model_to_dict(n)
         struct['slug'] = id2slug(n.id)
         if n.actor:
@@ -130,11 +130,11 @@ def live_unread_notification_list(request):
         if n.action_object:
             struct['action_object'] = str(n.action_object)
 
-        unread_list.append(struct)
+        list.append(struct)
         if request.GET.get('mark_as_read'):
             n.mark_as_read()
     data = {
         'unread_count': request.user.notifications.unread().count(),
-        'unread_list': unread_list
+        'list': list
     }
     return JsonResponse(data)
diff --git a/scipost/static/scipost/assets/js/notifications.js b/scipost/static/scipost/assets/js/notifications.js
index dfc340784..14ef93ed9 100644
--- a/scipost/static/scipost/assets/js/notifications.js
+++ b/scipost/static/scipost/assets/js/notifications.js
@@ -1,7 +1,7 @@
 var notify_badge_class = 'live_notify_badge';
 var notify_menu_class = 'live_notify_list';
 var notify_api_url_count = '/notifications/api/unread_count/';
-var notify_api_url_list = '/notifications/api/unread_list/';
+var notify_api_url_list = '/notifications/api/list/';
 var notify_fetch_count = '5';
 var notify_refresh_period = 15000;
 var consecutive_misfires = 0;
@@ -20,7 +20,7 @@ function fill_notification_badge(data) {
 function get_notification_list() {
     var data = fetch_api_data(notify_api_url_list, function(data) {
 
-        var messages = data.unread_list.map(function (item) {
+        var messages = data.list.map(function (item) {
             var message = "";
             if(typeof item.actor !== 'undefined'){
                 message = '<strong>' + item.actor + '</strong>';
@@ -39,7 +39,9 @@ function get_notification_list() {
                 message = message + " " + item.timestamp;
             }
             return '<li class="list-group-item ' + (item.unread ? ' active' : '') + '">' + message + '</li>';
-        }).join('')
+        }).join('');
+
+        if (messages == '') { messages = 'You have no notifications.' }
 
         document.getElementById('notification-list').innerHTML = messages;
     });
@@ -93,7 +95,7 @@ $(function(){
     }
 
     var get_notifications = function() {
-        var _str = '<ul id="notification-list" class="update_notifications list-group"><div class="w-100 text-center"><i class="fa fa-circle-o-notch fa-spin fa-fw"></i><span class="sr-only">Loading...</span></div></ul>';
+        var _str = '<ul id="notification-list" class="update_notifications list-group"><div class="w-100 text-center py-4"><i class="fa fa-circle-o-notch fa-2x fa-spin fa-fw"></i><span class="sr-only">Loading...</span></div></ul>';
         get_notification_list();
         return _str;
     }
-- 
GitLab