mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-20 02:06:42 -06:00

* Initial work on #15621 * Signal receiver should ignore models which don't support notifications * Flesh out NotificationGroup functionality * Add NotificationGroup filters for users & groups * Separate read & dimiss actions * Enable one-click dismissals from notifications list * Include total notification count in dropdown * Drop 'kind' field from Notification model * Register event types in the registry; add colors & icons * Enable event rules to target notification groups * Define dynamic choices for Notification.event_name * Move event registration to core * Add more job events * Misc cleanup * Misc cleanup * Correct absolute URLs for notifications & subscriptions * Optimize subscriber notifications * Use core event types when queuing events * Standardize queued event attribute to event_type; change content_type to object_type * Rename Notification.event_name to event_type * Restore NotificationGroupBulkEditView * Add API tests * Add view & filterset tests * Add model documentation * Fix tests * Update notification bell when notifications have been cleared * Ensure subscribe button appears only on relevant models * Notifications/subscriptions cannot be ordered by object * Misc cleanup * Add event icon & type to notifications table * Adjust icon sizing * Mute color of read notifications * Misc cleanup
83 lines
2.9 KiB
Python
83 lines
2.9 KiB
Python
from drf_spectacular.utils import extend_schema_field
|
|
from rest_framework import serializers
|
|
|
|
from core.models import ObjectType
|
|
from extras.models import Notification, NotificationGroup, Subscription
|
|
from netbox.api.fields import ContentTypeField, SerializedPKRelatedField
|
|
from netbox.api.serializers import ValidatedModelSerializer
|
|
from users.api.serializers_.users import GroupSerializer, UserSerializer
|
|
from users.models import Group, User
|
|
from utilities.api import get_serializer_for_model
|
|
|
|
__all__ = (
|
|
'NotificationSerializer',
|
|
'NotificationGroupSerializer',
|
|
'SubscriptionSerializer',
|
|
)
|
|
|
|
|
|
class NotificationSerializer(ValidatedModelSerializer):
|
|
object_type = ContentTypeField(
|
|
queryset=ObjectType.objects.with_feature('notifications'),
|
|
)
|
|
object = serializers.SerializerMethodField(read_only=True)
|
|
user = UserSerializer(nested=True)
|
|
|
|
class Meta:
|
|
model = Notification
|
|
fields = [
|
|
'id', 'url', 'display', 'object_type', 'object_id', 'object', 'user', 'created', 'read', 'event_type',
|
|
]
|
|
brief_fields = ('id', 'url', 'display', 'object_type', 'object_id', 'user', 'read', 'event_type')
|
|
|
|
@extend_schema_field(serializers.JSONField(allow_null=True))
|
|
def get_object(self, instance):
|
|
serializer = get_serializer_for_model(instance.object)
|
|
context = {'request': self.context['request']}
|
|
return serializer(instance.object, nested=True, context=context).data
|
|
|
|
|
|
class NotificationGroupSerializer(ValidatedModelSerializer):
|
|
groups = SerializedPKRelatedField(
|
|
queryset=Group.objects.all(),
|
|
serializer=GroupSerializer,
|
|
nested=True,
|
|
required=False,
|
|
many=True
|
|
)
|
|
users = SerializedPKRelatedField(
|
|
queryset=User.objects.all(),
|
|
serializer=UserSerializer,
|
|
nested=True,
|
|
required=False,
|
|
many=True
|
|
)
|
|
|
|
class Meta:
|
|
model = NotificationGroup
|
|
fields = [
|
|
'id', 'url', 'display', 'display_url', 'name', 'description', 'groups', 'users',
|
|
]
|
|
brief_fields = ('id', 'url', 'display', 'name', 'description')
|
|
|
|
|
|
class SubscriptionSerializer(ValidatedModelSerializer):
|
|
object_type = ContentTypeField(
|
|
queryset=ObjectType.objects.with_feature('notifications'),
|
|
)
|
|
object = serializers.SerializerMethodField(read_only=True)
|
|
user = UserSerializer(nested=True)
|
|
|
|
class Meta:
|
|
model = Subscription
|
|
fields = [
|
|
'id', 'url', 'display', 'object_type', 'object_id', 'object', 'user', 'created',
|
|
]
|
|
brief_fields = ('id', 'url', 'display', 'object_type', 'object_id', 'user')
|
|
|
|
@extend_schema_field(serializers.JSONField(allow_null=True))
|
|
def get_object(self, instance):
|
|
serializer = get_serializer_for_model(instance.object)
|
|
context = {'request': self.context['request']}
|
|
return serializer(instance.object, nested=True, context=context).data
|