@@ -460,21 +460,26 @@ def validate(self, attrs: dict) -> dict:
460460 data_product : Optional [Product ] = attrs .get ("product" )
461461 data_user = attrs .get ("user" )
462462
463- if self .instance is not None and (
464- (data_product and data_product != self .instance .product ) or (data_user and data_user != self .instance .user )
465- ):
466- raise ValidationError ("Product and user cannot be changed" )
463+ current_user = get_current_user ()
467464
468465 if self .instance is None :
466+ if data_product is None :
467+ raise ValidationError ("Product must be set" )
468+ if data_user is None :
469+ raise ValidationError ("User must be set" )
470+
469471 product_member = get_product_member (data_product , data_user )
470472 if product_member :
471473 raise ValidationError (f"Product member { data_product } / { data_user } already exists" )
472474
473- current_user = get_current_user ()
474- if self .instance is not None :
475- highest_user_role = get_highest_user_role (self .instance .product , current_user )
476- else :
477475 highest_user_role = get_highest_user_role (data_product , current_user )
476+ else :
477+ if (data_product and data_product != self .instance .product ) or (
478+ data_user and data_user != self .instance .user
479+ ):
480+ raise ValidationError ("Product and user cannot be changed" )
481+
482+ highest_user_role = get_highest_user_role (self .instance .product , current_user )
478483
479484 if highest_user_role != Roles .Owner and not (current_user and current_user .is_superuser ):
480485 if attrs .get ("role" ) == Roles .Owner :
@@ -498,26 +503,28 @@ def validate(self, attrs: dict) -> dict:
498503 data_product : Optional [Product ] = attrs .get ("product" )
499504 data_authorization_group = attrs .get ("authorization_group" )
500505
501- if self .instance is not None and (
502- (data_product and data_product != self .instance .product )
503- or (data_authorization_group and data_authorization_group != self .instance .authorization_group )
504- ):
505- raise ValidationError ("Product and authorization group cannot be changed" )
506+ current_user = get_current_user ()
506507
507508 if self .instance is None :
509+ if data_product is None :
510+ raise ValidationError ("Product must be set" )
511+ if data_authorization_group is None :
512+ raise ValidationError ("Authorization group must be set" )
513+
508514 product_authorization_group_member = get_product_authorization_group_member (
509515 data_product , data_authorization_group
510516 )
511517 if product_authorization_group_member :
512518 raise ValidationError (
513519 f"Product authorization group member { data_product } / { data_authorization_group } already exists"
514520 )
515-
516- current_user = get_current_user ()
517- if self .instance is not None :
518- highest_user_role = get_highest_user_role (self .instance .product , current_user )
519- else :
520521 highest_user_role = get_highest_user_role (data_product , current_user )
522+ else :
523+ if (data_product and data_product != self .instance .product ) or (
524+ data_authorization_group and data_authorization_group != self .instance .authorization_group
525+ ):
526+ raise ValidationError ("Product and authorization group cannot be changed" )
527+ highest_user_role = get_highest_user_role (self .instance .product , current_user )
521528
522529 if highest_user_role != Roles .Owner and not (current_user and current_user .is_superuser ):
523530 if attrs .get ("role" ) == Roles .Owner :
0 commit comments