def __init__(self, *args, **kwargs):
        self.expanded_fields = []

        passed = { 
            'expand' : kwargs.pop('expand', None), 
            'fields': kwargs.pop('fields', None) 
        }

        super(FlexFieldsSerializerMixin, self).__init__(*args, **kwargs)
        expand = self._get_expand_input(passed)
        fields = self._get_fields_input(passed)
        expand_field_names, next_expand_field_names = split_levels(expand)
        sparse_field_names, next_sparse_field_names = split_levels(fields)
        expandable_fields_names = self._get_expandable_names(sparse_field_names)

        if '~all' in expand_field_names:
            expand_field_names = self.expandable_fields.keys()
        
        for name in expand_field_names:
            if name not in expandable_fields_names:
                continue
            
            self.expanded_fields.append(name)

            self.fields[name] = self._make_expanded_field_serializer(
                name, next_expand_field_names, next_sparse_field_names
            )
    def __init__(self, *args, **kwargs):
        self.expanded_fields = []

        passed = {}
        for field in ["expand", "fields", "omit"]:
            if field in kwargs:
                if isinstance(kwargs[field], str):
                    passed[field] = [kwargs.pop(field)]
                else:
                    passed[field] = list(kwargs.pop(field, []))
            else:
                passed[field] = []

        super(FlexFieldsSerializerMixin, self).__init__(*args, **kwargs)
        expand = self._get_expand_input(passed)
        fields = self._get_fields_input(passed)
        omit = self._get_omit_input(passed)

        expand_fields, next_expand_fields = split_levels(expand)
        sparse_fields, next_sparse_fields = split_levels(fields)
        omit_fields, next_omit_fields = split_levels(omit)

        self._clean_fields(omit_fields, sparse_fields, next_omit_fields)

        expanded_field_names = self._get_expanded_names(
            expand_fields, omit_fields, sparse_fields, next_omit_fields)

        for name in expanded_field_names:
            self.expanded_fields.append(name)

            self.fields[name] = self._make_expanded_field_serializer(
                name, next_expand_fields, next_sparse_fields, next_omit_fields)
Beispiel #3
0
    def apply_flex_fields(self):
        expand_fields, next_expand_fields = split_levels(
            self._flex_options["expand"])
        sparse_fields, next_sparse_fields = split_levels(
            self._flex_options["fields"])
        omit_fields, next_omit_fields = split_levels(
            self._flex_options["omit"])

        to_remove = self._get_fields_names_to_remove(
            omit_fields,
            sparse_fields,
            next_omit_fields,
        )

        for field_name in to_remove:
            self.fields.pop(field_name)

        expanded_field_names = self._get_expanded_field_names(
            expand_fields,
            omit_fields,
            sparse_fields,
            next_omit_fields,
        )

        for name in expanded_field_names:
            self.expanded_fields.append(name)

            self.fields[name] = self._make_expanded_field_serializer(
                name,
                next_expand_fields,
                next_sparse_fields,
                next_omit_fields,
            )

        self._flex_fields_applied = True
    def __init__(self, *args, **kwargs):
        self.expanded_fields = []

        passed = {
            "expand": list(kwargs.pop("expand", [])),
            "fields": list(kwargs.pop("fields", [])),
            "omit": list(kwargs.pop("omit", [])),
        }

        super(FlexFieldsSerializerMixin, self).__init__(*args, **kwargs)
        expand = self._get_expand_input(passed)
        fields = self._get_fields_input(passed)
        omit = self._get_omit_input(passed)

        expand_fields, next_expand_fields = split_levels(expand)
        sparse_fields, next_sparse_fields = split_levels(fields)
        omit_fields, next_omit_fields = split_levels(omit)

        self._clean_fields(omit_fields, sparse_fields, next_omit_fields)

        expanded_field_names = self._get_expanded_names(
            expand_fields, omit_fields, sparse_fields, next_omit_fields
        )

        for name in expanded_field_names:
            self.expanded_fields.append(name)

            self.fields[name] = self._make_expanded_field_serializer(
                name, next_expand_fields, next_sparse_fields, next_omit_fields
            )
Beispiel #5
0
    def apply_flex_fields(self, fields, flex_options):
        expand_fields, next_expand_fields = split_levels(flex_options["expand"])
        sparse_fields, next_sparse_fields = split_levels(flex_options["fields"])
        omit_fields, next_omit_fields = split_levels(flex_options["omit"])

        for field_name in self._get_fields_names_to_remove(
            fields, omit_fields, sparse_fields, next_omit_fields
        ):
            fields.pop(field_name)

        for name in self._get_expanded_field_names(
            expand_fields, omit_fields, sparse_fields, next_omit_fields
        ):
            self.expanded_fields.append(name)

            fields[name] = self._make_expanded_field_serializer(
                name, next_expand_fields, next_sparse_fields, next_omit_fields
            )

        return fields
    def __init__(self, *args, **kwargs):
        passed = {
            'expand': kwargs.pop('expand', None),
            'fields': kwargs.pop('fields', None),
            'omit': kwargs.pop('omit', []),
            'parent': kwargs.pop('parent', ''),
            'identifier': kwargs.pop('identifier', None)
        }

        # add excludes from expandable_fields to those on query params
        passed['omit'] += kwargs.pop('exclude', [])

        super(FlexFieldsSerializerMixin, self).__init__(*args, **kwargs)
        expand = self._get_expand_input(passed)
        fields = self._get_fields_input(passed)
        omit = self._get_omit_input(passed)
        expand_field_names, next_expand_field_names = split_levels(expand)
        sparse_field_names, next_sparse_field_names = split_levels(fields)
        omit_field_names, next_omit_field_names = split_levels(omit)
        omit_field_names = set(omit_field_names) - next_omit_field_names.keys()

        expandable_fields_names = self._get_expandable_names(
            sparse_field_names, omit_field_names)
        forced_expand_names = [
            name for name, field in self.fields.items()
            if isinstance(field, serializers.Serializer)
        ]
        identifier = passed['identifier']

        if identifier or self._can_access_request:
            url_specific_fields = ('view_name', 'lookup_field',
                                   'lookup_url_kwarg', 'format')
            identifier = identifier or self.context['request'].query_params.get(
                'identifier') or self.context['request'].data.get('identifier')
            if identifier in ('id', 'name', 'reference'):
                for name in self.related_fields:
                    kwargs = {
                        k: v
                        for k, v in self.fields[name]._kwargs.items()
                        if k not in url_specific_fields
                    }
                    new_related_field = PrimaryKeyRelatedField(
                        **kwargs
                    ) if identifier == 'id' else SafeSlugRelatedField(
                        identifier, **kwargs)
                    self.fields[name] = new_related_field
                for name in self.many_related_fields:
                    kwargs = {
                        k: v
                        for k, v in
                        self.fields[name].child_relation._kwargs.items()
                        if k not in url_specific_fields
                    }
                    new_related_field = PrimaryKeyRelatedField(
                        **kwargs
                    ) if identifier == 'id' else SafeSlugRelatedField(
                        identifier, **kwargs)
                    self.fields[name].child_relation = new_related_field
                self.fields.pop('url', None)
                self.fields.pop('verbose_url', None)

        if '*' in expand_field_names:
            expand_field_names = self.expandable_fields.keys()

        resolved_expand_fields = set(expandable_fields_names) & (
            set(expand_field_names) | set(forced_expand_names))

        for name in resolved_expand_fields:
            self.fields[name] = self._make_expanded_field_serializer(
                name, next_expand_field_names, next_sparse_field_names,
                next_omit_field_names, identifier)