24
24
PRNField ,
25
25
)
26
26
from pulpcore .app .util import (
27
- get_viewset_for_model ,
28
27
get_request_without_query_params ,
28
+ get_url ,
29
+ get_prn ,
30
+ resolve_prn ,
29
31
)
30
32
31
33
User = get_user_model ()
@@ -60,27 +62,42 @@ def to_internal_value(self, data):
60
62
class ContentObjectField (serializers .CharField ):
61
63
"""Content object field"""
62
64
63
- def to_representation (self , obj ):
64
- content_object = getattr (obj , "content_object" , None )
65
- if content_object :
66
- viewset = get_viewset_for_model (obj .content_object )
67
-
65
+ def to_representation (self , value ):
66
+ if value is None :
67
+ return None
68
+ else :
68
69
request = get_request_without_query_params (self .context )
70
+ return get_url (value , request = request )
69
71
70
- serializer = viewset .serializer_class (obj .content_object , context = {"request" : request })
71
- return serializer .data .get ("pulp_href" )
72
-
73
- def to_internal_value (self , data ):
72
+ def to_internal_value (self , value ):
74
73
# ... circular import ...
75
74
from pulpcore .app .viewsets .base import NamedModelViewSet
76
75
77
- if data is None :
78
- return {"content_object" : None }
79
- try :
80
- obj = NamedModelViewSet .get_resource (data )
81
- except serializers .ValidationError :
82
- raise serializers .ValidationError (_ ("Invalid value: {}." ).format (data ))
83
- return {"content_object" : obj }
76
+ if value is None :
77
+ return None
78
+ else :
79
+ try :
80
+ obj = NamedModelViewSet .get_resource (value )
81
+ except serializers .ValidationError :
82
+ raise serializers .ValidationError (_ ("Invalid value: {}." ).format (value ))
83
+ return obj
84
+
85
+
86
+ class ContentObjectPRNField (serializers .CharField ):
87
+ """Content object PRN field"""
88
+
89
+ def to_representation (self , value ):
90
+ if value is None :
91
+ return None
92
+ else :
93
+ return get_prn (value )
94
+
95
+ def to_internal_value (self , value ):
96
+ if value is None :
97
+ return None
98
+ else :
99
+ model , pk = resolve_prn (value )
100
+ return model .objects .get (pk = pk )
84
101
85
102
86
103
class UserGroupSerializer (serializers .ModelSerializer ):
@@ -308,10 +325,19 @@ class UserRoleSerializer(ValidateRoleMixin, ModelSerializer, NestedHyperlinkedMo
308
325
"pulp_href of the object for which role permissions should be asserted. "
309
326
"If set to 'null', permissions will act on either domain or model-level."
310
327
),
311
- source = "*" ,
312
328
allow_null = True ,
313
329
)
314
330
331
+ content_object_prn = ContentObjectPRNField (
332
+ help_text = _ (
333
+ "prn of the object for which role permissions should be asserted. "
334
+ "If set to 'null', permissions will act on either domain or model-level."
335
+ ),
336
+ source = "content_object" ,
337
+ allow_null = True ,
338
+ required = False ,
339
+ )
340
+
315
341
domain = RelatedField (
316
342
help_text = _ (
317
343
"Domain this role should be applied on, mutually exclusive with content_object."
@@ -343,6 +369,7 @@ class Meta:
343
369
fields = ModelSerializer .Meta .fields + (
344
370
"role" ,
345
371
"content_object" ,
372
+ "content_object_prn" ,
346
373
"description" ,
347
374
"permissions" ,
348
375
"domain" ,
@@ -366,10 +393,19 @@ class GroupRoleSerializer(ValidateRoleMixin, ModelSerializer, NestedHyperlinkedM
366
393
"pulp_href of the object for which role permissions should be asserted. "
367
394
"If set to 'null', permissions will act on the model-level."
368
395
),
369
- source = "*" ,
370
396
allow_null = True ,
371
397
)
372
398
399
+ content_object_prn = ContentObjectPRNField (
400
+ help_text = _ (
401
+ "prn of the object for which role permissions should be asserted. "
402
+ "If set to 'null', permissions will act on either domain or model-level."
403
+ ),
404
+ source = "content_object" ,
405
+ allow_null = True ,
406
+ required = False ,
407
+ )
408
+
373
409
domain = RelatedField (
374
410
help_text = _ (
375
411
"Domain this role should be applied on, mutually exclusive with content_object."
@@ -403,6 +439,7 @@ class Meta:
403
439
fields = ModelSerializer .Meta .fields + (
404
440
"role" ,
405
441
"content_object" ,
442
+ "content_object_prn" ,
406
443
"description" ,
407
444
"permissions" ,
408
445
"domain" ,
0 commit comments