Skip to content

Commit 1419234

Browse files
committed
feat(itim): Add Notes Serializer for Cluster model
ref: #824 #825
1 parent 550d4d9 commit 1419234

1 file changed

Lines changed: 87 additions & 0 deletions

File tree

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
from rest_framework import serializers
2+
3+
from drf_spectacular.utils import extend_schema_serializer
4+
5+
from access.serializers.organization import (TenantBaseSerializer)
6+
7+
from centurion.models.meta import ClusterCenturionModelNote # pylint: disable=E0401:import-error disable=E0611:no-name-in-module
8+
9+
from core.serializers.centurionmodelnote import ( # pylint: disable=W0611:unused-import
10+
BaseSerializer,
11+
ModelSerializer as BaseModelModelSerializer,
12+
ViewSerializer as BaseModelViewSerializer
13+
)
14+
15+
16+
17+
@extend_schema_serializer(component_name = 'ClusterModelNoteModelSerializer')
18+
class ModelSerializer(
19+
BaseModelModelSerializer,
20+
):
21+
22+
23+
_urls = serializers.SerializerMethodField('get_url')
24+
25+
def get_url(self, item) -> dict:
26+
27+
return {
28+
'_self': item.get_url( request = self._context['view'].request ),
29+
}
30+
31+
32+
class Meta:
33+
34+
model = ClusterCenturionModelNote
35+
36+
fields = [
37+
'id',
38+
'organization',
39+
'display_name',
40+
'body',
41+
'created_by',
42+
'modified_by',
43+
'content_type',
44+
'model',
45+
'created',
46+
'modified',
47+
'_urls',
48+
]
49+
50+
read_only_fields = [
51+
'id',
52+
'display_name',
53+
'organization',
54+
'created_by',
55+
'modified_by',
56+
'content_type',
57+
'model',
58+
'created',
59+
'modified',
60+
'_urls',
61+
]
62+
63+
64+
65+
def validate(self, attrs):
66+
67+
is_valid = False
68+
69+
note_model = self.Meta.model.model.field.related_model
70+
71+
attrs['model'] = note_model.objects.get(
72+
id = int( self.context['view'].kwargs['model_id'] )
73+
)
74+
75+
76+
is_valid = super().validate(attrs)
77+
78+
return is_valid
79+
80+
81+
@extend_schema_serializer(component_name = 'ClusterModelNoteViewSerializer')
82+
class ViewSerializer(
83+
ModelSerializer,
84+
BaseModelViewSerializer,
85+
):
86+
87+
organization = TenantBaseSerializer( many = False, read_only = True )

0 commit comments

Comments
 (0)