Skip to main content

Weni's common utilities for Python backends

Project description

Weni Commons

A Python library that centralizes common functionality and utilities used across Weni's Django backend applications. This library promotes code reuse, consistency, and maintainability by providing shared components and utilities that can be easily integrated into multiple Django projects.

Installation

Using Poetry (Recommended for Weni projects)

poetry add weni-commons

Using pip

pip install weni-commons

Available Features

Feature Flags

Weni Feature Flags is a Python Library that functions as an abstraction layer between Django projects and GrowthBook.

from weni.feature_flags.services import FeatureFlagsService

# Use the feature flags service
feature_service = FeatureFlagsService()

You can access the complete instructions on how to use its features here.

Session Token Validation

Validate session hashes issued by Connect against centralized Redis using a DRF authentication class that composes with existing JWT/OIDC backends.

Requirements: CACHES must point to the same Redis instance Connect writes to:

CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": env("REDIS_URL"),
        "OPTIONS": {"CLIENT_CLASS": "django_redis.client.DefaultClient"},
    }
}

Usage:

from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView
from weni_commons.auth import SessionTokenAuthentication

class ContactsView(APIView):
    authentication_classes = [
        SessionTokenAuthentication,
        WeniOIDCAuthentication,
    ]
    permission_classes = [IsAuthenticated]

    def get(self, request):
        if isinstance(request.auth, SessionContext):
            project = request.auth.projeto
            user = request.user.email
        else:
            project = request.query_params.get("project_uuid")
            user = request.user.email
        return Response({"project": project, "user": user})

Place SessionTokenAuthentication before JWT/OIDC classes so opaque session hashes fall through to Redis first and JWT tokens are handled by the next authenticator.

When the session hash is invalid or missing, the class returns None and DRF tries the next authentication backend instead of blocking the request immediately.

Session data is available on request.auth as a SessionContext. The authenticated user email is on request.user.email.

Optional setting: WENI_SESSION_TOKEN_REDIS_ALIAS (default "default") to select the Redis cache alias.

Requirements

  • Python >= 3.8
  • Django >= 3.2.22
  • Django REST Framework >= 3.12.0
  • Celery >= 5.0.0
  • Redis (via django-redis >= 4.0.0)

Contributing

This library is designed to grow with Weni's needs. If you have common utilities that could benefit multiple Django projects, consider contributing them to this library.

License

This project is licensed under the GNU General Public License v3.0 - see the LICENSE file for details.

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

weni_commons-1.2.0a6.tar.gz (20.9 kB view details)

Uploaded Source

Built Distribution

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

weni_commons-1.2.0a6-py3-none-any.whl (22.8 kB view details)

Uploaded Python 3

File details

Details for the file weni_commons-1.2.0a6.tar.gz.

File metadata

  • Download URL: weni_commons-1.2.0a6.tar.gz
  • Upload date:
  • Size: 20.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.8.18 Linux/6.17.0-1018-azure

File hashes

Hashes for weni_commons-1.2.0a6.tar.gz
Algorithm Hash digest
SHA256 213c800e4bd7325e00d4698bed2de93dee31cc3b33c376b43afa4eb3c2d8878c
MD5 c3911e656197793044e0b4676d5e91be
BLAKE2b-256 37c1340b90609726800f067bf0cea63611ae46c0c99e592189b4bf8a4a8dfcb3

See more details on using hashes here.

File details

Details for the file weni_commons-1.2.0a6-py3-none-any.whl.

File metadata

  • Download URL: weni_commons-1.2.0a6-py3-none-any.whl
  • Upload date:
  • Size: 22.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.5 CPython/3.8.18 Linux/6.17.0-1018-azure

File hashes

Hashes for weni_commons-1.2.0a6-py3-none-any.whl
Algorithm Hash digest
SHA256 cea8f23991b93eb6c07b8e14276ab0c1063a6166d06256aa7599c15ee380a3a2
MD5 ae813ef538992d71278a7f1968e5c989
BLAKE2b-256 f176b973743e955b5af422e12b44931f094fa59e32483e9f2dc7c7cec33f102e

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