duck.backend.django.utils

Utilities module for Django.

Module Contents

Functions

django_to_duck_request

Converts a Django request to a Duck request.

duck_to_django_request

Converts Duck’s HTTP request to Django’s HttpRequest.

duck_to_django_response

Converts Duck’s http response to Django’s http response object.

duck_url_to_django_syntax

Converts a Duck URL path to Django’s url path.

get_raw_django_payload

Retrieves the raw byte representation of the HTTP header section from a Django request.

run_django_command

Runs any Django management command programmatically.

run_from_command_line

Runs any Django management command as if it was run from command line.

to_django_uploadedfile

Function to convert Duck fileupload to django UploadedFile object

API

duck.backend.django.utils.django_to_duck_request(django_request: django.http.request.HttpRequest)

Converts a Django request to a Duck request.

duck.backend.django.utils.duck_to_django_request(request)

Converts Duck’s HTTP request to Django’s HttpRequest.

duck.backend.django.utils.duck_to_django_response(response: duck.http.response.HttpResponse)

Converts Duck’s http response to Django’s http response object.

duck.backend.django.utils.duck_url_to_django_syntax(url: str) str

Converts a Duck URL path to Django’s url path.

Parameters:

url – The URL path in duck syntax (e.g., /path/<some_variable>).

Returns:

The URL path in explicit Django syntax (e.g., /path/str:some_variable).

Return type:

str

duck.backend.django.utils.get_raw_django_payload(request: django.http.request.HttpRequest) bytes

Retrieves the raw byte representation of the HTTP header section from a Django request.

This function constructs the raw headers, starting with the request line (method, path, protocol) and followed by all headers in the request, in the standard HTTP header format.

Parameters:

request – The Django HTTP request object containing the request and headers.

Returns:

A byte string representing the raw HTTP header section of the request, including the request line and all headers. The result will be suitable for use in raw HTTP request construction or logging.

Return type:

bytes

duck.backend.django.utils.run_django_command(command, *args, **kwargs)

Runs any Django management command programmatically.

Parameters:
  • command – The command name, e.g., “migrate”, “makemigrations”

  • *args – Positional arguments for the command.

  • **kwargs – Keyword arguments for the command.

duck.backend.django.utils.run_from_command_line(command)

Runs any Django management command as if it was run from command line.

Parameters:

command – The command e.g., “manage.py migrate”

duck.backend.django.utils.to_django_uploadedfile(fileupload: duck.http.fileuploads.handlers.BaseFileUpload) django.core.files.uploadedfile.SimpleUploadedFile

Function to convert Duck fileupload to django UploadedFile object