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