Serializers

Serializers for Django HTTP requests and user objects in the Smarter API.

This module defines custom serializers for:

  • Serializing the User model for API responses.

  • Representing anonymous and authenticated HTTP request data for use with the Django REST Framework.

These serializers enable structured, validated, and documented representations of HTTP request and user data for API endpoints and internal processing.

class smarter.lib.django.http.serializers.HttpAnonymousRequestSerializer(*args, **kwargs)[source]

Bases: Serializer

Serializes anonymous HTTP request data for the Smarter API.

This serializer is designed to represent the structure of an unauthenticated Django HTTP request, capturing key request attributes for use in API endpoints, logging, or debugging. It is intended for scenarios where user authentication is not required or available.

Parameters:
  • url (str) – The absolute URL of the request (read-only, derived from the request object).

  • method (str) – The HTTP method (e.g., GET, POST, PUT, DELETE).

  • GET (dict) – Query parameters from the request URL.

  • POST (dict) – Data submitted in the body of a POST request.

  • COOKIES (dict) – Cookies sent with the request.

  • META (dict) – Metadata and headers associated with the request.

  • path (str) – The path portion of the request URL.

  • encoding (str) – The encoding used for the request body.

  • content_type (str) – The MIME type of the request body.

Example

from smarter.lib.django.http.serializers import HttpAnonymousRequestSerializer
from django.http import HttpRequest

request = HttpRequest()
request.method = "GET"
request.path = "/api/v1/resource/"
request.GET = {"search": "test"}
request.encoding = "utf-8"
request.content_type = "application/json"

serializer = HttpAnonymousRequestSerializer(request)
print(serializer.data)
# Output (example):
# {
#     'url': None,
#     'method': 'GET',
#     'GET': {'search': 'test'},
#     'POST': {},
#     'COOKIES': {},
#     'META': {},
#     'path': '/api/v1/resource/',
#     'encoding': 'utf-8',
#     'content_type': 'application/json'
# }

Note

The url field is computed from the request object if available. If the request does not provide a method for building the absolute URI, this field may be None.

Warning

This serializer does not include user authentication information. For authenticated requests, use HttpAuthenticatedRequestSerializer.

create(validated_data)[source]
Return type:

HttpRequest

get_url(obj)[source]
to_representation(instance)[source]

Object instance -> Dict of primitive datatypes.

update(instance, validated_data)[source]
class smarter.lib.django.http.serializers.HttpAuthenticatedRequestSerializer(*args, **kwargs)[source]

Bases: HttpAnonymousRequestSerializer

Serializes authenticated HTTP request data for the Smarter API.

This serializer extends HttpAnonymousRequestSerializer to include user information, allowing for the representation of authenticated Django HTTP requests. It is intended for use in API endpoints, logging, or debugging where both request and user context are required.

Parameters:
  • url (str) – The absolute URL of the request (read-only, derived from the request object).

  • method (str) – The HTTP method (e.g., GET, POST, PUT, DELETE).

  • GET (dict) – Query parameters from the request URL.

  • POST (dict) – Data submitted in the body of a POST request.

  • COOKIES (dict) – Cookies sent with the request.

  • META (dict) – Metadata and headers associated with the request.

  • path (str) – The path portion of the request URL.

  • encoding (str) – The encoding used for the request body.

  • content_type (str) – The MIME type of the request body.

  • user – The authenticated user making the request, serialized using UserSerializer.

Example

from smarter.lib.django.http.serializers import HttpAuthenticatedRequestSerializer
from django.http import HttpRequest
from smarter.apps.account.models import User

request = HttpRequest()
request.method = "POST"
request.path = "/api/v1/secure/"
request.POST = {"data": "value"}
request.encoding = "utf-8"
request.content_type = "application/json"
request.user = User(username="bob")

serializer = HttpAuthenticatedRequestSerializer(request)
print(serializer.data)
# Output (example):
# {
#     'url': None,
#     'method': 'POST',
#     'GET': {},
#     'POST': {'data': 'value'},
#     'COOKIES': {},
#     'META': {},
#     'path': '/api/v1/secure/',
#     'encoding': 'utf-8',
#     'content_type': 'application/json',
#     'user': 'bob'
# }

Note

The user field is serialized using UserSerializer and only exposes the username by default.

Warning

Ensure that sensitive user information is not exposed by extending UserSerializer with caution. This serializer should only be used in contexts where authenticated user data is appropriate.

to_representation(instance)[source]

Object instance -> Dict of primitive datatypes.

class smarter.lib.django.http.serializers.UserSerializer(*args, **kwargs)[source]

Bases: ModelSerializer

Serializes the User model for use in Django REST Framework APIs.

This serializer provides a minimal representation of the User object, exposing only the username field. It is intended for use in API responses where user identification is required, but sensitive information should be excluded.

Parameters:

username (str) – The unique username of the user.

Example

from smarter.lib.django.http.serializers import UserSerializer
from smarter.apps.account.models import User

user = User(username="alice")
serializer = UserSerializer(user)
print(serializer.data)
# Output: {'username': 'alice'}

Note

This serializer is designed to be minimal for privacy and security. Only the username field is exposed.

Warning

Do not extend this serializer to include sensitive fields (such as passwords or email addresses) unless absolutely necessary and with proper security considerations.