forked from dmm-com/pagoda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserializers.py
1374 lines (1111 loc) · 49.4 KB
/
serializers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
from datetime import date, datetime
from typing import Any, Literal
from django.db.models import Prefetch
from drf_spectacular.utils import extend_schema_field, extend_schema_serializer
from pydantic import BaseModel, RootModel
from rest_framework import serializers
from rest_framework.exceptions import PermissionDenied, ValidationError
from typing_extensions import TypedDict
from acl.models import ACLBase
from airone.lib import custom_view, drf
from airone.lib.acl import ACLType
from airone.lib.drf import (
DuplicatedObjectExistsError,
IncorrectTypeError,
InvalidValueError,
ObjectNotExistsError,
RequiredParameterError,
)
from airone.lib.elasticsearch import FilterKey
from airone.lib.log import Logger
from airone.lib.types import AttrDefaultValue, AttrType
from entity.api_v2.serializers import EntitySerializer
from entity.models import Entity, EntityAttr
from entry.models import AliasEntry, Attribute, AttributeValue, Entry
from entry.settings import CONFIG as CONFIG_ENTRY
from group.models import Group
from job.models import Job, JobStatus
from role.models import Role
from user.api_v2.serializers import UserBaseSerializer
from user.models import User
class ExportedEntryAttributeValueObject(BaseModel):
entity: str
name: str
ExportedEntryAttributePrimitiveValue = (
str # includes text, string, group, role
| date
| datetime
| bool
| ExportedEntryAttributeValueObject
| dict[str, ExportedEntryAttributeValueObject] # named entry for yaml export
| dict[str, str] # named entry for csv export
| dict[str, None]
| None
)
ExportedEntryAttributeValue = (
ExportedEntryAttributePrimitiveValue | list[ExportedEntryAttributePrimitiveValue]
)
class ExportedEntryAttribute(BaseModel):
name: str
value: ExportedEntryAttributeValue
class ReferralEntry(BaseModel):
entity: str
entry: str
class ExportedEntry(BaseModel):
name: str
attrs: list[ExportedEntryAttribute]
referrals: list[ReferralEntry] | None = None
class ExportedEntityEntries(BaseModel):
entity: str
entries: list[ExportedEntry]
class ExportTaskParams(BaseModel):
export_format: Literal["yaml", "csv"]
target_id: int
class EntityAttributeType(TypedDict):
id: int
name: str
class EntryAttributeValueObject(TypedDict):
id: int
name: str
schema: EntityAttributeType
class EntryAttributeValueNamedObject(TypedDict):
name: str
object: EntryAttributeValueObject | None
class EntryAttributeValueNamedObjectBoolean(EntryAttributeValueNamedObject):
boolean: bool
class EntryAttributeValueBoolean(TypedDict):
boolean: bool
class EntryAttributeValueGroup(TypedDict):
id: int
name: str
class EntryAttributeValueRole(TypedDict):
id: int
name: str
# A thin container returns typed value(s)
class EntryAttributeValue(TypedDict, total=False):
as_object: EntryAttributeValueObject | None
as_string: str
as_named_object: EntryAttributeValueNamedObject
as_array_object: list[EntryAttributeValueObject | None]
as_array_string: list[str]
as_array_named_object: list[EntryAttributeValueNamedObject]
as_array_group: list[EntryAttributeValueGroup]
# text; use string instead
as_boolean: bool
as_group: EntryAttributeValueGroup | None
# date; use string instead
as_role: EntryAttributeValueRole | None
as_array_role: list[EntryAttributeValueRole]
class EntryAttributeType(TypedDict):
id: int | None
type: int
is_mandatory: bool
is_readable: bool
value: EntryAttributeValue
schema: EntityAttributeType
class AdvancedSearchJoinAttrAttrInfo(BaseModel):
name: str
keyword: str | None = None
filter_key: FilterKey | None = None
class AdvancedSearchJoinAttrInfo(BaseModel):
name: str
offset: int = 0
attrinfo: list[AdvancedSearchJoinAttrAttrInfo] = []
AdvancedSearchJoinAttrInfoList = RootModel[list[AdvancedSearchJoinAttrInfo]]
class EntityAttributeTypeSerializer(serializers.Serializer):
id = serializers.IntegerField()
name = serializers.CharField()
class EntryAttributeValueObjectSerializer(serializers.Serializer):
id = serializers.IntegerField()
name = serializers.CharField()
schema = EntityAttributeTypeSerializer()
class EntryAttributeValueNamedObjectSerializer(serializers.Serializer):
name = serializers.CharField()
object = EntryAttributeValueObjectSerializer(allow_null=True)
class EntryAttributeValueNamedObjectBooleanSerializer(EntryAttributeValueNamedObjectSerializer):
name = serializers.CharField()
object = EntryAttributeValueObjectSerializer(allow_null=True)
boolean = serializers.BooleanField()
class EntryAttributeValueGroupSerializer(serializers.Serializer):
id = serializers.IntegerField()
name = serializers.CharField()
class EntryAttributeValueRoleSerializer(serializers.Serializer):
id = serializers.IntegerField()
name = serializers.CharField()
class EntryAttributeValueSerializer(serializers.Serializer):
as_object = EntryAttributeValueObjectSerializer(allow_null=True, required=False)
as_string = serializers.CharField(required=False)
as_named_object = EntryAttributeValueNamedObjectSerializer(required=False)
as_array_object = serializers.ListField(
child=EntryAttributeValueObjectSerializer(allow_null=True), required=False
)
as_array_string = serializers.ListField(child=serializers.CharField(), required=False)
as_array_named_object = serializers.ListField(
child=EntryAttributeValueNamedObjectBooleanSerializer(),
required=False,
)
as_array_group = serializers.ListField(
child=EntryAttributeValueGroupSerializer(), required=False
)
# text; use string instead
as_boolean = serializers.BooleanField(required=False)
as_group = EntryAttributeValueGroupSerializer(allow_null=True, required=False)
# date; use string instead
as_role = EntryAttributeValueRoleSerializer(allow_null=True, required=False)
as_array_role = serializers.ListField(child=EntryAttributeValueRoleSerializer(), required=False)
class EntryAttributeTypeSerializer(serializers.Serializer):
@extend_schema_field(
{
"type": "integer",
"enum": [k.value for k in AttrType],
"x-enum-varnames": [k.name for k in AttrType],
}
)
class AttrTypeField(serializers.IntegerField):
pass
id = serializers.IntegerField(allow_null=True)
type = AttrTypeField()
is_mandatory = serializers.BooleanField()
is_readable = serializers.BooleanField()
value = EntryAttributeValueSerializer()
schema = EntityAttributeTypeSerializer()
class EntryAliasSerializer(serializers.ModelSerializer):
class Meta:
model = AliasEntry
fields = [
"id",
"name",
"entry",
]
def validate(self, params):
if not params["entry"].schema.is_available(params["name"]):
raise DuplicatedObjectExistsError("A duplicated named Alias exists in this model")
return params
class EntryBaseSerializer(serializers.ModelSerializer):
# This attribute toggle privileged mode that allow user to CRUD Entry without
# considering permission. This must not change from program, but declare in a
# serializer.
privileged_mode = False
schema = EntitySerializer(read_only=True)
deleted_user = UserBaseSerializer(read_only=True, allow_null=True)
aliases = EntryAliasSerializer(many=True, read_only=True)
class Meta:
model = Entry
fields = [
"id",
"name",
"schema",
"is_active",
"deleted_user",
"deleted_time",
"updated_time",
"aliases",
]
extra_kwargs = {
"id": {"read_only": True},
"name": {"read_only": True},
"is_active": {"read_only": True},
}
def validate_name(self, name: str):
if self.instance:
schema = self.instance.schema
else:
schema = self.get_initial()["schema"]
# Check there is another Item that has same name
if name and Entry.objects.filter(name=name, schema=schema, is_active=True).exists():
# In update case, there is no problem with the same name
if not (self.instance and self.instance.name == name):
raise DuplicatedObjectExistsError("specified name(%s) already exists" % name)
if "\t" in name:
raise InvalidValueError("Names containing tab characters cannot be specified.")
return name
def _validate(self, schema: Entity, name: str, attrs: list[dict[str, Any]]):
user: User | None = None
if "request" in self.context:
user = self.context["request"].user
if "_user" in self.context:
user = self.context["_user"]
# In create case, check attrs mandatory attribute
if not self.instance:
if user is None:
raise RequiredParameterError("user is required")
for mandatory_attr in schema.attrs.filter(is_mandatory=True, is_active=True):
if not user.has_permission(mandatory_attr, ACLType.Writable):
raise PermissionDenied(
"mandatory attrs id(%s) is permission denied" % mandatory_attr.id
)
if mandatory_attr.id not in [attr["id"] for attr in attrs]:
raise RequiredParameterError(
"mandatory attrs id(%s) is not specified" % mandatory_attr.id
)
exclude_items = [self.instance.id] if self.instance else []
# Check there is another Alias that has same name
if not schema.is_available(name, exclude_items):
raise DuplicatedObjectExistsError("A duplicated named Alias exists in this model")
# check attrs
for attr in attrs:
# check attrs id
entity_attr = schema.attrs.filter(id=attr["id"], is_active=True).first()
if not entity_attr:
raise ObjectNotExistsError("attrs id(%s) does not exist" % attr["id"])
# check attrs value
(is_valid, msg) = AttributeValue.validate_attr_value(
entity_attr.type, attr["value"], entity_attr.is_mandatory
)
if not is_valid:
raise IncorrectTypeError("attrs id(%s) - %s" % (attr["id"], msg))
# check custom validate
if custom_view.is_custom("validate_entry", schema.name):
custom_view.call_custom(
"validate_entry", schema.name, user, schema.name, name, attrs, self.instance
)
def get_aliases(self, obj: Entry):
return obj.aliases.all()
class EntrySearchSerializer(EntryBaseSerializer):
class Meta:
model = Entry
fields = [
"id",
"name",
"schema",
]
@extend_schema_field({})
class AttributeValueField(serializers.Field):
def to_internal_value(self, data):
return data
class AttributeDataSerializer(serializers.Serializer):
id = serializers.IntegerField()
value = AttributeValueField(allow_null=True)
class EntryCreateData(TypedDict, total=False):
name: str
schema: Entity
attrs: list[AttributeDataSerializer]
created_user: User
@extend_schema_serializer(exclude_fields=["schema"])
class EntryCreateSerializer(EntryBaseSerializer):
schema = serializers.PrimaryKeyRelatedField(
queryset=Entity.objects.all(), write_only=True, required=True
)
attrs = serializers.ListField(child=AttributeDataSerializer(), write_only=True, required=False)
created_user = serializers.HiddenField(default=drf.AironeUserDefault())
class Meta:
model = Entry
fields = ["id", "name", "schema", "attrs", "created_user"]
def validate(self, params):
self._validate(params["schema"], params["name"], params.get("attrs", []))
return params
def create(self, validated_data: EntryCreateData):
user: User | None = None
if "request" in self.context:
user = self.context["request"].user
if "_user" in self.context:
user = self.context["_user"]
if user is None:
raise RequiredParameterError("user is required")
entity_name = validated_data["schema"].name
if custom_view.is_custom("before_create_entry_v2", entity_name):
validated_data = custom_view.call_custom(
"before_create_entry_v2", entity_name, user, validated_data
)
attrs_data = validated_data.pop("attrs", [])
entry: Entry = Entry(**validated_data, status=Entry.STATUS_CREATING)
# for history record
entry._history_user = user
entry.save()
for entity_attr in entry.schema.attrs.filter(is_active=True):
attr: Attribute = entry.add_attribute_from_base(entity_attr, user)
# skip for unpermitted attributes
if not self.privileged_mode and not user.has_permission(attr, ACLType.Writable):
continue
# make an initial AttributeValue object if the initial value is specified
attr_data = [x for x in attrs_data if int(x["id"]) == entity_attr.id]
if not attr_data:
continue
attr.add_value(user, attr_data[0]["value"])
if custom_view.is_custom("after_create_entry_v2", entity_name):
custom_view.call_custom("after_create_entry_v2", entity_name, user, entry)
# register entry information to Elasticsearch
entry.register_es()
# run task that may run TriggerAction in response to TriggerCondition configuration
Job.new_invoke_trigger(user, entry, attrs_data).run()
# clear flag to specify this entry has been completed to create
entry.del_status(Entry.STATUS_CREATING)
# Send notification to the webhook URL
job_notify_event: Job = Job.new_notify_create_entry(user, entry)
job_notify_event.run()
return entry
class PrivilegedEntryCreateSerializer(EntryCreateSerializer):
privileged_mode = True
class EntryUpdateData(TypedDict, total=False):
name: str
attrs: list[AttributeDataSerializer]
delay_trigger: bool
call_stacks: list[int]
class EntryUpdateSerializer(EntryBaseSerializer):
attrs = serializers.ListField(child=AttributeDataSerializer(), write_only=True, required=False)
# These parameters are only used to run TriggerActions
delay_trigger = serializers.BooleanField(required=False, default=True)
# This will contain EntityAttr IDs that have already been updated in this TriggerAction
# running chain.
call_stacks = serializers.ListField(child=serializers.IntegerField(), required=False)
class Meta:
model = Entry
fields = ["id", "name", "attrs", "delay_trigger", "call_stacks"]
extra_kwargs = {
"name": {"required": False},
}
def validate(self, params):
self._validate(
self.instance.schema, params.get("name", self.instance.name), params.get("attrs", [])
)
return params
def update(self, entry: Entry, validated_data: EntryUpdateData):
entry.set_status(Entry.STATUS_EDITING)
user: User | None = None
if "request" in self.context:
user = self.context["request"].user
if "_user" in self.context:
user = self.context["_user"]
if user is None:
raise RequiredParameterError("user is required")
# for history record
entry._history_user = user
entity_name = entry.schema.name
if custom_view.is_custom("before_update_entry_v2", entity_name):
validated_data = custom_view.call_custom(
"before_update_entry_v2", entity_name, user, validated_data, entry
)
attrs_data = validated_data.pop("attrs", [])
is_updated = False
# update name of Entry object. If name would be updated, the elasticsearch data of entries
# that refers this entry also be updated by creating REGISTERED_REFERRALS task.
job_register_referrals: Job | None = None
if "name" in validated_data and entry.name != validated_data["name"]:
entry.name = validated_data["name"]
entry.save(update_fields=["name"])
is_updated = True
job_register_referrals = Job.new_register_referrals(user, entry)
for entity_attr in entry.schema.attrs.filter(is_active=True):
attr: Attribute = entry.attrs.filter(schema=entity_attr, is_active=True).first()
if not attr:
attr = entry.add_attribute_from_base(entity_attr, user)
# skip for unpermitted attributes
if not self.privileged_mode and not user.has_permission(attr, ACLType.Writable):
continue
# make AttributeValue object if the value is specified
attr_data = [x for x in attrs_data if int(x["id"]) == entity_attr.id]
if not attr_data:
continue
# Check a new update value is specified, or not
if not attr.is_updated(attr_data[0]["value"]):
continue
attr.add_value(user, attr_data[0]["value"])
is_updated = True
if custom_view.is_custom("after_update_entry_v2", entity_name):
custom_view.call_custom("after_update_entry_v2", entity_name, user, entry)
# update entry information to Elasticsearch
if is_updated:
entry.register_es()
# run task that may run TriggerAction in response to TriggerCondition configuration
if validated_data["delay_trigger"]:
Job.new_invoke_trigger(user, entry, attrs_data).run()
else:
# This declaration prevents circular reference because TriggerAction module
# imports this module indirectly. And this might affect little negative affect
# because Python interpreter will cache imported module once it's imported.
from trigger.models import TriggerCondition
# run TriggerActions immediately if it's necessary
for action in TriggerCondition.get_invoked_actions(entry.schema, attrs_data):
action.run(user, entry, validated_data["call_stacks"])
# clear flag to specify this entry has been completed to edit
entry.del_status(Entry.STATUS_EDITING)
# running job of re-register referrals because of chaning entry's name
if job_register_referrals:
job_register_referrals.run()
# running job to notify changing entry event
if is_updated:
job_notify_event: Job = Job.new_notify_update_entry(user, entry)
job_notify_event.run()
return entry
class PrivilegedEntryUpdateSerializer(EntryUpdateSerializer):
privileged_mode = True
class EntryRetrieveSerializer(EntryBaseSerializer):
attrs = serializers.SerializerMethodField()
schema = EntitySerializer()
class Meta:
model = Entry
fields = [
"id",
"name",
"schema",
"is_active",
"deleted_user",
"deleted_time",
"attrs",
"is_public",
]
read_only_fields = ["is_active"]
@extend_schema_field(serializers.ListField(child=EntryAttributeTypeSerializer()))
def get_attrs(self, obj: Entry) -> list[EntryAttributeType]:
def get_attr_value(attr: Attribute) -> EntryAttributeValue:
attrv = attr.attrv_list[0] if len(attr.attrv_list) > 0 else None
if not attrv:
return {}
try:
attr_type = AttrType(attr.schema.type)
except ValueError:
Logger.error("Invalid attribute type: %s" % attr.schema.type)
return {}
match attr_type:
case AttrType.ARRAY_STRING:
return {
"as_array_string": [x.value for x in attrv.data_array.all()],
}
case AttrType.ARRAY_OBJECT:
return {
"as_array_object": [
{
"id": x.referral.id if x.referral else 0,
"name": x.referral.name if x.referral else "",
"schema": {
"id": x.referral.entry.schema.id,
"name": x.referral.entry.schema.name,
},
}
for x in attrv.data_array.all().select_related(
"referral__entry__schema"
)
if x.referral and x.referral.is_active
]
}
case AttrType.ARRAY_NAMED_OBJECT:
array_named_object: list[EntryAttributeValueNamedObject] = [
{
"name": x.value,
"object": {
"id": x.referral.id if x.referral else 0,
"name": x.referral.name if x.referral else "",
"schema": {
"id": x.referral.entry.schema.id,
"name": x.referral.entry.schema.name,
},
}
if x.referral and x.referral.is_active
else None,
}
for x in attrv.data_array.all().select_related("referral__entry__schema")
if not (x.referral and not x.referral.is_active)
]
return {"as_array_named_object": array_named_object}
case AttrType.ARRAY_GROUP:
groups = [v.group for v in attrv.data_array.all().select_related("group")]
return {
"as_array_group": [
{
"id": group.id,
"name": group.name,
}
for group in groups
]
}
case AttrType.ARRAY_ROLE:
roles = [v.role for v in attrv.data_array.all().select_related("role")]
return {
"as_array_role": [
{
"id": role.id,
"name": role.name,
}
for role in roles
]
}
case AttrType.STRING | AttrType.TEXT:
return {"as_string": attrv.value}
case AttrType.OBJECT:
return {
"as_object": {
"id": attrv.referral.id if attrv.referral else 0,
"name": attrv.referral.name if attrv.referral else "",
"schema": {
"id": attrv.referral.entry.schema.id,
"name": attrv.referral.entry.schema.name,
},
}
if attrv.referral and attrv.referral.is_active
else None,
}
case AttrType.NAMED_OBJECT:
named: EntryAttributeValueNamedObject = {
"name": attrv.value,
"object": {
"id": attrv.referral.id if attrv.referral else 0,
"name": attrv.referral.name if attrv.referral else "",
"schema": {
"id": attrv.referral.entry.schema.id,
"name": attrv.referral.entry.schema.name,
},
}
if attrv.referral and attrv.referral.is_active
else None,
}
return {"as_named_object": named}
case AttrType.BOOLEAN:
return {"as_boolean": attrv.boolean}
case AttrType.DATE:
return {"as_string": attrv.date if attrv.date else ""}
case AttrType.GROUP if attrv.group:
return {
"as_group": {
"id": attrv.group.id,
"name": attrv.group.name,
}
}
case AttrType.ROLE if attrv.role:
return {
"as_role": {
"id": attrv.role.id,
"name": attrv.role.name,
}
}
case AttrType.DATETIME:
return {"as_string": attrv.datetime if attrv.datetime else ""}
case _:
return {}
def get_default_attr_value(type: int) -> EntryAttributeValue:
try:
attr_type = AttrType(type)
except ValueError:
raise IncorrectTypeError(f"unexpected type: {type}")
match attr_type:
case AttrType.ARRAY_STRING:
return {
"as_array_string": AttrDefaultValue[type],
}
case AttrType.ARRAY_NAMED_OBJECT:
return {"as_array_named_object": []}
case AttrType.ARRAY_OBJECT:
return {"as_array_object": AttrDefaultValue[type]}
case AttrType.ARRAY_GROUP:
return {"as_array_group": AttrDefaultValue[type]}
case AttrType.ARRAY_ROLE:
return {"as_array_role": AttrDefaultValue[type]}
case AttrType.STRING | AttrType.TEXT:
return {"as_string": AttrDefaultValue[type]}
case AttrType.OBJECT:
return {"as_object": AttrDefaultValue[type]}
case AttrType.NAMED_OBJECT:
return {"as_named_object": {"name": "", "object": None}}
case AttrType.BOOLEAN:
return {"as_boolean": AttrDefaultValue[type]}
case AttrType.DATE:
return {"as_string": AttrDefaultValue[type]}
case AttrType.GROUP:
return {"as_group": AttrDefaultValue[type]}
case AttrType.ROLE:
return {"as_role": AttrDefaultValue[type]}
case AttrType.DATETIME:
return {"as_string": AttrDefaultValue[type]}
case _:
raise IncorrectTypeError(f"unexpected type: {type}")
attrv_prefetch = Prefetch(
"values",
queryset=AttributeValue.objects.filter(is_latest=True).select_related(
"referral__entry__schema", "group", "role"
),
to_attr="attrv_list",
)
attr_prefetch = Prefetch(
"attribute_set",
queryset=Attribute.objects.filter(parent_entry=obj).prefetch_related(attrv_prefetch),
to_attr="attr_list",
)
entity_attrs = (
obj.schema.attrs.filter(is_active=True)
.prefetch_related(attr_prefetch)
.order_by("index")
)
user: User = self.context["request"].user
attrinfo: list[EntryAttributeType] = []
for entity_attr in entity_attrs:
attr = entity_attr.attr_list[0] if entity_attr.attr_list else None
if attr:
is_readable = user.has_permission(attr, ACLType.Readable)
else:
is_readable = user.has_permission(entity_attr, ACLType.Readable)
value = (
get_attr_value(attr)
if attr and is_readable
else get_default_attr_value(entity_attr.type)
)
attrinfo.append(
{
"id": attr.id if attr else None,
"type": entity_attr.type,
"is_mandatory": entity_attr.is_mandatory,
"is_readable": is_readable,
"value": value,
"schema": {
"id": entity_attr.id,
"name": entity_attr.name,
},
}
)
# add and remove attributes depending on entity
if custom_view.is_custom("get_entry_attr", obj.schema.name):
attrinfo = custom_view.call_custom(
"get_entry_attr", obj.schema.name, obj, attrinfo, True
)
return attrinfo
class EntryCopySerializer(serializers.Serializer):
copy_entry_names = serializers.ListField(
child=serializers.CharField(),
write_only=True,
required=True,
allow_empty=False,
)
class Meta:
fields = "copy_entry_names"
def validate_copy_entry_names(self, copy_entry_names: list[str]):
entry: Entry = self.instance
duplicated_entries = Entry.objects.filter(
name__in=copy_entry_names, schema=entry.schema, is_active=True
)
if duplicated_entries.exists():
raise DuplicatedObjectExistsError(
"specified names(%s) already exists"
% ",".join([e.name for e in duplicated_entries])
)
# check custom validate
user = self.context["request"].user
if custom_view.is_custom("validate_entry", entry.schema.name):
for name in copy_entry_names:
custom_view.call_custom(
"validate_entry", entry.schema.name, user, entry.schema.name, name, [], None
)
class EntryExportSerializer(serializers.Serializer):
format = serializers.CharField(default="yaml")
def validate_format(self, data: str) -> str:
if data.lower() == "csv":
return "csv"
return "yaml"
class EntryImportAttributeSerializer(serializers.Serializer):
name = serializers.CharField()
value = AttributeValueField(allow_null=True)
class EntryImportEntriesSerializer(serializers.Serializer):
name = serializers.CharField()
attrs = serializers.ListField(child=EntryImportAttributeSerializer(), required=False)
class EntryImportEntitySerializer(serializers.Serializer):
entity = serializers.CharField()
entries = serializers.ListField(child=EntryImportEntriesSerializer())
def validate(self, params: dict):
# It runs only in the background, because it takes a long time to process.
if self.parent:
return params
def _convert_value_name_to_id(attr_data: dict, entity_attrs: dict):
def _object(
val: str | dict | None,
refs: list[Entity],
) -> int | None:
if val:
# for compatibility;
# it will pick wrong entry if there are multiple entries with same name
if isinstance(val, str):
if len(refs) >= 2:
Logger.warn(
"ambiguous object given: entry name(%s), entity names(%s)",
val,
[x.name for x in refs],
)
ref_entry: Entry | None = Entry.objects.filter(
name=val, schema__in=refs
).first()
return ref_entry.id if ref_entry else 0
# fully qualified entry, safer than above
if isinstance(val, dict):
ref_entry = Entry.objects.filter(
name=val["name"], schema__name=val["entity"]
).first()
return ref_entry.id if ref_entry else 0
return None
def _group(val: str) -> int | None:
if val:
ref_group: Group | None = Group.objects.filter(name=val).first()
return ref_group.id if ref_group else 0
return None
def _role(val: str) -> int | None:
if val:
ref_role: Role | None = Role.objects.filter(name=val).first()
return ref_role.id if ref_role else 0
return None
if entity_attrs[attr_data["name"]]["type"] & AttrType._ARRAY:
if not isinstance(attr_data["value"], list):
return
for i, child_value in enumerate(attr_data["value"]):
if entity_attrs[attr_data["name"]]["type"] & AttrType.OBJECT:
if entity_attrs[attr_data["name"]]["type"] & AttrType._NAMED:
if not isinstance(child_value, dict):
return
attr_data["value"][i] = {
"name": list(child_value.keys())[0],
"id": _object(
list(child_value.values())[0],
entity_attrs[attr_data["name"]]["refs"],
),
}
else:
attr_data["value"][i] = _object(
child_value, entity_attrs[attr_data["name"]]["refs"]
)
if entity_attrs[attr_data["name"]]["type"] & AttrType.GROUP:
attr_data["value"][i] = _group(child_value)
if entity_attrs[attr_data["name"]]["type"] & AttrType.ROLE:
attr_data["value"][i] = _role(child_value)
else:
if entity_attrs[attr_data["name"]]["type"] & AttrType.OBJECT:
if entity_attrs[attr_data["name"]]["type"] & AttrType._NAMED:
if not isinstance(attr_data["value"], dict):
return
attr_data["value"] = (
{
"name": list(attr_data["value"].keys())[0],
"id": _object(
list(attr_data["value"].values())[0],
entity_attrs[attr_data["name"]]["refs"],
),
}
if len(attr_data["value"].keys()) > 0
else {}
)
else:
attr_data["value"] = _object(
attr_data["value"], entity_attrs[attr_data["name"]]["refs"]
)
if entity_attrs[attr_data["name"]]["type"] & AttrType.GROUP:
attr_data["value"] = _group(attr_data["value"])
if entity_attrs[attr_data["name"]]["type"] & AttrType.ROLE:
attr_data["value"] = _role(attr_data["value"])
entity: Entity | None = Entity.objects.filter(name=params["entity"], is_active=True).first()
if not entity:
return params
entity_attrs = {
entity_attr.name: {
"id": entity_attr.id,
"type": entity_attr.type,
"refs": [x for x in entity_attr.referral.filter(is_active=True)],
}
for entity_attr in entity.attrs.filter(is_active=True)
}
for entry_data in params["entries"]:
for attr_data in entry_data.get("attrs", []):
if attr_data["name"] in entity_attrs.keys():
attr_data["id"] = entity_attrs[attr_data["name"]]["id"]
_convert_value_name_to_id(attr_data, entity_attrs)
return params