mirror of
https://gitlab.com/allianceauth/allianceauth.git
synced 2025-07-09 20:40:17 +02:00
Fix logging notifications
This commit is contained in:
parent
a7f468efd1
commit
a0db8e8e2c
@ -12,21 +12,20 @@ class NotificationHandler(logging.Handler):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
perm = Permission.objects.get(codename="logging_notifications")
|
perm = Permission.objects.get(codename="logging_notifications")
|
||||||
|
|
||||||
message = record.getMessage()
|
|
||||||
if record.exc_text:
|
|
||||||
message += "\n\n"
|
|
||||||
message = message + record.exc_text
|
|
||||||
|
|
||||||
users = User.objects.filter(
|
|
||||||
Q(groups__permissions=perm) | Q(user_permissions=perm) | Q(is_superuser=True)).distinct()
|
|
||||||
|
|
||||||
for user in users:
|
|
||||||
notify(
|
|
||||||
user,
|
|
||||||
"%s [%s:%s]" % (record.levelname, record.funcName, record.lineno),
|
|
||||||
level=str([item[0] for item in Notification.LEVEL_CHOICES if item[1] == record.levelname][0]),
|
|
||||||
message=message
|
|
||||||
)
|
|
||||||
except Permission.DoesNotExist:
|
except Permission.DoesNotExist:
|
||||||
pass
|
return
|
||||||
|
|
||||||
|
message = record.getMessage()
|
||||||
|
if record.exc_text:
|
||||||
|
message += "\n\n"
|
||||||
|
message = message + record.exc_text
|
||||||
|
|
||||||
|
users = User.objects.filter(
|
||||||
|
Q(groups__permissions=perm) | Q(user_permissions=perm) | Q(is_superuser=True)).distinct()
|
||||||
|
for user in users:
|
||||||
|
notify(
|
||||||
|
user,
|
||||||
|
"%s [%s:%s]" % (record.levelname, record.funcName, record.lineno),
|
||||||
|
level=Notification.Level.from_old_name(record.levelname),
|
||||||
|
message=message
|
||||||
|
)
|
||||||
|
69
allianceauth/notifications/tests/test_handlers.py
Normal file
69
allianceauth/notifications/tests/test_handlers.py
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
from logging import LogRecord, DEBUG
|
||||||
|
|
||||||
|
from django.contrib.auth.models import Permission, Group, User
|
||||||
|
from django.test import TestCase
|
||||||
|
from allianceauth.tests.auth_utils import AuthUtils
|
||||||
|
|
||||||
|
from ..handlers import NotificationHandler
|
||||||
|
from ..models import Notification
|
||||||
|
|
||||||
|
MODULE_PATH = 'allianceauth.notifications.handlers'
|
||||||
|
|
||||||
|
|
||||||
|
class TestHandler(TestCase):
|
||||||
|
def test_do_nothing_if_permission_does_not_exist(self):
|
||||||
|
# given
|
||||||
|
Permission.objects.get(codename="logging_notifications").delete()
|
||||||
|
handler = NotificationHandler()
|
||||||
|
record = LogRecord(
|
||||||
|
name="name",
|
||||||
|
level=DEBUG,
|
||||||
|
pathname="pathname",
|
||||||
|
lineno=42,
|
||||||
|
msg="msg",
|
||||||
|
args=[],
|
||||||
|
exc_info=None,
|
||||||
|
func="func"
|
||||||
|
)
|
||||||
|
# when
|
||||||
|
handler.emit(record)
|
||||||
|
# then
|
||||||
|
self.assertEqual(Notification.objects.count(), 0)
|
||||||
|
|
||||||
|
def test_should_emit_message_to_users_with_permission_only(self):
|
||||||
|
# given
|
||||||
|
AuthUtils.create_user('Lex Luthor')
|
||||||
|
user_permission = AuthUtils.create_user('Bruce Wayne')
|
||||||
|
user_permission = AuthUtils.add_permission_to_user_by_name(
|
||||||
|
"auth.logging_notifications", user_permission
|
||||||
|
)
|
||||||
|
group = Group.objects.create(name="Dummy Group")
|
||||||
|
perm = Permission.objects.get(codename="logging_notifications")
|
||||||
|
group.permissions.add(perm)
|
||||||
|
user_group = AuthUtils.create_user('Peter Parker')
|
||||||
|
user_group.groups.add(group)
|
||||||
|
user_superuser = User.objects.create_superuser("Clark Kent")
|
||||||
|
handler = NotificationHandler()
|
||||||
|
record = LogRecord(
|
||||||
|
name="name",
|
||||||
|
level=DEBUG,
|
||||||
|
pathname="pathname",
|
||||||
|
lineno=42,
|
||||||
|
msg="msg",
|
||||||
|
args=[],
|
||||||
|
exc_info=None,
|
||||||
|
func="func"
|
||||||
|
)
|
||||||
|
# when
|
||||||
|
handler.emit(record)
|
||||||
|
# then
|
||||||
|
self.assertEqual(Notification.objects.count(), 3)
|
||||||
|
users = set(Notification.objects.values_list("user__pk", flat=True))
|
||||||
|
self.assertSetEqual(
|
||||||
|
users, {user_permission.pk, user_group.pk, user_superuser.pk}
|
||||||
|
)
|
||||||
|
notif = Notification.objects.first()
|
||||||
|
self.assertEqual(notif.user, user_permission)
|
||||||
|
self.assertEqual(notif.title, "DEBUG [func:42]")
|
||||||
|
self.assertEqual(notif.level, "success")
|
||||||
|
self.assertEqual(notif.message, "msg")
|
Loading…
x
Reference in New Issue
Block a user