mirror of
https://gitlab.com/allianceauth/allianceauth.git
synced 2026-02-06 15:16:20 +01:00
Front-end notification system
Currently only creates notifications for logging events Addresses #75
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
from django.contrib import admin
|
||||
from.models import Notification
|
||||
|
||||
# Register your models here.
|
||||
admin.site.register(Notification)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from .models import Notification
|
||||
|
||||
def user_notification_count(request):
|
||||
return len(Notification.objects.filter(user=request.user).filter(viewed=False))
|
||||
return {'notifications':len(Notification.objects.filter(user__id=request.user.id).filter(viewed=False))}
|
||||
|
||||
@@ -2,13 +2,13 @@ import logging
|
||||
from django.contrib.auth.models import User
|
||||
from .models import Notification
|
||||
|
||||
def NotificationHandler(logging.Handler):
|
||||
class NotificationHandler(logging.Handler):
|
||||
def emit(self, record):
|
||||
for user in User.objects.all():
|
||||
if user.has_perm('auth.logging_notifications'):
|
||||
notif = Notification()
|
||||
notif.user = user
|
||||
notif.title = "%s [%s:%s]" % (record.levelname, record.funcName, record.lineno)
|
||||
notif.level = str([item[0] for item in Notification.LEVEL_CHOICES if item[1] == record.levelname])
|
||||
notif.level = str([item[0] for item in Notification.LEVEL_CHOICES if item[1] == record.levelname][0])
|
||||
notif.message = record.getMessage()
|
||||
notif.save()
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
from django.db import models
|
||||
from django.contrib.auth.models import User
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
class Notification(models.Model):
|
||||
|
||||
@@ -15,8 +18,8 @@ class Notification(models.Model):
|
||||
level = models.CharField(choices=LEVEL_CHOICES, max_length=10)
|
||||
title = models.CharField(max_length=254)
|
||||
message = models.TextField()
|
||||
created = models.DateTimeField(auto_now_add=True)
|
||||
viewed = models.BooleanField()
|
||||
timestamp = models.DateTimeField(auto_now_add=True)
|
||||
viewed = models.BooleanField(default=False)
|
||||
|
||||
def view(self):
|
||||
logger.info("Marking notification as viewed: %s" % self)
|
||||
@@ -26,3 +29,6 @@ class Notification(models.Model):
|
||||
def __unicode__(self):
|
||||
output = "%s: %s" % (self.user, self.title)
|
||||
return output.encode('utf-8')
|
||||
|
||||
def set_level(self, level):
|
||||
self.level = [item[0] for item in self.LEVEL_CHOICES if item[1] == level][0]
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
from django.shortcuts import render, get_object_or_404
|
||||
from django.shortcuts import render, get_object_or_404, redirect
|
||||
from .models import Notification
|
||||
from django.contrib.auth.decorators import login_required
|
||||
import logging
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
@login_required
|
||||
def notification_list(request):
|
||||
logger.debug("notification_list called by user %s" % request.user)
|
||||
new_notifs = Notification.objects.filter(user=request.user).filter(read=False)
|
||||
old_notifs = Notification.objects.filter(user=request.user).filter(read=True)
|
||||
new_notifs = Notification.objects.filter(user=request.user).filter(viewed=False)
|
||||
old_notifs = Notification.objects.filter(user=request.user).filter(viewed=True)
|
||||
logger.debug("User %s has %s unread and %s read notifications" % (request.user, len(new_notifs), len(old_notifs)))
|
||||
context = {
|
||||
'read': old_notifs,
|
||||
@@ -16,11 +20,12 @@ def notification_list(request):
|
||||
@login_required
|
||||
def notification_view(request, notif_id):
|
||||
logger.debug("notification_view called by user %s for notif_id %s" % (request.user, notif_id))
|
||||
notif = get_object_or_404(notification, pk=notif_id)
|
||||
notif = get_object_or_404(Notification, pk=notif_id)
|
||||
if notif.user == request.user:
|
||||
logger.debug("Providing notification for user %s" % request.user)
|
||||
context = {'notification': notif}
|
||||
context = {'notif': notif}
|
||||
notif.view()
|
||||
return render(request, 'registered/notification_view.html', context)
|
||||
else:
|
||||
logger.warn("User %s not authorized to view notif_id %s belonging to user %s" % (request.user, notif_id, notif.user))
|
||||
return redirect('auth_notification_list')
|
||||
|
||||
Reference in New Issue
Block a user