Skip to content

Commit 960112e

Browse files
committed
Use match statement for handling services and drop COMPOSED_MT_SERVICES
1 parent 7b1a079 commit 960112e

1 file changed

Lines changed: 24 additions & 26 deletions

File tree

pontoon/machinery/views.py

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,6 @@
3131
from .openai_service import OpenAIService
3232

3333

34-
# Map machinery `service` query param to the locale attribute that indicates whether
35-
# the locale supports the MT service, used by the composed-translation view.
36-
# `translation-memory` is special-cased to mean "TM-only, no MT fallback".
37-
COMPOSED_MT_SERVICES = {
38-
"google-translate": "google_translate_code",
39-
"microsoft-translator": "ms_translator_code",
40-
}
41-
42-
4334
log = logging.getLogger(__name__)
4435

4536

@@ -120,26 +111,33 @@ def machinery_composed(request):
120111
{"status": False, "message": f"Bad Request: {e}"}, status=400
121112
)
122113

123-
if service == "translation-memory":
124-
# TM-only: no MT service is called (mt_supported=False).
125-
mt_service = None
126-
mt_service_name = "tm"
127-
mt_supported = False
128-
elif service in COMPOSED_MT_SERVICES:
129-
if not request.user.is_authenticated:
130-
return JsonResponse(
131-
{"status": False, "message": "Authentication required"}, status=403
132-
)
133-
if service == "google-translate":
114+
match service:
115+
case "translation-memory":
116+
# TM-only: no MT service is called (mt_supported=False).
117+
mt_service = None
118+
mt_service_name = "tm"
119+
mt_supported = False
120+
case "google-translate":
134121
mt_service = get_google_translate_data
135-
else:
122+
mt_service_name = service
123+
mt_supported = bool(locale.google_translate_code)
124+
case "microsoft-translator":
136125
mt_service = get_microsoft_translator_data
137-
mt_service_name = service
138-
mt_supported = bool(getattr(locale, COMPOSED_MT_SERVICES[service], None))
139-
else:
126+
mt_service_name = service
127+
mt_supported = bool(locale.ms_translator_code)
128+
case _:
129+
return JsonResponse(
130+
{
131+
"status": False,
132+
"message": f"Bad Request: unknown service `{service}`",
133+
},
134+
status=400,
135+
)
136+
137+
# MT services call an external provider; translation-memory is anonymous-friendly.
138+
if mt_service is not None and not request.user.is_authenticated:
140139
return JsonResponse(
141-
{"status": False, "message": f"Bad Request: unknown service `{service}`"},
142-
status=400,
140+
{"status": False, "message": "Authentication required"}, status=403
143141
)
144142

145143
# Only multi-pattern messages — those with multiple properties and/or

0 commit comments

Comments
 (0)