Serializer for a User with a bit more info.
| 299 | |
| 300 | |
| 301 | class ExtendedUserSerializer(UserSerializer): |
| 302 | """Serializer for a User with a bit more info.""" |
| 303 | |
| 304 | # from users.serializers import GroupSerializer |
| 305 | |
| 306 | class Meta(UserSerializer.Meta): |
| 307 | """Metaclass defines serializer fields.""" |
| 308 | |
| 309 | fields = [ |
| 310 | *UserSerializer.Meta.fields, |
| 311 | 'groups', |
| 312 | 'group_ids', |
| 313 | 'is_staff', |
| 314 | 'is_superuser', |
| 315 | 'is_active', |
| 316 | 'profile', |
| 317 | ] |
| 318 | |
| 319 | read_only_fields = [*UserSerializer.Meta.read_only_fields, 'groups'] |
| 320 | |
| 321 | groups = GroupSerializer(many=True, read_only=True) |
| 322 | |
| 323 | # Write-only field, for updating the groups associated with the user |
| 324 | group_ids = serializers.PrimaryKeyRelatedField( |
| 325 | queryset=Group.objects.all(), many=True, write_only=True, required=False |
| 326 | ) |
| 327 | |
| 328 | is_staff = serializers.BooleanField( |
| 329 | label=_('Administrator'), |
| 330 | help_text=_('Does this user have administrative permissions'), |
| 331 | required=False, |
| 332 | ) |
| 333 | |
| 334 | is_superuser = serializers.BooleanField( |
| 335 | label=_('Superuser'), help_text=_('Is this user a superuser'), required=False |
| 336 | ) |
| 337 | |
| 338 | is_active = serializers.BooleanField( |
| 339 | label=_('Active'), help_text=_('Is this user account active'), required=False |
| 340 | ) |
| 341 | |
| 342 | profile = BriefUserProfileSerializer(many=False, read_only=True) |
| 343 | |
| 344 | def validate_is_superuser(self, value): |
| 345 | """Only a superuser account can adjust this value!""" |
| 346 | request_user = self.context['request'].user |
| 347 | |
| 348 | if 'is_superuser' in self.context['request'].data: |
| 349 | if not request_user.is_superuser: |
| 350 | raise PermissionDenied({ |
| 351 | 'is_superuser': _('Only a superuser can adjust this field') |
| 352 | }) |
| 353 | |
| 354 | return value |
| 355 | |
| 356 | def update(self, instance, validated_data): |
| 357 | """Update the user instance with the provided data.""" |
| 358 | # Update the groups associated with the user |
nothing calls this directly
no test coverage detected