Files
vontor-cz/backend/vontor_cz/views.py
David Bruno Vontor 4cbebff43b Add production Docker setup and update backend/frontend configs
Introduces .dockerignore, production Dockerfile and nginx config for frontend, and refactors docker-compose.yml for multi-service deployment. Updates backend and frontend code to support public API tagging, improves refund handling, adds test email endpoint, and migrates Orval config to TypeScript. Removes unused frontend Dockerfile and updates dependencies for React Query and Orval.
2025-12-05 18:22:35 +01:00

110 lines
3.5 KiB
Python

import json
from django.apps import apps
from rest_framework.decorators import api_view
from rest_framework.response import Response
from drf_spectacular.utils import extend_schema, OpenApiParameter
from rest_framework.permissions import AllowAny
from account.tasks import send_email_test_task
@extend_schema(
tags=["public"],
description="Vrátí všechny možné hodnoty pro ChoiceField s podporou vícejazyčných labelů. "
"Umožňuje načíst více modelů a polí najednou.",
parameters=[
OpenApiParameter(
name="fields",
description=(
"JSON pole objektů {model: 'ModelName', field: 'field_name'} "
"např. '[{\"model\": \"User\", \"field\": \"role\"}, {\"model\": \"Carrier\", \"field\": \"shipping_method\"}]'"
),
required=True,
type=str,
),
OpenApiParameter(
name="lang",
description="Jazyk pro labely (např. 'cz', 'en')",
required=False,
type=str,
),
],
responses={
200: {
"type": "object",
"additionalProperties": {
"type": "array",
"items": {
"type": "object",
"properties": {
"value": {"type": "string", "description": "Logická hodnota pro backend"},
"label": {"type": "string", "description": "Human-readable label podle zvoleného jazyka"}
}
}
}
}
}
)
@api_view(["GET"])
def choices(request):
"""
Endpoint pro získání choices pro dropdowny s podporou vícejazyčných labelů.
Formát labelu v modelu: 'lang#Label', např. 'cz#Administrátor'.
Podporuje více modelů a polí najednou.
"""
permission_classes = [AllowAny]
fields_param = request.query_params.get("fields")
lang = request.query_params.get("lang", None)
try:
fields_list = json.loads(fields_param)
except Exception:
return Response({"error": "Invalid 'fields' parameter, must be valid JSON"}, status=400)
result = {}
for item in fields_list:
model_name = item.get("model")
field_name = item.get("field")
if not model_name or not field_name:
continue
model = apps.get_model("app_name", model_name)
field = model._meta.get_field(field_name)
choices_data = []
for value, label in field.choices:
if "#" in label and lang:
label_parts = label.split("#", 1)
if label_parts[0] == lang:
label = label_parts[1]
else:
label = label_parts[1] # fallback na defaultní text (po #)
elif "#" in label:
label = label.split("#", 1)[1]
choices_data.append({"value": value, "label": label})
result[f"{model_name}.{field_name}"] = choices_data
return Response(result)
@extend_schema(
tags=["Testing"],
description="Testovací endpoint pro odeslání testovacího emailu.",
parameters=[
OpenApiParameter(
name="email",
description="Emailová adresa příjemce testovacího emailu.",
required=True,
type=str,
),
],
)
@api_view(["GET"])
def test_email(request):
email = request.query_params.get("email")
send_email_test_task.delay(email)
return Response({"status": "Test task (celery) email sent, await delivery."})