Skip to main content

A core utility library for normalizing Django request data.

Project description

Bear Django Request Utils

This is a core utility library for Django, designed to clean and normalize incoming request data (GET, POST, JSON, etc.) before it reaches your business logic. It solves the common problem of client applications (frontend, mobile apps) sending data in a non-unified format.

This package provides the base logic and is intended to be used within your own components, such as middleware.


✨ Key Features

  • None Normalization: Converts strings like "null", empty strings "", and the integer 0 for id or _id fields into a None value.
  • Boolean Normalization: Converts string values like "true" and "false" (case-insensitive) into True and False.
  • String Cleaning: Automatically trims leading/trailing whitespace and decodes URL-encoded characters (like %20).
  • Special Email Handling: Removes spaces and lowercases email fields.
  • Universal: Works with GET parameters, application/json, multipart/form-data, and application/x-www-form-urlencoded.
  • Configurable: Allows you to specify which URL prefixes (e.g., /api/) the normalization should apply to.

⚙️ Installation

pip install bear-django-request-utils

🧩 Concept and Usage

This package provides a RequestUtils class with a set of static methods. The main method is convert_request_params(request), which accepts a Django request object and modifies it in-place.

The best way to use this is by calling the method from within your own custom middleware.


Example of creating middleware that uses this package

your_project/middleware.py

from bear_request_utils import RequestUtils

class RequestNormalizerMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        # Call the core logic from the package
        RequestUtils.convert_request_params(request)
        return self.get_response(request)

Add your middleware to settings.py:

MIDDLEWARE = [
    # ...
    'your_project.middleware.RequestNormalizerMiddleware',
    # ...
]

⚙️ Configuration

To enable normalization, you must specify a list of URL prefixes for which it will be applied. This is done in your settings.py.

# settings.py

# A list of URL prefixes for which normalization will be active.
API_PREFIXES = ['/api/v1/', '/api/v2/']

🧮 Normalization Rules

Input Value Key Output Value Explanation
"null" any None The string "null" becomes None
"true" any True The string "true" becomes True
"false" any False The string "false" becomes False
0 (integer) user_id None 0 for _id or id fields becomes None
"0" (string) id None The string "0" for _id or id fields becomes None
"" (empty string) category_id None An empty string for _id or id fields becomes None
" test%20string " name "test string" Whitespace is trimmed and characters are decoded
" User@Example.com " email "user@example.com" Whitespace is removed and the string is lowercased

📜 License

This project is licensed under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bear_django_request_utils-0.2.1.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bear_django_request_utils-0.2.1-py3-none-any.whl (4.6 kB view details)

Uploaded Python 3

File details

Details for the file bear_django_request_utils-0.2.1.tar.gz.

File metadata

File hashes

Hashes for bear_django_request_utils-0.2.1.tar.gz
Algorithm Hash digest
SHA256 63eb3e2e76d89f0890dbf617a49ded4989f5ecbdcbcbfc6c1594669e0c305b34
MD5 876cddfe2ea6518cb56605a12bc766e6
BLAKE2b-256 3f905c634fa30cfb86dc40e00ba2f3ac226b122c4056b7a77a24da6cb11b86f7

See more details on using hashes here.

File details

Details for the file bear_django_request_utils-0.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for bear_django_request_utils-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f9811050b17d97fd1fd8e12949ef937a1d938acc52d36e75fdfb432c3de105ed
MD5 99acb9a6b40c0098e76107802d2a2c07
BLAKE2b-256 a923401677b13f6c46f2b01c237d6ba6560d04016cb518114efe56e69c8fe95d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page