Skip to content

Stream serializers

hypha.apply.api.v1.stream_serializers

IGNORE_ARGS module-attribute

IGNORE_ARGS = ['self', 'cls']

WagtailSerializer

get_serializer_fields

get_serializer_fields(draft=False)

Get the respective serializer fields for all the form fields.

Source code in hypha/apply/api/v1/stream_serializers.py
def get_serializer_fields(self, draft=False):
    """
    Get the respective serializer fields for all the form fields.
    """
    serializer_fields = OrderedDict()
    form_fields = self.get_form_fields()
    for field_id, field in form_fields.items():
        serializer_fields[field_id] = self._get_field(
            field, self.get_serializer_field_class(field), draft
        )
    return serializer_fields

find_function_args

find_function_args(func)

Get the list of parameter names which function accepts.

Source code in hypha/apply/api/v1/stream_serializers.py
def find_function_args(self, func):
    """
    Get the list of parameter names which function accepts.
    """
    try:
        spec = (
            inspect.getfullargspec(func)
            if hasattr(inspect, "getfullargspec")
            else inspect.getargspec(func)
        )
        return [i for i in spec[0] if i not in IGNORE_ARGS]
    except TypeError:
        return []

find_class_args

find_class_args(klass)

Find all class arguments (parameters) which can be passed in __init__.

Source code in hypha/apply/api/v1/stream_serializers.py
def find_class_args(self, klass):
    """
    Find all class arguments (parameters) which can be passed in ``__init__``.
    """
    args = set()
    for i in klass.mro():
        if i is object or not hasattr(i, "__init__"):
            continue
        args |= set(self.find_function_args(i.__init__))

    return list(args)

find_matching_class_kwargs

find_matching_class_kwargs(reference_object, klass)
Source code in hypha/apply/api/v1/stream_serializers.py
def find_matching_class_kwargs(self, reference_object, klass):
    return {
        i: getattr(reference_object, i)
        for i in self.find_class_args(klass)
        if hasattr(reference_object, i)
    }

get_serializer_field_class

get_serializer_field_class(field)

Assumes that a serializer field exist with the same name as form field.

TODO: In case there are form fields not existing in serializer fields, we would have to create mapping b/w form fields and serializer fields to get the respective classes. But for now this works.

Source code in hypha/apply/api/v1/stream_serializers.py
def get_serializer_field_class(self, field):
    """
    Assumes that a serializer field exist with the same name as form field.

    TODO:
    In case there are form fields not existing in serializer fields, we would
    have to create mapping b/w form fields and serializer fields to get the
    respective classes. But for now this works.
    """
    if isinstance(field, BlockFieldWrapper):
        return serializers.CharField
    if isinstance(field, ScoredAnswerField):
        return ScoredAnswerListField
    if isinstance(field, TypedChoiceField):
        return serializers.ChoiceField
    class_name = field.__class__.__name__
    return getattr(serializers, class_name)

get_serializer_class

get_serializer_class(draft=False)
Source code in hypha/apply/api/v1/stream_serializers.py
def get_serializer_class(self, draft=False):
    # Model serializers needs to have each field declared in the field options
    # of Meta. This code adds the dynamically generated serializer fields
    # to the serializer class meta fields.
    model_fields = [
        field
        for field in self.serializer_class.Meta.fields
        if hasattr(self.serializer_class.Meta.model, field)
    ]
    self.serializer_class.Meta.fields = model_fields + [
        *self.get_serializer_fields(draft).keys()
    ]
    return type(
        "WagtailStreamSerializer",
        (self.serializer_class,),
        self.get_serializer_fields(draft),
    )