python

python
import threading
from contextlib import contextmanager

_state = threading.local()

Per-Tenant Data Isolation in Django with a Thread-Local Current Tenant Manager

django multi-tenancy orm
by codesnips 4 tabs
python
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'mydb',
        'USER': 'myuser',
        'PASSWORD': 'mypassword',

Django database connection pooling for performance

django python database
by Priya Sharma 1 tab
python
# Debug Toolbar setup
INSTALLED_APPS += ['debug_toolbar']

MIDDLEWARE = [
    'debug_toolbar.middleware.DebugToolbarMiddleware',
] + MIDDLEWARE

Django debug toolbar for development

django python debugging
by Priya Sharma 1 tab
python
from rest_framework.pagination import PageNumberPagination, CursorPagination


class StandardResultsSetPagination(PageNumberPagination):
    page_size = 25
    page_size_query_param = 'page_size'

Django REST Framework pagination with custom classes

django python rest
by Priya Sharma 2 tabs
python
# Session configuration
SESSION_ENGINE = 'django.contrib.sessions.backends.cached_db'
SESSION_CACHE_ALIAS = 'default'
SESSION_COOKIE_AGE = 1209600  # 2 weeks in seconds
SESSION_COOKIE_SECURE = True  # HTTPS only
SESSION_COOKIE_HTTPONLY = True  # No JavaScript access

Django session management and custom session backends

django python sessions
by Priya Sharma 2 tabs
python
from django.db import models
from django.utils import timezone


class Event(models.Model):
    name = models.CharField(max_length=200)

Django timezone-aware datetime handling

django python timezone
by Priya Sharma 1 tab
python
import hashlib
import hmac
import time


class SignatureError(Exception):

Verify Stripe-Style Webhook HMAC Signatures Before Processing in Flask

flask webhooks hmac
by codesnips 3 tabs
python
CACHES = {
    "default": {
        "BACKEND": "django_redis.cache.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379/1",
        "OPTIONS": {
            "CLIENT_CLASS": "django_redis.client.DefaultClient",

Custom DRF Throttle Class for a Public API With Sliding-Window Rate Limits

django drf rate-limiting
by codesnips 3 tabs
python
from rest_framework.throttling import UserRateThrottle


class BurstRateThrottle(UserRateThrottle):
    """Allow short bursts of requests."""
    scope = 'burst'

Django REST Framework throttling for rate limiting

django python rest
by Priya Sharma 3 tabs
python
from django.db import models
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType


class Comment(models.Model):

Django contenttypes framework for generic relations

django python models
by Priya Sharma 3 tabs
python
from django.core.cache import cache
from django.views.decorators.cache import cache_page
from django.utils.decorators import method_decorator
from django.views.generic import ListView, DetailView
from .models import Post

Django caching with cache_page and cache decorator

django python caching
by Priya Sharma 1 tab
python
import sqlite3

import click
from flask import current_app, g

Request-Scoped Database Connections in Flask Using g and teardown_appcontext

flask sqlite connection-pooling
by codesnips 4 tabs