class NotificationViewList(ListView): template_name = 'notifications/list.html' context_object_name = 'notifications' paginate_by = settings.get_config()['PAGINATE_BY'] @method_decorator(login_required) def dispatch(self, request, *args, **kwargs): return super(NotificationViewList, self).dispatch( request, *args, **kwargs)
def delete(request, notification_id=None): notification = get_object_or_404( Notification, recipient=request.user, id=notification_id ) if settings.get_config()["SOFT_DELETE"]: notification.deleted = True notification.save() else: notification.delete() return Response(status=status.HTTP_200_OK)
def live_all_notification_list(request): ''' Return a json with a unread notification list ''' try: user_is_authenticated = request.user.is_authenticated() except TypeError: # Django >= 1.11 user_is_authenticated = request.user.is_authenticated if not user_is_authenticated: data = {'all_count': 0, 'unread_count': 0, 'all_list': []} return JsonResponse(data) 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 before_datetime = request.GET.get('before') all_notifications_qs = request.user.notifications.all() if before_datetime: all_notifications_qs = all_notifications_qs.filter( timestamp__lt=dateutil.parser.parse(before_datetime)) all_list = [] for notification in all_notifications_qs[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(), 'unread_count': request.user.notifications.unread().count(), 'all_list': all_list } return JsonResponse(data)
def deleteunread(request): notifications = Notification.objects.filter(unread=False) for notification in notifications: if settings.get_config()['SOFT_DELETE']: notification.deleted = True notification.save() else: notification.delete() _next = request.GET.get('next') if _next: return redirect(_next) return redirect('notifications:all')
def live_unread_notification_list(request): ''' Return a json with a unread notification list ''' try: user_is_authenticated = request.user.is_authenticated() except TypeError: # Django >= 1.11 user_is_authenticated = request.user.is_authenticated if not user_is_authenticated: data = { 'unread_count': 0, 'unread_list': [] } return JsonResponse(data) 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 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 struct['timestamp'] = arrow.get(struct['timestamp']).humanize(locale='ar') 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 } return JsonResponse(data)
def live_all_notification_list(request): ''' Return a json with a notification list ''' try: user_is_authenticated = request.user.is_authenticated() except TypeError: # Django >= 1.11 user_is_authenticated = request.user.is_authenticated print(user_is_authenticated) if not user_is_authenticated: data = { 'all_count': 0, 'all_list': [] } return JsonResponse(data) 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 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 if notification.verb: struct['verb'] = notification.verb all_list.append(struct) data = { 'all_count': request.user.notifications.count(), 'all_list': all_list } return HttpResponse(json.dumps(data))
def live_all_notification_list(request): ''' Return a json with a unread notification list ''' try: user_is_authenticated = request.user.is_authenticated() except TypeError: # Django >= 1.11 user_is_authenticated = request.user.is_authenticated if not user_is_authenticated: data = { 'all_count': 0, 'all_list': [] } return JsonResponse(data) 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 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 } return JsonResponse(data)
def delete(request, slug=None): notification_id = slug2id(slug) notification = get_object_or_404( Notification, recipient=request.user, id=notification_id) if settings.get_config()['SOFT_DELETE']: notification.deleted = True notification.save() else: notification.delete() _next = request.GET.get('next') if _next: return redirect(_next) return redirect('notifications:all')
def notifications_handler(request): ''' Return a json with a unread notification list ''' try: user_is_authenticated = request.user.is_authenticated() except TypeError: user_is_authenticated = request.user.is_authenticated if not user_is_authenticated: data = { 'unread_count_notifications': 0, 'unread_list_notifications': [] } return JsonResponse(data) default_num_to_fetch = get_config()['NUM_TO_FETCH'] try: 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: num_to_fetch = default_num_to_fetch unread_notifications = build_notification_data(user=request.user, notification_type=NotificationType.Notification.value, num_to_fetch=num_to_fetch, mark_as_read=request.GET.get('mark_as_read')) unread_messages = build_notification_data(user=request.user, notification_type=NotificationType.Message.value, num_to_fetch=num_to_fetch, mark_as_read=request.GET.get('mark_as_read')) data = { 'unread_count_notifications': unread_notifications[1], 'unread_list_notifications': unread_notifications[0], 'unread_count_messages': unread_messages[1], 'unread_list_messages': unread_messages[0] } return JsonResponse(data)
from django.utils.encoding import python_2_unicode_compatible from django.utils.six import text_type from jsonfield.fields import JSONField from model_utils import Choices from notifications import settings as notifications_settings from notifications.signals import notify from notifications.utils import id2slug from swapper import load_model if StrictVersion(get_version()) >= StrictVersion('1.8.0'): from django.contrib.contenttypes.fields import GenericForeignKey # noqa else: from django.contrib.contenttypes.generic import GenericForeignKey # noqa EXTRA_DATA = notifications_settings.get_config()['USE_JSONFIELD'] def is_soft_delete(): return notifications_settings.get_config()['SOFT_DELETE'] def assert_soft_delete(): if not is_soft_delete(): # msg = """To use 'deleted' field, please set 'SOFT_DELETE'=True in settings. # Otherwise NotificationQuerySet.unread and NotificationQuerySet.read do NOT filter by 'deleted' field. # """ msg = 'REVERTME' raise ImproperlyConfigured(msg)
def is_soft_delete(): return notifications_settings.get_config()['SOFT_DELETE']
from django.db.models.query import QuerySet from django.utils import timezone from django.utils.six import text_type from jsonfield.fields import JSONField from model_utils import Choices from notifications import settings as notifications_settings from notifications.signals import notify from notifications.utils import id2slug if StrictVersion(get_version()) >= StrictVersion('1.8.0'): from django.contrib.contenttypes.fields import GenericForeignKey # noqa else: from django.contrib.contenttypes.generic import GenericForeignKey # noqa EXTRA_DATA = notifications_settings.get_config()['USE_JSONFIELD'] def is_soft_delete(): return notifications_settings.get_config()['SOFT_DELETE'] def assert_soft_delete(): if not is_soft_delete(): msg = """To use 'deleted' field, please set 'SOFT_DELETE'=True in settings. Otherwise NotificationQuerySet.unread and NotificationQuerySet.read do NOT filter by 'deleted' field. """ raise ImproperlyConfigured(msg) class NotificationQuerySet(models.query.QuerySet):
def get_queryset(self): if settings.get_config()['SOFT_DELETE']: qset = self.request.user.notifications.active() else: qset = self.request.user.notifications.all() return qset
from rest_framework.decorators import ( api_view, authentication_classes, permission_classes, ) from rest_framework.permissions import IsAuthenticated from rest_framework.response import Response from rest_framework_simplejwt.authentication import JWTAuthentication from notifications import settings from notifications.models import Notification from notifications.settings import get_config from .pagination import MyPageNumberPagination NotificationSerializer = import_string(settings.get_config()["SERIALIZER_CLASS"]) @api_view(["GET"]) @authentication_classes([JWTAuthentication, SessionAuthentication, TokenAuthentication]) @permission_classes([IsAuthenticated]) def mark_all_as_read(request): request.user.notifications.mark_all_as_read() return Response(status=status.HTTP_200_OK) @api_view(["GET"]) @authentication_classes([JWTAuthentication, SessionAuthentication, TokenAuthentication]) @permission_classes([IsAuthenticated]) def mark_all_as_unread(request):