From 0474c884e952179202e92f1c2658822273f7c76b Mon Sep 17 00:00:00 2001 From: nahid Date: Mon, 2 Mar 2020 12:31:19 +0600 Subject: [PATCH] Add helpers.py file with some helper functions to ensure DRY principle --- notifications/helpers.py | 34 +++++++++++++++++++++++ notifications/views.py | 60 +++++----------------------------------- 2 files changed, 41 insertions(+), 53 deletions(-) create mode 100644 notifications/helpers.py diff --git a/notifications/helpers.py b/notifications/helpers.py new file mode 100644 index 0000000..61252ba --- /dev/null +++ b/notifications/helpers.py @@ -0,0 +1,34 @@ +from django.forms import model_to_dict +from notifications.utils import id2slug +from notifications.settings import get_config + +def get_num_to_fetch(request): + default_num_to_fetch = get_config()['NUM_TO_FETCH'] + try: + # If they don't specify, make it 5. + num_to_fetch = request.GET.get('max', default_num_to_fetch) + num_to_fetch = int(num_to_fetch) + if not (1 <= num_to_fetch <= 100): + num_to_fetch = default_num_to_fetch + except ValueError: # If casting to an int fails. + num_to_fetch = default_num_to_fetch + return num_to_fetch + +def get_notification_list(request, method_name='all'): + num_to_fetch = get_num_to_fetch(request) + notification_list = [] + for notification in getattr(request.user.notifications, method_name)()[0:num_to_fetch]: + struct = model_to_dict(notification) + struct['slug'] = id2slug(notification.id) + if notification.actor: + struct['actor'] = str(notification.actor) + if notification.target: + struct['target'] = str(notification.target) + if notification.action_object: + struct['action_object'] = str(notification.action_object) + if notification.data: + struct['data'] = notification.data + notification_list.append(struct) + if request.GET.get('mark_as_read'): + notification.mark_as_read() + return notification_list diff --git a/notifications/views.py b/notifications/views.py index 126d325..f2363b6 100644 --- a/notifications/views.py +++ b/notifications/views.py @@ -6,11 +6,13 @@ from distutils.version import \ from django import get_version from django.conf import settings from django.contrib.auth.decorators import login_required -from django.forms import model_to_dict from django.shortcuts import get_object_or_404, redirect from django.utils.decorators import method_decorator -from django.utils.encoding import iri_to_uri -from django.utils.http import url_has_allowed_host_and_scheme +from django.views.generic import ListView +from notifications import settings +from notifications.models import Notification +from notifications.utils import slug2id +from notifications.helpers import get_notification_list from django.views.decorators.cache import never_cache from django.views.generic import ListView from notifications import settings as notification_settings @@ -162,32 +164,8 @@ def live_unread_notification_list(request): } return JsonResponse(data) - default_num_to_fetch = notification_settings.get_config()['NUM_TO_FETCH'] - try: - # If they don't specify, make it 5. - num_to_fetch = request.GET.get('max', default_num_to_fetch) - num_to_fetch = int(num_to_fetch) - if not (1 <= num_to_fetch <= 100): - num_to_fetch = default_num_to_fetch - except ValueError: # If casting to an int fails. - num_to_fetch = default_num_to_fetch + unread_list = get_notification_list(request, 'unread') - unread_list = [] - - for notification in request.user.notifications.unread()[0:num_to_fetch]: - struct = model_to_dict(notification) - struct['slug'] = id2slug(notification.id) - if notification.actor: - struct['actor'] = str(notification.actor) - if notification.target: - struct['target'] = str(notification.target) - if notification.action_object: - struct['action_object'] = str(notification.action_object) - if notification.data: - struct['data'] = notification.data - unread_list.append(struct) - if request.GET.get('mark_as_read'): - notification.mark_as_read() data = { 'unread_count': request.user.notifications.unread().count(), 'unread_list': unread_list @@ -210,32 +188,8 @@ def live_all_notification_list(request): } return JsonResponse(data) - default_num_to_fetch = notification_settings.get_config()['NUM_TO_FETCH'] - try: - # If they don't specify, make it 5. - num_to_fetch = request.GET.get('max', default_num_to_fetch) - num_to_fetch = int(num_to_fetch) - if not (1 <= num_to_fetch <= 100): - num_to_fetch = default_num_to_fetch - except ValueError: # If casting to an int fails. - num_to_fetch = default_num_to_fetch + all_list = get_notification_list(request) - all_list = [] - - for notification in request.user.notifications.all()[0:num_to_fetch]: - struct = model_to_dict(notification) - struct['slug'] = id2slug(notification.id) - if notification.actor: - struct['actor'] = str(notification.actor) - if notification.target: - struct['target'] = str(notification.target) - if notification.action_object: - struct['action_object'] = str(notification.action_object) - if notification.data: - struct['data'] = notification.data - all_list.append(struct) - if request.GET.get('mark_as_read'): - notification.mark_as_read() data = { 'all_count': request.user.notifications.count(), 'all_list': all_list