Skip to main content

A collection of essential utilities for Python projects, including data type detection, date filtering, and token decoding.

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-base-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_base_utils-0.1.0.tar.gz (4.6 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_base_utils-0.1.0-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file bear_django_base_utils-0.1.0.tar.gz.

File metadata

  • Download URL: bear_django_base_utils-0.1.0.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.0

File hashes

Hashes for bear_django_base_utils-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b3ac8cc32d561818b24b3bd8dad66adab9be0d825cb2379e62853d1e631e9a3e
MD5 5e79c267bb0f858b5919884c59ce4066
BLAKE2b-256 b8dbef4c1da4ae0fcd4940c516e4e4db32342d463d39f84f0bd6aa6f9e5f4e5b

See more details on using hashes here.

File details

Details for the file bear_django_base_utils-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for bear_django_base_utils-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 131a8200198c160effe08abc5d637837783fbcdab9cbaf7156dc5e32ca151e5d
MD5 6f0fa6624f3ff70b4c977f488823647c
BLAKE2b-256 07f849e46a054b5e935c56c5556b8f15c6e8a3de1e30a76f6e61c4e293a60b82

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