python

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
import re

EMAIL_RE = re.compile(r"^[^@\s]+@[^@\s]+\.[^@\s]+$")


class SignupValidator:

Validate a Flask Signup Form and Collect Per-Field Errors

flask validation forms
by codesnips 4 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
from __future__ import annotations

from dataclasses import dataclass, field
from datetime import datetime
from typing import Any, Dict, List

Parsing a Nested JSON Feed into Normalized Dataclasses in Python

dataclasses json parsing
by codesnips 3 tabs
python
from datetime import datetime

from pydantic import BaseModel


class UserV1(BaseModel):

Versioning a FastAPI API With APIRouter Mounted Under /v1 and /v2 Prefixes

fastapi api-versioning apirouter
by codesnips 4 tabs
python
from django.conf import settings
from django.contrib.contenttypes.fields import GenericForeignKey
from django.contrib.contenttypes.models import ContentType
from django.db import models, transaction

from .middleware import get_current_user

Auditing Django Model Field Changes in an Overridden save() Method

django audit-log orm
by codesnips 3 tabs
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.db.models import Avg, Count, Sum, DecimalField
from django.db.models.functions import Coalesce, TruncDate
from django.utils import timezone

Daily Order Stats Rollup with Django TruncDate Aggregation and a JSON API View

django orm aggregation
by codesnips 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