Coverage for baibu/localization/forms.py: 100%

25 statements  

« prev     ^ index     » next       coverage.py v7.10.7, created at 2026-10-06 13:07 +0000

1from django import forms 

2from django.utils.translation import gettext_lazy as _ 

3 

4from . import catalogues 

5 

6MAX_UPLOAD_BYTES = 5 * 1024 * 1024 

7 

8 

9class PluralFormsForm(forms.Form): 

10 plural_forms = forms.CharField( 

11 label=_("Plural forms"), 

12 max_length=500, 

13 help_text=_("The gettext Plural-Forms rule for this language, for example “nplurals=1; plural=0;”."), 

14 ) 

15 

16 def clean_plural_forms(self): 

17 value = self.cleaned_data["plural_forms"].strip() 

18 try: 

19 catalogues.parse_plural_forms(value) 

20 except catalogues.CatalogueError as exc: 

21 raise forms.ValidationError(str(exc)) from exc 

22 return value 

23 

24 

25class UploadForm(forms.Form): 

26 file = forms.FileField( 

27 label=_("PO file"), 

28 help_text=_("Translations for messages in the draft are taken; other messages are ignored."), 

29 ) 

30 

31 def clean_file(self): 

32 upload = self.cleaned_data["file"] 

33 if upload.size > MAX_UPLOAD_BYTES: 

34 raise forms.ValidationError(_("The file is too large.")) 

35 data = upload.read() 

36 try: 

37 catalogues.parse(data) 

38 except (catalogues.CatalogueError, UnicodeDecodeError) as exc: 

39 raise forms.ValidationError(_("This is not a valid PO file.")) from exc 

40 return data