SciPost Code Repository

Skip to content
Snippets Groups Projects
Commit d7b86810 authored by Jorran de Wit's avatar Jorran de Wit
Browse files

Optimize

parent 6dbc0fee
No related branches found
No related tags found
No related merge requests found
...@@ -85,7 +85,7 @@ class Notification(models.Model): ...@@ -85,7 +85,7 @@ class Notification(models.Model):
current timestamp. current timestamp.
""" """
from django.utils.timesince import timesince as timesince_ from django.utils.timesince import timesince as timesince_
return timesince_(self.timestamp, now) return timesince_(self.created, now)
@property @property
def slug(self): def slug(self):
......
...@@ -13,6 +13,6 @@ urlpatterns = [ ...@@ -13,6 +13,6 @@ urlpatterns = [
url(r'^delete/(?P<slug>\d+)/$', views.delete, name='delete'), url(r'^delete/(?P<slug>\d+)/$', views.delete, name='delete'),
url(r'^api/unread_count/$', views.live_unread_notification_count, url(r'^api/unread_count/$', views.live_unread_notification_count,
name='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'), name='live_unread_notification_list'),
] ]
...@@ -102,11 +102,11 @@ def live_unread_notification_count(request): ...@@ -102,11 +102,11 @@ def live_unread_notification_count(request):
return JsonResponse(data) return JsonResponse(data)
def live_unread_notification_list(request): def live_notification_list(request):
if not request.user.is_authenticated(): if not request.user.is_authenticated():
data = { data = {
'unread_count': 0, 'unread_count': 0,
'unread_list': [] 'list': []
} }
return JsonResponse(data) return JsonResponse(data)
...@@ -117,9 +117,9 @@ def live_unread_notification_list(request): ...@@ -117,9 +117,9 @@ def live_unread_notification_list(request):
except ValueError: except ValueError:
num_to_fetch = 5 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 = model_to_dict(n)
struct['slug'] = id2slug(n.id) struct['slug'] = id2slug(n.id)
if n.actor: if n.actor:
...@@ -130,11 +130,11 @@ def live_unread_notification_list(request): ...@@ -130,11 +130,11 @@ def live_unread_notification_list(request):
if n.action_object: if n.action_object:
struct['action_object'] = str(n.action_object) struct['action_object'] = str(n.action_object)
unread_list.append(struct) list.append(struct)
if request.GET.get('mark_as_read'): if request.GET.get('mark_as_read'):
n.mark_as_read() n.mark_as_read()
data = { data = {
'unread_count': request.user.notifications.unread().count(), 'unread_count': request.user.notifications.unread().count(),
'unread_list': unread_list 'list': list
} }
return JsonResponse(data) return JsonResponse(data)
var notify_badge_class = 'live_notify_badge'; var notify_badge_class = 'live_notify_badge';
var notify_menu_class = 'live_notify_list'; var notify_menu_class = 'live_notify_list';
var notify_api_url_count = '/notifications/api/unread_count/'; 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_fetch_count = '5';
var notify_refresh_period = 15000; var notify_refresh_period = 15000;
var consecutive_misfires = 0; var consecutive_misfires = 0;
...@@ -20,7 +20,7 @@ function fill_notification_badge(data) { ...@@ -20,7 +20,7 @@ function fill_notification_badge(data) {
function get_notification_list() { function get_notification_list() {
var data = fetch_api_data(notify_api_url_list, function(data) { 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 = ""; var message = "";
if(typeof item.actor !== 'undefined'){ if(typeof item.actor !== 'undefined'){
message = '<strong>' + item.actor + '</strong>'; message = '<strong>' + item.actor + '</strong>';
...@@ -39,7 +39,9 @@ function get_notification_list() { ...@@ -39,7 +39,9 @@ function get_notification_list() {
message = message + " " + item.timestamp; message = message + " " + item.timestamp;
} }
return '<li class="list-group-item ' + (item.unread ? ' active' : '') + '">' + message + '</li>'; 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; document.getElementById('notification-list').innerHTML = messages;
}); });
...@@ -93,7 +95,7 @@ $(function(){ ...@@ -93,7 +95,7 @@ $(function(){
} }
var get_notifications = 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(); get_notification_list();
return _str; return _str;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment