class BouwDossierSerializer(CustomHalSerializer): documenten = DocumentSerializer(many=True) adressen = AdresSerializer(many=True) _display = DisplayField() class Meta: model = BouwDossier fields = ( '_links', 'titel', '_display', 'dossiernr', 'stadsdeel', 'datering', 'dossier_type', 'gebruiksdoel', 'bwt_nummer', 'dossier_status', 'olo_liaan_nummer', 'access', 'access_restricted_until', 'activiteiten', 'documenten', 'adressen', 'source', )
class GenericSerializer(HALSerializer): _links = serializers.SerializerMethodField() _display = DisplayField( ) # note: this field is not part of the HAL/DSO spec. class Meta: model = None fields = [ '_links', '_display', ] def get__links(self, obj): """Generate the HAL-style '_links' section. This overrides a generic ``serializer_url_field``, which is needed because the datasets are not registered in urls.py. """ request = self.context['request'] # These methods are generated in dataset_model_factory(): dataset = obj.get_dataset().name pk = obj.get_id() return { "self": { "href": request.build_absolute_uri(f'/vsd/{dataset}/{pk}/'), "title": str(obj), }, }
class UserListHALSerializer(WriteOnceMixin, HALSerializer): _display = DisplayField() roles = serializers.SerializerMethodField() role_ids = serializers.PrimaryKeyRelatedField( many=True, required=False, read_only=False, write_only=True, queryset=_get_groups_queryset(), source='groups' ) profile = ProfileListSerializer(required=False) class Meta: model = User fields = ( # '_links', '_display', 'id', 'username', 'is_active', 'roles', 'role_ids', 'profile' ) extra_kwargs = { 'email': {'read_only': True}, 'username': {'validators': [EmailValidator()]}, } write_once_fields = ( 'username', ) def get_roles(self, obj): return [group.name for group in obj.groups.all()]
class PermissionSerializer(HALSerializer): _display = DisplayField() class Meta: model = Permission fields = ( '_links', '_display', 'id', 'name', 'codename', )
class GenericSerializer(BaseSerializer, HALSerializer): _links = serializers.SerializerMethodField() _display = DisplayField() class Meta(object): model = None fields = [ '_links', '_display', ] def get__links(self, obj): links = self.dict_with_self_href( '/vsd/{}/{}/'.format( self.context['dataset'], obj.get_id())) return links
class RoleSerializer(HALSerializer): _display = DisplayField() permissions = PermissionSerializer(many=True, required=False, read_only=True) permission_ids = serializers.PrimaryKeyRelatedField( many=True, required=False, read_only=False, write_only=True, queryset=_get_permissions_queryset(), source='permissions' ) class Meta: model = Group fields = ( '_links', '_display', 'id', 'name', 'permissions', 'permission_ids', )
class UserDetailHALSerializer(WriteOnceMixin, HALSerializer): serializer_url_field = UserHyperlinkedIdentityField _display = DisplayField() roles = RoleSerializer(source='groups', many=True, read_only=True) role_ids = serializers.PrimaryKeyRelatedField( many=True, required=False, read_only=False, write_only=True, queryset=_get_groups_queryset(), source='groups') permissions = PermissionSerializer(source='user_permissions', many=True, read_only=True) profile = ProfileDetailSerializer(required=False) class Meta: model = User fields = ( '_links', '_display', 'id', 'username', 'email', 'first_name', 'last_name', 'is_active', 'is_staff', 'is_superuser', 'roles', 'role_ids', 'permissions', 'profile', ) extra_kwargs = { 'email': { 'read_only': True }, 'is_staff': { 'read_only': True }, 'is_superuser': { 'read_only': True }, 'username': { 'validators': [EmailValidator()] }, } write_once_fields = ('username', ) def create(self, validated_data): self.get_extra_kwargs() profile_data = validated_data.pop('profile', None) validated_data['email'] = validated_data[ 'username'] # noqa The email address and username are basically the same. TODO refactor this behavior in the user model instance = super(UserDetailHALSerializer, self).create(validated_data=validated_data) if profile_data: profile_detail_serializer = ProfileDetailSerializer() profile_detail_serializer.update(instance=instance.profile, validated_data=profile_data) instance.refresh_from_db() return instance def update(self, instance, validated_data): profile_data = validated_data.pop('profile', None) if profile_data: profile_detail_serializer = ProfileDetailSerializer() profile_detail_serializer.update(instance=instance.profile, validated_data=profile_data) groups = validated_data.pop('groups', None) if groups or isinstance(groups, (list, tuple)): instance.groups.set(groups) instance = super(UserDetailHALSerializer, self).update(instance=instance, validated_data=validated_data) return instance