Source code for smarter.lib.drf.serializers

"""Account serializers for smarter api"""

import sys
from typing import Optional

from django.http import HttpRequest
from rest_framework import serializers

from smarter.common.utils import to_camel_case, to_snake_case
from smarter.lib import logging

logger = logging.getSmarterLogger(__name__)


[docs] def is_sphinx_build(): """Determine if the current execution context is a Sphinx documentation build.""" return "sphinx" in sys.modules
[docs] class SmarterCamelCaseSerializer(serializers.ModelSerializer): """Base serializer to convert field names to camelCase.""" request: Optional[HttpRequest]
[docs] def __init__(self, *args, **kwargs): """Initialize the serializer and set the request context.""" super().__init__(*args, **kwargs) self.logger_prefix = logging.formatted_text(__name__ + "." + self.__class__.__name__) # Get the request from the context if available, while # guarding against Sphinx autodoc generation issues. if is_sphinx_build(): self.request = None else: context = getattr(self, "context", None) if isinstance(context, dict): self.request = context.get("request", None) else: self.request = None
[docs] def to_representation(self, instance): """Convert field names to camelCase.""" representation = super().to_representation(instance) new_representation = {} for key, value in representation.items(): # double check that the key is a snake_case string before converting to camelCase snake_key = to_snake_case(key) camel_key = to_camel_case(snake_key) new_representation[camel_key] = value return new_representation