|
15 | 15 | from readthedocs.core.resolver import Resolver
|
16 | 16 | from readthedocs.core.utils import slugify
|
17 | 17 | from readthedocs.core.utils.extend import SettingsOverrideObject
|
| 18 | +from readthedocs.notifications.models import Notification |
18 | 19 | from readthedocs.oauth.models import RemoteOrganization, RemoteRepository
|
19 | 20 | from readthedocs.organizations.models import Organization, Team
|
20 | 21 | from readthedocs.projects.constants import (
|
@@ -60,10 +61,33 @@ class Meta:
|
60 | 61 | fields = []
|
61 | 62 |
|
62 | 63 |
|
| 64 | +# TODO: decide whether or not include a `_links` field on the object |
| 65 | +# |
| 66 | +# This also includes adding `/api/v3/notifications/<pk>` endpoint, |
| 67 | +# which I'm not sure it's useful at this point. |
| 68 | +# |
| 69 | +# class NotificationLinksSerializer(BaseLinksSerializer): |
| 70 | +# _self = serializers.SerializerMethodField() |
| 71 | +# attached_to = serializers.SerializerMethodField() |
| 72 | + |
| 73 | +# def get__self(self, obj): |
| 74 | +# path = reverse( |
| 75 | +# "notifications-detail", |
| 76 | +# kwargs={ |
| 77 | +# "pk": obj.pk, |
| 78 | +# }, |
| 79 | +# ) |
| 80 | +# return self._absolute_url(path) |
| 81 | + |
| 82 | +# def get_attached_to(self, obj): |
| 83 | +# return None |
| 84 | + |
| 85 | + |
63 | 86 | class BuildLinksSerializer(BaseLinksSerializer):
|
64 | 87 | _self = serializers.SerializerMethodField()
|
65 | 88 | version = serializers.SerializerMethodField()
|
66 | 89 | project = serializers.SerializerMethodField()
|
| 90 | + notifications = serializers.SerializerMethodField() |
67 | 91 |
|
68 | 92 | def get__self(self, obj):
|
69 | 93 | path = reverse(
|
@@ -96,6 +120,16 @@ def get_project(self, obj):
|
96 | 120 | )
|
97 | 121 | return self._absolute_url(path)
|
98 | 122 |
|
| 123 | + def get_notifications(self, obj): |
| 124 | + path = reverse( |
| 125 | + "project-builds-notifications-list", |
| 126 | + kwargs={ |
| 127 | + "parent_lookup_project__slug": obj.project.slug, |
| 128 | + "parent_lookup_build__id": obj.pk, |
| 129 | + }, |
| 130 | + ) |
| 131 | + return self._absolute_url(path) |
| 132 | + |
99 | 133 |
|
100 | 134 | class BuildURLsSerializer(BaseLinksSerializer, serializers.Serializer):
|
101 | 135 | build = serializers.URLField(source="get_full_url")
|
@@ -190,6 +224,57 @@ def get_success(self, obj):
|
190 | 224 | return None
|
191 | 225 |
|
192 | 226 |
|
| 227 | +class NotificationMessageSerializer(serializers.Serializer): |
| 228 | + id = serializers.SlugField() |
| 229 | + header = serializers.CharField(source="get_rendered_header") |
| 230 | + body = serializers.CharField(source="get_rendered_body") |
| 231 | + type = serializers.CharField() |
| 232 | + icon_classes = serializers.CharField(source="get_display_icon_classes") |
| 233 | + |
| 234 | + class Meta: |
| 235 | + fields = [ |
| 236 | + "id", |
| 237 | + "header", |
| 238 | + "body", |
| 239 | + "type", |
| 240 | + "icon_classes", |
| 241 | + ] |
| 242 | + |
| 243 | + |
| 244 | +class NotificationCreateSerializer(serializers.ModelSerializer): |
| 245 | + class Meta: |
| 246 | + model = Notification |
| 247 | + fields = [ |
| 248 | + "message_id", |
| 249 | + "dismissable", |
| 250 | + "news", |
| 251 | + "state", |
| 252 | + ] |
| 253 | + |
| 254 | + |
| 255 | +class NotificationSerializer(serializers.ModelSerializer): |
| 256 | + message = NotificationMessageSerializer(source="get_message") |
| 257 | + attached_to_content_type = serializers.SerializerMethodField() |
| 258 | + # TODO: review these fields |
| 259 | + # _links = BuildLinksSerializer(source="*") |
| 260 | + # urls = BuildURLsSerializer(source="*") |
| 261 | + |
| 262 | + class Meta: |
| 263 | + model = Notification |
| 264 | + fields = [ |
| 265 | + "id", |
| 266 | + "state", |
| 267 | + "dismissable", |
| 268 | + "news", |
| 269 | + "attached_to_content_type", |
| 270 | + "attached_to_id", |
| 271 | + "message", |
| 272 | + ] |
| 273 | + |
| 274 | + def get_attached_to_content_type(self, obj): |
| 275 | + return obj.attached_to_content_type.name |
| 276 | + |
| 277 | + |
193 | 278 | class VersionLinksSerializer(BaseLinksSerializer):
|
194 | 279 | _self = serializers.SerializerMethodField()
|
195 | 280 | builds = serializers.SerializerMethodField()
|
|
0 commit comments