Coverage for app/venv/lib/python3.14/site-packages/weblate/trans/validators.py: 72%
42 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 07:15 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 07:15 +0000
1# Copyright © Michal Čihař <michal@weblate.org>
2#
3# SPDX-License-Identifier: GPL-3.0-or-later
5from __future__ import annotations
7import re
9from django.core.exceptions import ValidationError
10from django.utils.translation import gettext
11from pyparsing import ParseException
13from weblate.checks.flags import FlagsValidator
14from weblate.lang.models import Language
15from weblate.trans.defines import LANGUAGE_CODE_LENGTH
18def validate_filemask(val) -> None:
19 """Validate that the filemask contains *."""
20 if "*" not in val:
21 raise ValidationError(
22 gettext("File mask does not contain * as a language placeholder!")
23 )
26def validate_autoaccept(val) -> None:
27 """Validate correct value for automatic acceptance."""
28 if val == 1:
29 raise ValidationError(
30 gettext(
31 "A value of 1 is not allowed for automatic acceptance as "
32 "it would permit users to vote on their own suggestions."
33 )
34 )
37def validate_check_flags(val) -> None:
38 """Validate check-influencing flags."""
39 try:
40 flags = FlagsValidator(val)
41 except (ParseException, re.error) as error:
42 raise ValidationError(gettext("Could not parse flags: %s") % error) from error
43 flags.validate()
46def validate_language_code(code: str | None, filename: str, required: bool = False):
47 if not code: 47 ↛ 48line 47 didn't jump to line 48 because the condition on line 47 was never true
48 if not required:
49 return None
50 message = gettext(
51 'The language code for "%(filename)s" is empty, please check the file mask.'
52 ) % {"filename": filename}
53 raise ValidationError({"filemask": message})
55 if len(code) > LANGUAGE_CODE_LENGTH: 55 ↛ 56line 55 didn't jump to line 56 because the condition on line 55 was never true
56 message = gettext(
57 'The language code "%(code)s" for "%(filename)s" is too long,'
58 " please check the file mask."
59 ) % {"code": code, "filename": filename}
60 raise ValidationError({"filemask": message})
62 return Language.objects.auto_get_or_create(code=code, create=False)
65def validate_file_format_parameters(value: dict | None) -> None:
66 from weblate.trans.file_format_params import FILE_FORMATS_PARAMS
68 name_to_file_format_params = {param.name: param for param in FILE_FORMATS_PARAMS}
70 if value is None:
71 return
73 if not isinstance(value, dict):
74 raise ValidationError(
75 gettext("File format parameters must be a dictionary of key-value pairs.")
76 )
78 for param_name, param_value in value.items(): 78 ↛ 79line 78 didn't jump to line 79 because the loop on line 78 never started
79 if param_name in name_to_file_format_params:
80 param = name_to_file_format_params[param_name]
81 param().get_field().clean(param_value)
82 else:
83 raise ValidationError(
84 gettext('Unknown file format parameter: "%(param_name)s".')
85 % {"param_name": param_name}
86 )