Add helpers.py file with some helper functions to ensure DRY principle

This commit is contained in:
nahid 2020-03-02 12:31:19 +06:00 committed by Alvaro Mariano
parent 28c49b801a
commit 0474c884e9
2 changed files with 41 additions and 53 deletions

34
notifications/helpers.py Normal file
View file

@ -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

View file

@ -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