-
-
Notifications
You must be signed in to change notification settings - Fork 957
/
forms.py
322 lines (284 loc) · 10.6 KB
/
forms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# Copyright © Michal Čihař <michal@weblate.org>
#
# SPDX-License-Identifier: GPL-3.0-or-later
import json
from django import forms
from django.core.exceptions import ValidationError
from django.utils.translation import gettext, gettext_lazy, pgettext_lazy
from weblate.utils.validators import WeblateServiceURLValidator
class BaseMachineryForm(forms.Form):
def __init__(self, machinery, *args, **kwargs) -> None:
self.machinery = machinery
super().__init__(*args, **kwargs)
def serialize_form(self):
return self.cleaned_data
def clean(self) -> None:
settings = self.serialize_form()
for field, data in self.fields.items():
if not data.required:
continue
if field not in settings:
return
machinery = self.machinery(settings)
machinery.validate_settings()
class KeyMachineryForm(BaseMachineryForm):
key = forms.CharField(
label=pgettext_lazy("Automatic suggestion service configuration", "API key")
)
class URLMachineryForm(BaseMachineryForm):
url = forms.URLField(
label=pgettext_lazy("Automatic suggestion service configuration", "API URL"),
validators=[WeblateServiceURLValidator()],
)
class KeySecretMachineryForm(KeyMachineryForm):
key = forms.CharField(
label=pgettext_lazy("Automatic suggestion service configuration", "Client ID")
)
secret = forms.CharField(
label=pgettext_lazy(
"Automatic suggestion service configuration", "Client secret"
)
)
class KeyURLMachineryForm(KeyMachineryForm, URLMachineryForm):
pass
class LibreTranslateMachineryForm(KeyURLMachineryForm):
key = forms.CharField(
label=pgettext_lazy("Automatic suggestion service configuration", "API key"),
required=False,
)
class MyMemoryMachineryForm(BaseMachineryForm):
email = forms.EmailField(
label=pgettext_lazy(
"Automatic suggestion service configuration", "Contact e-mail"
),
required=False,
)
username = forms.CharField(
label=pgettext_lazy("Automatic suggestion service configuration", "Username"),
required=False,
)
key = forms.CharField(
label=pgettext_lazy("Automatic suggestion service configuration", "API key"),
required=False,
)
class SAPMachineryForm(URLMachineryForm):
key = forms.CharField(
label=pgettext_lazy("Automatic suggestion service configuration", "API key"),
required=False,
)
username = forms.CharField(
label=pgettext_lazy(
"Automatic suggestion service configuration", "SAP username"
),
required=False,
)
password = forms.CharField(
label=pgettext_lazy(
"Automatic suggestion service configuration", "SAP password"
),
widget=forms.PasswordInput,
required=False,
)
enable_mt = forms.BooleanField(
label=pgettext_lazy(
"Automatic suggestion service configuration", "Enable machine translation"
),
required=False,
initial=True,
)
domain = forms.CharField(
label=pgettext_lazy(
"Automatic suggestion service configuration", "Translation domain"
),
help_text=gettext_lazy(
"The ID of a translation domain, for example, BC. If you do not specify "
"a domain, the method searches for translations in all available domains."
),
required=False,
)
def clean(self) -> None:
has_sandbox = bool(self.cleaned_data.get("key"))
has_production = self.cleaned_data.get("username") and self.cleaned_data.get(
"password"
)
if not has_sandbox and not has_production:
raise ValidationError(
gettext("Please provide either key or username and password.")
)
super().clean()
class MicrosoftMachineryForm(KeyMachineryForm):
base_url = forms.ChoiceField(
label=pgettext_lazy(
"Automatic suggestion service configuration", "Application base URL"
),
initial="api.cognitive.microsofttranslator.com",
choices=(
("api.cognitive.microsofttranslator.com", "Global (non-regional)"),
("api-apc.cognitive.microsofttranslator.com", "Asia Pacific"),
("api-eur.cognitive.microsofttranslator.com", "Europe"),
("api-nam.cognitive.microsofttranslator.com", "North America"),
("api.translator.azure.cn", "China"),
("api.cognitive.microsofttranslator.us", "Azure US Government cloud"),
),
)
endpoint_url = forms.ChoiceField(
label=pgettext_lazy(
"Automatic suggestion service configuration", "Authentication service URL"
),
initial="api.cognitive.microsoft.com",
choices=(
("api.cognitive.microsoft.com", "Global"),
("api.cognitive.azure.cn", "China"),
("api.cognitive.microsoft.us", "Azure US Government cloud"),
),
help_text=gettext_lazy(
"Regional or multi-service can be specified using region field below."
),
)
region = forms.CharField(
label=pgettext_lazy(
"Automatic suggestion service configuration",
"Authentication service region",
),
required=False,
)
category = forms.CharField(
label=pgettext_lazy("Automatic suggestion service configuration", "Category"),
help_text=pgettext_lazy(
"Automatic suggestion service configuration",
"Specify a customized system category ID to use it instead of general one.",
),
initial="general",
required=False,
)
class GoogleV3MachineryForm(BaseMachineryForm):
credentials = forms.CharField(
label=pgettext_lazy(
"Automatic suggestion service configuration",
"Google Translate service account info",
),
widget=forms.Textarea,
help_text=pgettext_lazy(
"Google Cloud Translation configuration",
"Enter a JSON key for the service account.",
),
)
project = forms.CharField(
label=pgettext_lazy(
"Automatic suggestion service configuration", "Google Translate project"
),
help_text=pgettext_lazy(
"Google Cloud Translation configuration",
"Enter the numeric or alphanumeric ID of your Google Cloud project.",
),
)
location = forms.CharField(
label=pgettext_lazy(
"Automatic suggestion service configuration", "Google Translate location"
),
initial="global",
help_text=pgettext_lazy(
"Google Cloud Translation configuration",
"Choose a Google Cloud Translation region that is used for the Google Cloud project or is closest to you.",
),
widget=forms.Select(
choices=(
("global ", pgettext_lazy("Google Cloud region", "Global")),
("europe-west1 ", pgettext_lazy("Google Cloud region", "Europe")),
("us-west1", pgettext_lazy("Google Cloud region", "US")),
)
),
)
def clean_credentials(self):
try:
json.loads(self.cleaned_data["credentials"])
except json.JSONDecodeError as error:
raise ValidationError(gettext("Could not parse JSON: %s") % error)
return self.cleaned_data["credentials"]
class AWSMachineryForm(KeySecretMachineryForm):
key = forms.CharField(
label=pgettext_lazy("AWS Translate configuration", "Access key ID")
)
secret = forms.CharField(
label=pgettext_lazy("AWS Translate configuration", "API secret key")
)
region = forms.CharField(
label=pgettext_lazy("AWS Translate configuration", "Region name")
)
class AlibabaMachineryForm(KeySecretMachineryForm):
key = forms.CharField(
label=pgettext_lazy("Alibaba Translate configuration", "Access key ID")
)
secret = forms.CharField(
label=pgettext_lazy("Alibaba Translate configuration", "Access key secret")
)
region = forms.CharField(
label=pgettext_lazy("Alibaba Translate configuration", "Region ID")
)
class ModernMTMachineryForm(KeyURLMachineryForm):
url = forms.URLField(
label=pgettext_lazy("Automatic suggestion service configuration", "API URL"),
initial="https://api.modernmt.com/",
validators=[WeblateServiceURLValidator()],
)
class DeepLMachineryForm(KeyURLMachineryForm):
url = forms.URLField(
label=pgettext_lazy("Automatic suggestion service configuration", "API URL"),
initial="https://api.deepl.com/v2/",
validators=[WeblateServiceURLValidator()],
)
formality = forms.CharField(
label=pgettext_lazy("Automatic suggestion service configuration", "Formality"),
help_text=gettext_lazy(
"Uses the specified formality if language is not specified as (in)formal"
),
widget=forms.Select(
choices=(
("default ", "Default"),
("prefer_more", "Formal"),
("prefer_less", "Informal"),
)
),
initial="default",
required=False,
)
class OpenAIMachineryForm(KeyMachineryForm):
# Ordering choices here defines priority for automatic selection
MODEL_CHOICES = (
("auto", pgettext_lazy("OpenAI model selection", "Automatic selection")),
("gpt-4o", "GPT-4o"),
("gpt-4-1106-preview", "GPT-4 Turbo"),
("gpt-4", "GPT-4"),
("gpt-3.5-turbo-1106", "Updated GPT 3.5 Turbo"),
("gpt-3.5-turbo", "GPT-3.5 Turbo"),
)
model = forms.ChoiceField(
label=pgettext_lazy(
"Automatic suggestion service configuration",
"OpenAI model",
),
initial="auto",
choices=MODEL_CHOICES,
)
persona = forms.CharField(
label=pgettext_lazy(
"Automatic suggestion service configuration",
"Translator persona",
),
widget=forms.Textarea,
help_text=gettext_lazy(
"Describe the persona of translator to improve the accuracy of the translation. For example: “You are a squirrel breeder.”"
),
required=False,
)
style = forms.CharField(
label=pgettext_lazy(
"Automatic suggestion service configuration",
"Translator style",
),
widget=forms.Textarea,
help_text=gettext_lazy(
"Describe the style of translation. For example: “Use informal language.”"
),
required=False,
)