schon/evibes/settings/drf.py

149 lines
6.7 KiB
Python

from datetime import timedelta
from os import getenv
from django.utils.translation import gettext_lazy as _
from evibes.settings.base import DEBUG, EVIBES_VERSION, SECRET_KEY
from evibes.settings.constance import CONSTANCE_CONFIG
REST_FRAMEWORK: dict[str, str | int | list[str] | tuple[str, ...] | dict[str, bool]] = {
"DEFAULT_PAGINATION_CLASS": "evibes.pagination.CustomPagination",
"PAGE_SIZE": 30,
"DEFAULT_AUTHENTICATION_CLASSES": [
"rest_framework_simplejwt.authentication.JWTAuthentication",
],
"DEFAULT_RENDERER_CLASSES": (
"djangorestframework_camel_case.render.CamelCaseJSONRenderer",
"rest_framework.renderers.MultiPartRenderer",
"rest_framework_xml.renderers.XMLRenderer",
"rest_framework_yaml.renderers.YAMLRenderer",
),
"DEFAULT_PARSER_CLASSES": (
"djangorestframework_camel_case.parser.CamelCaseJSONParser",
"djangorestframework_camel_case.parser.CamelCaseMultiPartParser",
"rest_framework.parsers.FormParser",
"rest_framework.parsers.MultiPartParser",
"rest_framework_xml.parsers.XMLParser",
"rest_framework_yaml.parsers.YAMLParser",
),
"DEFAULT_SCHEMA_CLASS": "drf_spectacular.generators.AutoSchema",
"DEFAULT_PERMISSION_CLASSES": ("rest_framework.permissions.AllowAny",),
"JSON_UNDERSCOREIZE": {
"no_underscore_before_number": False,
},
}
SIMPLE_JWT: dict[str, timedelta | str | bool] = {
"ACCESS_TOKEN_LIFETIME": timedelta(hours=8)
if not DEBUG # noqa: F405
else timedelta(hours=88),
"REFRESH_TOKEN_LIFETIME": timedelta(days=8),
"ROTATE_REFRESH_TOKENS": True,
"BLACKLIST_AFTER_ROTATION": True,
"UPDATE_LAST_LOGIN": True,
"SIGNING_KEY": getenv("JWT_SIGNING_KEY", SECRET_KEY), # noqa: F405
"USER_ID_FIELD": "uuid",
"USER_ID_CLAIM": "user_uuid",
"AUTH_HEADER_NAME": "HTTP_X_EVIBES_AUTH",
}
SPECTACULAR_DESCRIPTION = _(f"""
Welcome to the eVibes documentation.
eVibes is a powerful e-commerce platform that allows you to launch and manage an online store of any kind in just a few clicks.
## Key Features
- **Product Catalog:** Manage product details, pricing, inventory, and availability across multiple categories.
- **Order Management:** Process orders, track fulfillment, and handle customer requests efficiently.
- **Authentication & Authorization:** Comprehensive user authentication with JWT tokens and role-based permissions.
- **Payment Processing:** Integrate multiple payment gateways and manage transactions securely.
- **Blog & Content Management:** Create and manage blog posts and marketing content for your store.
- **B2B Operations:** Dedicated endpoints for business-to-business transactions and wholesale management.
- **Multi-language Support:** Serve customers worldwide with full internationalization (i18n) capabilities.
- **Custom Integrations:** Extensible API architecture for integrating with external platforms and services.
- **Analytics & Reporting:** Generate detailed reports on sales, inventory, and customer behavior.
- **Real-Time Updates:** Get live data on inventory levels, order statuses, and pricing changes.
## Available APIs
- **REST API:** Full RESTful interface (this documentation)
- **GraphQL API:** Available at `/graphql/` with GraphiQL interface for interactive queries
## Authentication
- Authentication is handled via JWT tokens. Include the token in the `X-EVIBES-AUTH` header of your requests in the format `Bearer <your_token>`.
- Access token lifetime is {
SIMPLE_JWT.get("ACCESS_TOKEN_LIFETIME").total_seconds() // 60 if not DEBUG else 3600 # type: ignore [union-attr]
} {"minutes" if not DEBUG else "hours"}.
- Refresh token lifetime is {
SIMPLE_JWT.get("REFRESH_TOKEN_LIFETIME").total_seconds() // 3600 # type: ignore [union-attr]
} hours.
- Refresh tokens are automatically rotated and invalidated after usage for enhanced security.
## Internationalization (i18n)
- Set the `Accept-Language` header to specify your preferred language (e.g., `Accept-Language: en-US`).
- Available languages can be retrieved from the `/app/languages/` endpoint.
- All user-facing content supports multiple languages out of the box.
## Response Formats
The API supports multiple response formats:
- **JSON** (default, camelCase formatted)
- **XML** (add `?format=xml` or set `Accept: application/xml`)
- **YAML** (add `?format=yaml` or set `Accept: application/x-yaml`)
## Health & Monitoring
- Health checks: `/health/`
- Prometheus metrics (basic-auth protected): `/prometheus/`
## Version
Current API version: {EVIBES_VERSION}
""") # noqa: E501, F405
SPECTACULAR_SETTINGS = {
"TITLE": f"{CONSTANCE_CONFIG.get('PROJECT_NAME')[0]} API", # type: ignore [index]
"DESCRIPTION": SPECTACULAR_DESCRIPTION,
"VERSION": EVIBES_VERSION, # noqa: F405
"TOS": "https://evibes.wiseless.xyz/terms-of-service",
"SWAGGER_UI_DIST": "SIDECAR",
"CAMELIZE_NAMES": True,
"POSTPROCESSING_HOOKS": [
"drf_spectacular.contrib.djangorestframework_camel_case.camelize_serializer_fields",
"drf_spectacular.hooks.postprocess_schema_enums",
],
"REDOC_DIST": "SIDECAR",
"ENABLE_DJANGO_DEPLOY_CHECK": not DEBUG, # noqa: F405
"SWAGGER_UI_FAVICON_HREF": r"/static/favicon.png",
"SWAGGER_UI_SETTINGS": {
"requestInterceptor": """
function(request) {
const fmt = new URL(request.url).searchParams.get('format');
if (fmt) {
switch (fmt) {
case 'xml':
request.headers['Accept'] = 'application/xml';
break;
case 'yaml':
request.headers['Accept'] = 'application/x-yaml';
break;
case 'yml':
request.headers['Accept'] = 'application/x-yaml';
break;
default:
request.headers['Accept'] = 'application/json';
}
}
return request;
}
""",
},
"SERVERS": [
{
"url": f"https://api.{CONSTANCE_CONFIG.get('BASE_DOMAIN')[0]}/", # type: ignore [index]
"description": "Production Server",
},
{"url": "http://api.localhost:8000/", "description": "Development Server"},
],
"CONTACT": {
"name": 'Egor "fureunoir" Gorbunov',
"email": "contact@fureunoir.com",
"URL": "https://t.me/fureunoir",
},
}