@@ -33,6 +33,10 @@ class Meta:
33
33
34
34
35
35
class MyACLSerializer (BaseSerializer ):
36
+ scope = serializers .ResourceRelatedField (
37
+ queryset = Scope .objects .all (), required = False , many = False
38
+ )
39
+
36
40
included_serializers = {
37
41
"scope" : "emeis.core.serializers.ScopeSerializer" ,
38
42
"role" : "emeis.core.serializers.RoleSerializer" ,
@@ -73,6 +77,22 @@ class Meta:
73
77
74
78
75
79
class ScopeSerializer (BaseSerializer ):
80
+ level = serializers .SerializerMethodField ()
81
+
82
+ def get_level (self , obj ):
83
+ depth = getattr (obj , "tree_depth" , None )
84
+ if depth is not None :
85
+ return depth
86
+
87
+ # Note: This should only happen on CREATE, never in GET (Either list,
88
+ # detail, or include!) In CREATE, it's a new object that doesn't come
89
+ # from a QS
90
+
91
+ # Sometimes, the model object may come out of a non-django-tree-queries
92
+ # QS, and thus would not have the `tree_*` attributes amended. Then we
93
+ # need to go the "slow path"
94
+ return obj .ancestors ().count ()
95
+
76
96
class Meta :
77
97
model = Scope
78
98
fields = BaseSerializer .Meta .fields + (
@@ -83,7 +103,7 @@ class Meta:
83
103
"full_name" ,
84
104
"is_active" ,
85
105
)
86
- read_only_fields = ["full_name" ]
106
+ read_only_fields = ["full_name" , "level" ]
87
107
88
108
89
109
class PermissionSerializer (BaseSerializer ):
@@ -111,6 +131,9 @@ class Meta:
111
131
112
132
113
133
class ACLSerializer (BaseSerializer ):
134
+ scope = serializers .ResourceRelatedField (
135
+ queryset = Scope .objects .all (), required = False , many = False
136
+ )
114
137
included_serializers = {
115
138
"user" : UserSerializer ,
116
139
"scope" : ScopeSerializer ,
0 commit comments