-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5ac5135
commit 865619b
Showing
33 changed files
with
959 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
from drf_spectacular.utils import extend_schema_field | ||
from rest_framework import serializers | ||
|
||
from core.models import ObjectType | ||
from extras.models import Notification, 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', 'kind', 'event', | ||
] | ||
brief_fields = ('id', 'url', 'display', 'object_type', 'object_id', 'user', 'kind', 'event') | ||
|
||
@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 = Notification | ||
fields = [ | ||
'id', 'url', 'display', 'name', 'description', 'object', '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_id', 'object_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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.