Core - Utilities Documentation

Location: api/nextango/apps/core/utils/ Last Updated: 2026-07-09

Overview

Core utility modules shared by every domain app: encryption for sensitive configuration, performance logging decorators, and analytics query-param parsing. The caching helpers have their own page, see Caching.

Encryption Utilities

Location: utils/encryption.py

ConfigurationEncryption

Encrypts and decrypts configuration dictionaries (payment processor API keys, secrets) using Fernet symmetric encryption. The key comes from the CONFIGURATION_ENCRYPTION_KEY setting; a missing or malformed key raises ImproperlyConfigured on first use.

Public methods:

  • encrypt_configuration(config_dict) returns a base64-encoded encrypted string. Raises ValueError if the input is not a dict or encryption fails.
  • decrypt_configuration(encrypted_config) returns the original dict. Returns {} for empty input; raises ValueError on decryption failure.
  • rotate_encryption(old_encrypted_config, new_key) re-encrypts a payload under a new key.

Module functions

  • get_encryption_service() returns the lazily created singleton ConfigurationEncryption instance. This is what EncryptedJSONField calls internally, see Fields.
  • generate_encryption_key() returns a new Fernet key string. Also exposed as the generate_encryption_key management command.
  • validate_encryption_key(key) returns True if the key is a well-formed Fernet key.
from nextango.apps.core.utils.encryption import get_encryption_service

service = get_encryption_service()
token = service.encrypt_configuration({'api_key': 'secret'})
config = service.decrypt_configuration(token)

Performance Utilities

Location: utils/performance.py

log_execution_time(func_name=None)

Decorator that logs execution time and database query count for the wrapped function under a PERFORMANCE: log line. Warns when the query count exceeds 10 (potential N+1) and, in DEBUG, lists queries slower than 100ms. Applied across the codebase to views and actions that query the database.

from nextango.apps.core.utils.performance import log_execution_time

@log_execution_time('StoreViewSet.dashboard_home')
def dashboard_home(self, request, **kwargs):
    ...

profile_database_queries(func)

Decorator that logs every query the wrapped function executes, with timing. Active only when DEBUG=True; otherwise it calls through unchanged.

PerformanceTimer

Context manager that logs the elapsed time of a code block:

from nextango.apps.core.utils.performance import PerformanceTimer

with PerformanceTimer('bulk import'):
    run_import()

analyze_queryset_performance(queryset, operation_name='Queryset Operation')

Forces evaluation of a queryset and logs elapsed time and query count. A debugging aid, not for production paths.

Analytics Utilities

Location: utils/analytics.py

parse_date_range(request, default_days=30)

Parses start_date and end_date ISO date strings from request query params. Missing or unparseable values fall back to a window ending today and spanning default_days. Returns a (start_date, end_date) tuple of date objects. Used by the analytics endpoints across domains and by the dashboard overview endpoint.

parse_store_filter(request)

Reads store_id from query params and returns a Q object matching either the store's Django UUID or its Sanity ID (Q(store__id=...) | Q(store__sanity_id=...)), or None when no store_id is supplied.

Was this page helpful?