class ShareProjectSerializer(serializers.Serializer): project = ProjectField() username = serializers.CharField(max_length=255) role = serializers.CharField(max_length=50) def restore_object(self, attrs, instance=None): if instance: return attrs_to_instance(attrs, instance) return ShareProject(**attrs) def validate_username(self, attrs, source): """Check that the username exists""" value = attrs[source] try: User.objects.get(username=value) except User.DoesNotExist: raise ValidationError( _(u"User '%(value)s' does not exist." % {"value": value})) return attrs def validate_role(self, attrs, source): """check that the role exists""" value = attrs[source] if value not in ROLES: raise ValidationError( _(u"Unknown role '%(role)s'." % {"role": value})) return attrs
class CloneXFormSerializer(serializers.Serializer): xform = XFormField() username = serializers.CharField(max_length=255) project = ProjectField(required=False) def create(self, validated_data): instance = CloneXForm(**validated_data) instance.save() return instance def update(self, instance, validated_data): instance.xform = validated_data.get('xform', instance.xform) instance.username = validated_data.get('username', instance.username) instance.project = validated_data.get('project', instance.project) instance.save() return instance def validate_username(self, value): """Check that the username exists""" try: User.objects.get(username=value) except User.DoesNotExist: raise serializers.ValidationError( _(u"User '%(value)s' does not exist." % {"value": value})) return value
class ShareTeamProjectSerializer(serializers.Serializer): team = TeamField() project = ProjectField() role = serializers.CharField(max_length=50) def update(self, instance, validated_data): instance.team = validated_data.get('team', instance.team) instance.project = validated_data.get('project', instance.project) instance.role = validated_data.get('role', instance.role) instance.save() return instance def create(self, validated_data): instance = ShareTeamProject(**validated_data) instance.save() return instance def validate_role(self, value): """check that the role exists""" if value not in ROLES: raise serializers.ValidationError(_( u"Unknown role '%(role)s'." % {"role": value} )) return value
class ShareProjectSerializer(serializers.Serializer): project = ProjectField() username = serializers.CharField(max_length=255) role = serializers.CharField(max_length=50) def create(self, validated_data): instance = ShareProject(**validated_data) instance.save() return instance def update(self, instance, validated_data): instance = attrs_to_instance(validated_data, instance) instance.save() return instance def validate(self, attrs): user = User.objects.get(username=attrs.get('username')) project = attrs.get('project') # check if the user is the owner of the project if user and project: if user == project.organization: raise serializers.ValidationError( {'username': _(u"Cannot share project with the owner")}) return attrs def validate_username(self, value): """Check that the username exists""" user = None try: user = User.objects.get(username=value) except User.DoesNotExist: raise serializers.ValidationError( _(u"User '%(value)s' does not exist." % {"value": value})) else: if not user.is_active: raise serializers.ValidationError(_(u"User is not active")) return value def validate_role(self, value): """check that the role exists""" if value not in ROLES: raise serializers.ValidationError( _(u"Unknown role '%(role)s'." % {"role": value})) return value
class ShareProjectSerializer(serializers.Serializer): project = ProjectField() username = serializers.CharField(max_length=255) role = serializers.CharField(max_length=50) def restore_object(self, attrs, instance=None): if instance: return attrs_to_instance(attrs, instance) return ShareProject(**attrs) def validate_username(self, attrs, source): """Check that the username exists""" value = attrs[source] user = None project = attrs.get('project') try: user = User.objects.get(username=value) except User.DoesNotExist: raise ValidationError( _(u"User '%(value)s' does not exist." % {"value": value})) # check if the user is the owner of the project if user and project: if user == project.organization: raise ValidationError( _(u"Cannot share project with" u" the owner")) if not user.is_active: raise ValidationError(_(u"User is not active")) return attrs def validate_role(self, attrs, source): """check that the role exists""" value = attrs[source] if value not in ROLES: raise ValidationError( _(u"Unknown role '%(role)s'." % {"role": value})) return attrs
class ShareProjectSerializer(serializers.Serializer): project = ProjectField() username = serializers.CharField(max_length=255) role = serializers.CharField(max_length=50) def update(self, instance, validated_data): instance.project = validated_data.get('project', instance.project) instance.username = validated_data.get('username', instance.username) instance.role = validated_data.get('role', instance.role) return instance def create(self, validated_data): project = ShareProject(**validated_data) project.save() return project def validate_username(self, value): """Check that the username exists""" try: User.objects.get(username=value) except User.DoesNotExist: raise ValidationError( _(u"User '%(value)s' does not exist." % {"value": value})) return value def validate_role(self, value): """check that the role exists""" if value not in ROLES: raise ValidationError( _(u"Unknown role '%(role)s'." % {"role": value})) return value def remove_user(self): obj = ShareProject(**self.validated_data) obj.remove_user()
class ShareTeamProjectSerializer(serializers.Serializer): team = TeamField() project = ProjectField() role = serializers.CharField(max_length=50) def restore_object(self, attrs, instance=None): if instance is not None: instance.team = attrs.get('team', instance.team) instance.project = attrs.get('project', instance.project) instance.role = attrs.get('role', instance.role) return instance return ShareTeamProject(**attrs) def validate_role(self, attrs, source): """check that the role exists""" value = attrs[source] if value not in ROLES: raise ValidationError(_(u"Unknown role '%(role)s'." % {"role": value})) return attrs
class ShareProjectSerializer(serializers.Serializer): project = ProjectField() username = serializers.CharField(max_length=255) role = serializers.CharField(max_length=50) def create(self, validated_data): usernames = validated_data.pop('username').split(',') created_instances = [] for username in usernames: validated_data['username'] = username instance = ShareProject(**validated_data) instance.save() created_instances.append(instance) return created_instances def update(self, instance, validated_data): instance = attrs_to_instance(validated_data, instance) instance.save() return instance def validate(self, attrs): usernames = attrs.get('username').split(',') for username in usernames: user = User.objects.get(username=username) project = attrs.get('project') # check if the user is the owner of the project if user and project: if user == project.organization: raise serializers.ValidationError({ 'username': _(u"Cannot share project with the owner (%(value)s)" % {"value": user.username}) }) return attrs def validate_username(self, value): """Check that the username exists""" usernames = value.split(',') user = None non_existent_users = [] inactive_users = [] for username in usernames: try: user = User.objects.get(username=username) except User.DoesNotExist: non_existent_users.append(username) else: if not user.is_active: inactive_users.append(username) if non_existent_users: non_existent_users = ', '.join(non_existent_users) raise serializers.ValidationError( _('The following user(s) does/do not exist:' f' {non_existent_users}')) if inactive_users: inactive_users = ', '.join(inactive_users) raise serializers.ValidationError( _(f'The following user(s) is/are not active: {inactive_users}') ) return value def validate_role(self, value): """check that the role exists""" if value not in ROLES: raise serializers.ValidationError( _(u"Unknown role '%(role)s'." % {"role": value})) return value